mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
74 lines
1.8 KiB
PHP
74 lines
1.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
|
|
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\Person;
|
|
|
|
use DateInterval;
|
|
use DateTime;
|
|
use LogicException;
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
use function get_class;
|
|
use function gettype;
|
|
use function is_object;
|
|
|
|
class BirthdateValidator extends ConstraintValidator
|
|
{
|
|
private $interval_spec;
|
|
|
|
public function __construct($interval_spec = null)
|
|
{
|
|
$this->interval_spec = $interval_spec;
|
|
}
|
|
|
|
public function validate($value, Constraint $constraint)
|
|
{
|
|
if (null === $value) {
|
|
return;
|
|
}
|
|
|
|
if (!$value instanceof DateTime) {
|
|
throw new LogicException('The input should a be a \DateTime interface,'
|
|
. (is_object($value) ? get_class($value) : gettype($value)));
|
|
}
|
|
|
|
$limitDate = $this->getLimitDate();
|
|
|
|
if ($limitDate < $value) {
|
|
$this->context->buildViolation($constraint->message)
|
|
->setParameter('%date%', $limitDate->format('d-m-Y'))
|
|
->setCode(Birthdate::BIRTHDATE_INVALID_CODE)
|
|
->addViolation();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return DateTime
|
|
*/
|
|
private function getLimitDate()
|
|
{
|
|
if (null !== $this->interval_spec) {
|
|
$interval = new DateInterval($this->interval_spec);
|
|
|
|
return (new DateTime('now'))->sub($interval);
|
|
}
|
|
|
|
return new DateTime('now');
|
|
}
|
|
}
|