[AccompanyingPeriod] create constraints for location

This commit is contained in:
2021-07-28 14:46:03 +02:00
parent 4e8dd3b189
commit 3c7b67604c
4 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
<?php
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Templating\Entity\PersonRender;
class LocationValidityValidator extends ConstraintValidator
{
private PersonRender $render;
public function __construct(PersonRender $render)
{
$this->render = $render;
}
public function validate($period, Constraint $constraint)
{
if (!$constraint instanceof LocationValidity) {
throw new UnexpectedTypeException($constraint, LocationValidity::class);
}
if (!$period instanceof AccompanyingPeriod) {
throw new UnexpectedValueException($value, AccompanyingPeriod::class);
}
if ($period->getLocationStatus() === 'person') {
if (null === $period->getOpenParticipationContainsPerson(
$period->getPersonLocation())) {
$this->context->buildViolation($constraint->messagePersonLocatedMustBeAssociated)
->setParameter('{{ person_name }}', $this->render->renderString(
$period->getPersonLocation(), []
))
->addViolation()
;
}
}
if ($period->getStep() !== AccompanyingPeriod::STEP_DRAFT
&& $period->getLocationStatus() === 'none') {
$this->context->buildViolation($constraint->messagePeriodMustRemainsLocated
->addViolation()
;
}
}
}