[AccompanyingPeriod] create constraints for location

This commit is contained in:
Julien Fastré 2021-07-28 14:46:03 +02:00 committed by Marc Ducobu
parent 3302d69373
commit a3a5d5cfd0
4 changed files with 78 additions and 0 deletions

View File

@ -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;
}
}

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()
;
}
}
}

View File

@ -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'

View File

@ -81,6 +81,7 @@ Chill\PersonBundle\Entity\AccompanyingPeriod:
constraints:
- Callback:
callback: isDateConsistent
- 'Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\LocationValidity': ~
Chill\PersonBundle\Entity\PersonPhone:
properties: