mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
backend: allow to remove people from household, or move to a new household
This commit is contained in:
@@ -24,6 +24,58 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
{
|
||||
// some test about schema first...
|
||||
$this->performChecks($data);
|
||||
|
||||
// route to "leave movement" (all concerned leave household)
|
||||
// or "move to another household" (all concerned go to another
|
||||
// household)
|
||||
if (NULL === $data['destination']) {
|
||||
return $this->denormalizeLeave($data, $type, $format, $context);
|
||||
} else {
|
||||
return $this->denormalizeMove($data, $type, $format, $context);
|
||||
}
|
||||
}
|
||||
|
||||
private function performChecks($data): void
|
||||
{
|
||||
if (NULL == $data['concerned'] ?? NULL
|
||||
&& FALSE === ·\is_array('concerned')) {
|
||||
throw new Exception\UnexpectedValueException("The schema does not have any key 'concerned'");
|
||||
}
|
||||
|
||||
if (FALSE === \array_key_exists('destination', $data)) {
|
||||
throw new Exception\UnexpectedValueException("The schema does not have any key 'destination'");
|
||||
}
|
||||
}
|
||||
|
||||
protected function denormalizeLeave($data, string $type, string $format, array $context = [])
|
||||
{
|
||||
$editor = $this->factory->createEditor(null);
|
||||
|
||||
foreach ($data['concerned'] as $key => $concerned) {
|
||||
$person = $this->denormalizer->denormalize($concerned['person'] ?? null, Person::class,
|
||||
$format, $context);
|
||||
$startDate = $this->denormalizer->denormalize($concerned['start_date'] ?? null, \DateTimeImmutable::class,
|
||||
$format, $context);
|
||||
|
||||
if (
|
||||
NULL === $person
|
||||
&& NULL === $startDate
|
||||
) {
|
||||
throw new Exception\InvalidArgumentException("position with ".
|
||||
"key $key could not be denormalized: missing ".
|
||||
"person or start_date.");
|
||||
}
|
||||
|
||||
$editor->leaveMovement($startDate, $person);
|
||||
}
|
||||
|
||||
return $editor;
|
||||
}
|
||||
|
||||
protected function denormalizeMove($data, string $type, string $format, array $context = [])
|
||||
{
|
||||
$household = $this->denormalizer->denormalize($data['destination'], Household::class,
|
||||
$format, $context);
|
||||
@@ -34,11 +86,6 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
|
||||
|
||||
$editor = $this->factory->createEditor($household);
|
||||
|
||||
if (NULL == $data['concerned'] ?? NULL
|
||||
&& FALSE === ·\is_array('concerned')) {
|
||||
throw new Exception\UnexpectedValueException("The schema does not have any key 'concerned'");
|
||||
}
|
||||
|
||||
foreach ($data['concerned'] as $key => $concerned) {
|
||||
$person = $this->denormalizer->denormalize($concerned['person'] ?? null, Person::class,
|
||||
$format, $context);
|
||||
@@ -62,9 +109,9 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
|
||||
|
||||
$editor->addMovement($startDate, $person, $position, $holder,
|
||||
$comment);
|
||||
|
||||
return $editor;
|
||||
}
|
||||
|
||||
return $editor;
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, string $format = null)
|
||||
|
Reference in New Issue
Block a user