normalization for docgen:read: add groups and so on (wip)

This commit is contained in:
2021-12-06 13:18:59 +01:00
parent 51fd81c661
commit c4998f4ac1
15 changed files with 166 additions and 70 deletions

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
@@ -30,18 +31,48 @@ class SocialActionNormalizer implements NormalizerAwareInterface, NormalizerInte
public function normalize($socialAction, ?string $format = null, array $context = [])
{
return [
'id' => $socialAction->getId(),
'type' => 'social_work_social_action',
'text' => $this->render->renderString($socialAction, []),
'parent' => $this->normalizer->normalize($socialAction->getParent()),
'desactivationDate' => $this->normalizer->normalize($socialAction->getDesactivationDate()),
'title' => $socialAction->getTitle(),
];
switch ($format) {
case 'json':
return [
'id' => $socialAction->getId(),
'type' => 'social_work_social_action',
'text' => $this->render->renderString($socialAction, []),
'parent' => $this->normalizer->normalize($socialAction->getParent()),
'desactivationDate' => $this->normalizer->normalize($socialAction->getDesactivationDate()),
'title' => $socialAction->getTitle(),
];
case 'docgen':
if (null === $socialAction) {
return ['id' => 0, 'title' => '', 'text' => ''];
}
return [
'id' => $socialAction->getId(),
'text' => $this->render->renderString($socialAction, []),
'title' => $socialAction->getTitle(),
];
default:
throw new \Symfony\Component\Serializer\Exception\RuntimeException("format not supported");
}
}
public function supportsNormalization($data, ?string $format = null)
public function supportsNormalization($data, string $format = null, array $context = [])
{
return $data instanceof SocialAction;
if ($data instanceof SocialAction && 'json' === $format) {
return true;
}
if ('docgen' === $format) {
if ($data instanceof SocialAction) {
return true;
}
if (null === $data && ($context['docgen:expects'] ?? null) === SocialAction::class) {
return true;
}
}
return false;
}
}