mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
|
|
|
|
use Chill\MainBundle\Util\DateRangeCovering;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
|
|
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ParticipationOverlap;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|
|
|
class ParticipationOverlapValidator extends ConstraintValidator
|
|
{
|
|
private const MAX_PARTICIPATION = 1;
|
|
|
|
public function validate($participations, Constraint $constraint)
|
|
{
|
|
if (!$constraint instanceof ParticipationOverlap) {
|
|
throw new UnexpectedTypeException($constraint, ParticipationOverlap::class);
|
|
}
|
|
|
|
if (!$participations instanceof Collection) {
|
|
throw new UnexpectedTypeException($participations, 'This should be a collection');
|
|
}
|
|
|
|
if (count($participations) <= self::MAX_PARTICIPATION) {
|
|
return;
|
|
}
|
|
|
|
$overlaps = new DateRangeCovering(self::MAX_PARTICIPATION, $participations[0]->getStartDate()->getTimezone());
|
|
$participationList = [];
|
|
|
|
foreach ($participations as $participation) {
|
|
|
|
if (!$participation instanceof AccompanyingPeriodParticipation) {
|
|
throw new UnexpectedTypeException($participation, AccompanyingPeriodParticipation::class);
|
|
}
|
|
|
|
$personId = $participation->getPerson()->getId();
|
|
|
|
$participationList[$personId][] = $participation;
|
|
|
|
}
|
|
|
|
foreach ($participationList as $group) {
|
|
if (count($group) > 1) {
|
|
foreach ($group as $p) {
|
|
$overlaps->add($p->getStartDate(), $p->getEndDate(), $p->getId());
|
|
}
|
|
}
|
|
}
|
|
|
|
$overlaps->compute();
|
|
|
|
if ($overlaps->hasIntersections()) {
|
|
foreach ($overlaps->getIntersections() as list($start, $end, $ids)) {
|
|
$msg = $end === null ? $constraint->message :
|
|
$constraint->message;
|
|
|
|
$this->context->buildViolation($msg)
|
|
->setParameters([
|
|
'{{ start }}' => $start->format('d-m-Y'),
|
|
'{{ end }}' => $end === null ? null : $end->format('d-m-Y'),
|
|
'{{ ids }}' => $ids,
|
|
])
|
|
->addViolation();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|