48 lines
1.5 KiB
PHP

<?php
namespace Chill\PersonBundle\Validator\Constraints\Household;
use Chill\MainBundle\Util\DateRangeCovering;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class MaxHolderValidator extends ConstraintValidator
{
private const MAX_HOLDERS = 2;
public function validate($household, Constraint $constraint)
{
$holders = $household->getMembersHolder();
if ($holders->count() <= self::MAX_HOLDERS) {
return;
}
$covers = new DateRangeCovering(self::MAX_HOLDERS,
$holders[0]->getStartDate()->getTimezone());
foreach ($holders as $key => $member) {
$covers->add($member->getStartDate(), $member->getEndDate(), $key);
}
$covers->compute();
if ($covers->hasIntersections()) {
foreach ($covers->getIntersections() as list($start, $end, $ids)) {
$msg = $end === null ? $constraint->messageEndInfinite :
$constraint->message;
$this->context->buildViolation($msg)
->setParameters([
'start' => $start->format('d-m-Y'), // TODO fix when MessageParameter works with timezone
'end' => $end === null ? null : $end->format('d-m-Y')
])
->addViolation();
}
}
}
}