validation on simultaneous household membership for a person

This commit is contained in:
2021-06-14 12:56:01 +02:00
parent 51399b21b9
commit 17c3ecbabe
9 changed files with 112 additions and 3 deletions

View File

@@ -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 ];
}
}

View File

@@ -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()
;
}
}
}
}