mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Add AccompanyingPeriodWorkEvaluationDenormalizer
This commit is contained in:
parent
1ab4f997ba
commit
15487cd84c
@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument;
|
||||
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
|
||||
|
||||
use function array_key_exists;
|
||||
use function array_merge;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* This denormalizer rely on AbstractNormalizer for most of the job, and
|
||||
* add some logic for synchronizing collection.
|
||||
*/
|
||||
class AccompanyingPeriodWorkEvaluationDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface
|
||||
{
|
||||
use DenormalizerAwareTrait;
|
||||
|
||||
use ObjectToPopulateTrait;
|
||||
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
private AccompanyingPeriodWorkRepository $workRepository;
|
||||
|
||||
public function __construct(
|
||||
AccompanyingPeriodWorkRepository $workRepository,
|
||||
EntityManagerInterface $em
|
||||
) {
|
||||
$this->workRepository = $workRepository;
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function denormalize($data, $type, $format = null, array $context = [])
|
||||
{
|
||||
$work = $this->denormalizer->denormalize($data, $type, $format, array_merge(
|
||||
$context,
|
||||
['skip' => self::class]
|
||||
));
|
||||
dump($work);
|
||||
dump($data);
|
||||
dump($type);
|
||||
//if (in_array('accompanying_period_work:edit', $context['groups'] ?? [], true)) {
|
||||
$this->handleEvaluationCollection($data, $work, $format, $context);
|
||||
//}
|
||||
|
||||
return $work;
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
|
||||
{
|
||||
return AccompanyingPeriodWorkEvaluation::class === $type
|
||||
&& self::class !== ($context['skip'] ?? null)
|
||||
&& is_array($data)
|
||||
&& array_key_exists('type', $data)
|
||||
&& 'accompanying_period_work_evaluation' === $data['type'];
|
||||
}
|
||||
|
||||
private function handleEvaluationCollection(array $data, AccompanyingPeriodWorkEvaluation $evaluation, string $format, array $context)
|
||||
{
|
||||
$dataById = [];
|
||||
$dataWithoutId = [];
|
||||
|
||||
foreach ($data['documents'] as $e) {
|
||||
if (array_key_exists('id', $e)) {
|
||||
$dataById[$e['id']] = $e;
|
||||
} else {
|
||||
$dataWithoutId[] = $e;
|
||||
}
|
||||
}
|
||||
dump($dataById);
|
||||
dump($dataWithoutId);
|
||||
|
||||
dump($evaluation);
|
||||
// partition the separate kept documents and removed one
|
||||
[$kept, $removed] = $evaluation->getDocuments()
|
||||
->partition(
|
||||
static fn (int $key, AccompanyingPeriodWorkEvaluationDocument $a) => array_key_exists($a->getId(), $dataById)
|
||||
);
|
||||
|
||||
|
||||
|
||||
// remove the document from evaluation
|
||||
foreach ($removed as $r) {
|
||||
$evaluation->removeDocument($r);
|
||||
}
|
||||
// handle the documents kept
|
||||
foreach ($kept as $k) {
|
||||
$this->denormalizer->denormalize(
|
||||
$dataById[$k->getId()],
|
||||
AccompanyingPeriodWorkEvaluationDocument::class,
|
||||
$format,
|
||||
array_merge(
|
||||
$context,
|
||||
[
|
||||
'groups' => ['write'],
|
||||
AbstractNormalizer::OBJECT_TO_POPULATE => $k,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
// create new document
|
||||
foreach ($dataWithoutId as $newData) {
|
||||
$document = $this->denormalizer->denormalize(
|
||||
$newData,
|
||||
AccompanyingPeriodWorkEvaluationDocument::class,
|
||||
$format,
|
||||
array_merge(
|
||||
$context,
|
||||
['groups' => ['accompanying_period_work_evaluation:create']]
|
||||
)
|
||||
);
|
||||
$evaluation->addDocument($document);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user