From a3a5d5cfd0704f3bf0db606538931f578ed6ccd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 28 Jul 2021 14:46:03 +0200 Subject: [PATCH] [AccompanyingPeriod] create constraints for location --- .../AccompanyingPeriod/LocationValidity.php | 20 +++++++ .../LocationValidityValidator.php | 52 +++++++++++++++++++ .../ChillPersonBundle/config/services.yaml | 5 ++ .../ChillPersonBundle/config/validation.yaml | 1 + 4 files changed, 78 insertions(+) create mode 100644 src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidity.php create mode 100644 src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidityValidator.php diff --git a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidity.php b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidity.php new file mode 100644 index 000000000..9a64b3cf6 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidity.php @@ -0,0 +1,20 @@ +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() + ; + + } + + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services.yaml b/src/Bundle/ChillPersonBundle/config/services.yaml index aedea4396..c2f60a729 100644 --- a/src/Bundle/ChillPersonBundle/config/services.yaml +++ b/src/Bundle/ChillPersonBundle/config/services.yaml @@ -97,3 +97,8 @@ services: resource: '../Templating/Entity' tags: - 'chill.render_entity' + + Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\: + autowire: true + autoconfigure: true + resource: '../Validator/Constraints/AccompanyingPeriod' diff --git a/src/Bundle/ChillPersonBundle/config/validation.yaml b/src/Bundle/ChillPersonBundle/config/validation.yaml index fb0dd7b50..23369a467 100644 --- a/src/Bundle/ChillPersonBundle/config/validation.yaml +++ b/src/Bundle/ChillPersonBundle/config/validation.yaml @@ -81,6 +81,7 @@ Chill\PersonBundle\Entity\AccompanyingPeriod: constraints: - Callback: callback: isDateConsistent + - 'Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\LocationValidity': ~ Chill\PersonBundle\Entity\PersonPhone: properties: