45 lines
1.4 KiB
PHP

<?php
namespace Chill\PersonBundle\Validator\Constraints\Person;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
use Chill\PersonBundle\Entity\Person;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class PersonHasCenterValidator extends \Symfony\Component\Validator\ConstraintValidator
{
private bool $centerRequired;
private CenterResolverDispatcher $centerResolverDispatcher;
public function __construct(ParameterBagInterface $parameterBag, CenterResolverDispatcher $centerResolverDispatcher)
{
$this->centerRequired = $parameterBag->get('chill_person')['validation']['center_required'];
$this->centerResolverDispatcher = $centerResolverDispatcher;
}
/**
* @inheritDoc
*/
public function validate($person, Constraint $constraint)
{
if (!$person instanceof Person) {
throw new UnexpectedTypeException($constraint, Person::class);
}
if (!$this->centerRequired) {
return;
}
if (NULL === $this->centerResolverDispatcher->resolveCenter($person)) {
$this
->context
->buildViolation($constraint->message)
->atPath('center')
->addViolation()
;
}
}
}