mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
validation on simultaneous household membership for a person
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\Household;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
*/
|
||||
class HouseholdMembershipSequential extends Constraint
|
||||
{
|
||||
public $message = 'household_membership.Person with membership covering';
|
||||
|
||||
public function getTargets()
|
||||
{
|
||||
return [ self::CLASS_CONSTRAINT ];
|
||||
}
|
||||
}
|
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\Household;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\MainBundle\Util\DateRangeCovering;
|
||||
use Chill\PersonBundle\Templating\Entity\PersonRender;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Validate that a person does not belong to two household at
|
||||
* the same time
|
||||
*/
|
||||
class HouseholdMembershipSequentialValidator extends ConstraintValidator
|
||||
{
|
||||
private PersonRender $render;
|
||||
|
||||
public function __construct(PersonRender $render)
|
||||
{
|
||||
$this->render = $render;
|
||||
}
|
||||
|
||||
public function validate($person, Constraint $constraint)
|
||||
{
|
||||
if (!$person instanceof Person) {
|
||||
throw new UnexpectedTypeException($constraint, Person::class);
|
||||
}
|
||||
|
||||
$participations = $person->getHouseholdParticipations();
|
||||
|
||||
if ($participations->count() === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$covers = new DateRangeCovering(1, $participations[0]
|
||||
->getStartDate()->getTimezone());
|
||||
|
||||
foreach ($participations as $k => $p) {
|
||||
$covers->add($p->getStartDate(), $p->getEndDate(), $k);
|
||||
}
|
||||
|
||||
$covers->compute();
|
||||
|
||||
if ($covers->hasIntersections()) {
|
||||
foreach ($covers->getIntersections() as list($start, $end, $metadata)) {
|
||||
$participation = $participations[$metadata[0]];
|
||||
|
||||
$this->context
|
||||
->buildViolation("household_membership.Person with membership covering")
|
||||
->setParameters([
|
||||
'%person_name%' => $this->render->renderString(
|
||||
$participation->getPerson(), []
|
||||
),
|
||||
// TODO when date is correctly i18n, fix this
|
||||
'%from%' => $start->format('d-m-Y')
|
||||
])
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user