Files
chill-bundles/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidityValidator.php

64 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Templating\Entity\PersonRenderInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class LocationValidityValidator extends ConstraintValidator
{
public function __construct(private readonly PersonRenderInterface $render)
{
}
public function validate($period, Constraint $constraint)
{
if (!$constraint instanceof LocationValidity) {
throw new UnexpectedTypeException($constraint, LocationValidity::class);
}
if (!$period instanceof AccompanyingPeriod) {
throw new UnexpectedValueException($period, 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();
}
}
}