chill-bundles/src/Bundle/ChillMainBundle/Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php
2022-01-27 11:50:26 +01:00

89 lines
2.7 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\MainBundle\Serializer\Normalizer;
use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\UserRepository;
use DateTime;
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 function array_key_exists;
class CommentEmbeddableDocGenNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private UserRepository $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* @param CommentEmbeddable $object
*
* @throws ExceptionInterface
*/
public function normalize($object, ?string $format = null, array $context = []): array
{
if (null === $object) {
return [
'comment' => '',
'isNull' => true,
'date' => $this->normalizer->normalize(null, $format, array_merge($context, [
'docgen:expects' => DateTime::class,
])),
'user' => $this->normalizer->normalize(null, $format, array_merge($context, [
'docgen:expects' => User::class,
])),
];
}
$user = $this->userRepository->find($object->getUserId());
return [
'comment' => $object->getComment(),
'isNull' => false,
'date' => $this->normalizer->normalize($object->getDate(), $format, array_merge($context, [
'docgen:expects' => DateTime::class,
])),
'user' => $this->normalizer->normalize($user, $format, array_merge($context, [
'docgen:expects' => User::class,
])),
];
}
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
if ('docgen' !== $format) {
return false;
}
if ($data instanceof CommentEmbeddable) {
return true;
}
if (
null === $data
&& array_key_exists('docgen:expects', $context)
&& CommentEmbeddable::class === $context['docgen:expects']) {
return true;
}
return false;
}
}