mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-10-31 17:28:23 +00:00 
			
		
		
		
	[AccompanyingPeriod] create constraints for location
This commit is contained in:
		| @@ -0,0 +1,20 @@ | ||||
| <?php | ||||
|  | ||||
| namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod; | ||||
|  | ||||
| use Symfony\Component\Validator\Constraint; | ||||
|  | ||||
| /** | ||||
|  * @Annotation | ||||
|  */ | ||||
| class LocationValidity extends Constraint | ||||
| { | ||||
|     public $messagePersonLocatedMustBeAssociated = "The person where the course is located must be associated to the course. Change course's location before removing the person."; | ||||
|  | ||||
|     public $messagPeriodMustRemainsLocated = "The period must remains located"; | ||||
|  | ||||
|     public function getTargets() | ||||
|     { | ||||
|         return self::CLASS_CONSTRAINT; | ||||
|     } | ||||
| } | ||||
| @@ -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() | ||||
|                           ; | ||||
|               | ||||
|         } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -85,3 +85,8 @@ services: | ||||
|         resource: '../Templating/Entity' | ||||
|         tags: | ||||
|             - 'chill.render_entity' | ||||
|  | ||||
|     Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\: | ||||
|         autowire: true | ||||
|         autoconfigure: true | ||||
|         resource: '../Validator/Constraints/AccompanyingPeriod' | ||||
|   | ||||
| @@ -81,6 +81,7 @@ Chill\PersonBundle\Entity\AccompanyingPeriod: | ||||
|     constraints: | ||||
|           - Callback: | ||||
|               callback: isDateConsistent | ||||
|           - 'Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\LocationValidity': ~ | ||||
|  | ||||
| Chill\PersonBundle\Entity\PersonPhone: | ||||
|     properties: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user