Serializer made for privateComment

This commit is contained in:
Julie Lenaerts 2022-04-27 11:50:32 +02:00
parent 3ed2b36057
commit 27ce146aa0
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,56 @@
<?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\Embeddable\PrivateCommentEmbeddable;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class PrivateCommentEmbeddableNormalizer implements NormalizerInterface, DenormalizerInterface
{
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function denormalize($data, string $type, ?string $format = null, array $context = [])
{
if (null === $data) {
return null;
}
$comment = new PrivateCommentEmbeddable();
$comment->setCommentForUser($this->security->getUser(), $data['privateComment']);
}
public function supportsDenormalization($data, string $type, ?string $format = null)
{
return $type === CommentEmbeddable::class;
}
public function normalize($comment, $format = null, array $contect = [])
{
return $comment->getComment();
}
public function supportsNormalization($data, ?string $format = null)
{
return $data instanceof CommentEmbeddable;
}
}

View File

@ -10,4 +10,7 @@ services:
Chill\MainBundle\Serializer\Normalizer\DoctrineExistingEntityNormalizer:
tags:
- { name: 'serializer.normalizer', priority: 8 }
Chill\MainBundle\Serializer\Normalizer\PrivateCommentEmbeddableNormalizer:
autowire: true
autoconfigure: true