51 lines
1.6 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\User;
use Chill\MainBundle\Templating\Entity\UserRender;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class UserNormalizer implements NormalizerAwareInterface, NormalizerInterface
{
use NormalizerAwareTrait;
private UserRender $userRender;
public function __construct(UserRender $userRender)
{
$this->userRender = $userRender;
}
public function normalize($user, ?string $format = null, array $context = [])
{
/** @var User $user */
return [
'type' => 'user',
'id' => $user->getId(),
'username' => $user->getUsername(),
'text' => $this->userRender->renderString($user, []),
'label' => $user->getLabel(),
'user_job' => $this->normalizer->normalize($user->getUserJob(), $format, $context),
'main_center' => $this->normalizer->normalize($user->getMainCenter(), $format, $context),
'main_scope' => $this->normalizer->normalize($user->getMainScope(), $format, $context),
];
}
public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof User && ('json' === $format || 'docgen' === $format);
}
}