mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
128 lines
4.5 KiB
PHP
128 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Serializer\Normalizer;
|
|
|
|
use Chill\PersonBundle\Entity\Household\Position;
|
|
use Chill\PersonBundle\Entity\Household\Household;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
|
use Symfony\Component\Serializer\Exception;
|
|
use Chill\PersonBundle\Household\MembersEditorFactory;
|
|
use Chill\PersonBundle\Household\MembersEditor;
|
|
|
|
class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwareInterface
|
|
{
|
|
private MembersEditorFactory $factory;
|
|
|
|
use DenormalizerAwareTrait;
|
|
|
|
public function __construct(MembersEditorFactory $factory)
|
|
{
|
|
$this->factory = $factory;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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 = [])
|
|
{
|
|
$householdContext = $context;
|
|
$householdContext['groups'][] = 'create';
|
|
|
|
$household = $this->denormalizer->denormalize($data['destination'], Household::class,
|
|
$format, $householdContext);
|
|
|
|
if (NULL === $household) {
|
|
throw new Exception\InvalidArgumentException("household could not be denormalized. Impossible to process");
|
|
}
|
|
|
|
$editor = $this->factory->createEditor($household);
|
|
|
|
foreach ($data['concerned'] as $key => $concerned) {
|
|
$person = $this->denormalizer->denormalize($concerned['person'] ?? null, Person::class,
|
|
$format, $context);
|
|
$position = $this->denormalizer->denormalize($concerned['position'] ?? null, Position::class,
|
|
$format, $context);
|
|
$startDate = $this->denormalizer->denormalize($concerned['start_date'] ?? null, \DateTimeImmutable::class,
|
|
$format, $context);
|
|
|
|
$holder = (bool) $concerned['holder'] ?? false;
|
|
$comment = (string) $concerned['comment'] ?? false;
|
|
|
|
if (
|
|
NULL === $person
|
|
&& NULL === $position
|
|
&& NULL === $startDate
|
|
) {
|
|
throw new Exception\InvalidArgumentException("position with ".
|
|
"key $key could not be denormalized: missing ".
|
|
"person, position or start_date.");
|
|
}
|
|
|
|
$editor->addMovement($startDate, $person, $position, $holder,
|
|
$comment);
|
|
}
|
|
|
|
return $editor;
|
|
}
|
|
|
|
public function supportsDenormalization($data, string $type, string $format = null)
|
|
{
|
|
return $type === MembersEditor::class;
|
|
}
|
|
|
|
}
|