normalizer for entityworkflow

This commit is contained in:
2022-01-28 17:09:00 +01:00
parent da22532587
commit fdafe7c82b
4 changed files with 134 additions and 5 deletions

View File

@@ -0,0 +1,59 @@
<?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\MainBundle\Serializer\Normalizer;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Workflow\Registry;
class EntityWorkflowNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private MetadataExtractor $metadataExtractor;
private Registry $registry;
public function __construct(MetadataExtractor $metadataExtractor, Registry $registry)
{
$this->metadataExtractor = $metadataExtractor;
$this->registry = $registry;
}
/**
* @param EntityWorkflow $object
*
* @return array
*/
public function normalize($object, ?string $format = null, array $context = [])
{
$workflow = $this->registry->get($object, $object->getWorkflowName());
return [
'type' => 'entity_workflow',
'id' => $object->getId(),
'relatedEntityClass' => $object->getRelatedEntityClass(),
'relatedEntityId' => $object->getRelatedEntityId(),
'workflow' => $this->metadataExtractor->buildArrayPresentationForWorkflow($workflow),
'current_step' => $this->metadataExtractor->buildArrayPresentationForPlace($object),
'steps' => $this->normalizer->normalize($object->getStepsChained(), $format, $context),
];
}
public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof EntityWorkflow && 'json' === $format;
}
}