mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
create max holder validator
This commit is contained in:
@@ -2,7 +2,46 @@
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\Household;
|
||||
|
||||
class MaxHolderValidator
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user