← Back
Editing: ControlTimerValidator.php
<?php namespace App\Validator; use App\Entity\Controle; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedValueException; class ControlTimerValidator extends ConstraintValidator { /** * @throws \Exception */ public function validate(mixed $value, Constraint $constraint) { $control = $value; if (!$control instanceof Controle) { throw new UnexpectedValueException($control, Controle::class); } if (!$constraint instanceof ControlTimer) { throw new UnexpectedValueException($constraint, ControlTimer::class); } if (null === $control->getAgent()->getLastControlDate()) { return; } $minControlDate = new \DateTime($control->getAgent()->getLastControlDate()->format('Y-m-d H:i:s')); $minControlDate->add(new \DateInterval(sprintf('PT%sM', ControlTimer::CONTROL_DELAY))); if ($control->getCreatedAt() < $minControlDate) { $this->context ->buildViolation($constraint->message) ->atPath('items') ->addViolation(); } } }
Save File
Cancel