chill-bundles/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodWorkNormalizer.php
2022-01-24 13:17:46 +00:00

86 lines
2.9 KiB
PHP

<?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 ArrayObject;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Workflow\Registry;
use function array_key_exists;
class AccompanyingPeriodWorkNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private const IGNORE_WORK = 'ignore:work';
private EntityWorkflowRepository $entityWorkflowRepository;
private MetadataExtractor $metadataExtractor;
private Registry $registry;
public function __construct(Registry $registry, EntityWorkflowRepository $entityWorkflowRepository, MetadataExtractor $metadataExtractor)
{
$this->registry = $registry;
$this->entityWorkflowRepository = $entityWorkflowRepository;
$this->metadataExtractor = $metadataExtractor;
}
/**
* @param AccompanyingPeriodWork $object
*
* @throws ExceptionInterface
*
* @return array|ArrayObject|bool|float|int|string|null
*/
public function normalize($object, ?string $format = null, array $context = [])
{
$initial = $this->normalizer->normalize($object, $format, array_merge(
$context,
[self::IGNORE_WORK => spl_object_hash($object)]
));
// then, we add normalization for things which are not into the entity
$initial['workflows_availables'] = $this->metadataExtractor->availableWorkflowFor(
AccompanyingPeriodWork::class,
$object->getId()
);
$initial['workflows_availables_evaluation'] = $this->metadataExtractor->availableWorkflowFor(
AccompanyingPeriodWorkEvaluation::class
);
$workflows = $this->entityWorkflowRepository->findBy([
'relatedEntityClass' => AccompanyingPeriodWork::class,
'relatedEntityId' => $object->getId(),
]);
$initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $context);
return $initial;
}
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
return 'json' === $format
&& $data instanceof AccompanyingPeriodWork
&& !array_key_exists(self::IGNORE_WORK, $context);
}
}