mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
[AccompanyingPeriod] create constraints for location
This commit is contained in:
@@ -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()
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user