mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
105 lines
3.5 KiB
PHP
105 lines
3.5 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\Center;
|
|
use Chill\MainBundle\Entity\Location;
|
|
use Chill\MainBundle\Entity\Scope;
|
|
use Chill\MainBundle\Entity\User;
|
|
use Chill\MainBundle\Entity\UserJob;
|
|
use Chill\MainBundle\Templating\Entity\UserRender;
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
|
|
|
class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
|
|
{
|
|
use NormalizerAwareTrait;
|
|
|
|
public const NULL_USER = [
|
|
'type' => 'user',
|
|
'id' => '',
|
|
'username' => '',
|
|
'text' => '',
|
|
'label' => '',
|
|
'email' => '',
|
|
];
|
|
|
|
private UserRender $userRender;
|
|
|
|
public function __construct(UserRender $userRender)
|
|
{
|
|
$this->userRender = $userRender;
|
|
}
|
|
|
|
public function normalize($user, $format = null, array $context = [])
|
|
{
|
|
/** @var User $user */
|
|
$userJobContext = array_merge(
|
|
$context,
|
|
['docgen:expects' => UserJob::class, 'groups' => 'docgen:read']
|
|
);
|
|
$scopeContext = array_merge(
|
|
$context,
|
|
['docgen:expects' => Scope::class, 'groups' => 'docgen:read']
|
|
);
|
|
$centerContext = array_merge(
|
|
$context,
|
|
['docgen:expects' => Center::class, 'groups' => 'docgen:read']
|
|
);
|
|
$locationContext = array_merge(
|
|
$context,
|
|
['docgen:expects' => Location::class, 'groups' => 'dogen:read']
|
|
);
|
|
|
|
if (null === $user && 'docgen' === $format) {
|
|
return array_merge(self::NULL_USER, [
|
|
'user_job' => $this->normalizer->normalize(null, $format, $userJobContext),
|
|
'main_center' => $this->normalizer->normalize(null, $format, $centerContext),
|
|
'main_scope' => $this->normalizer->normalize(null, $format, $scopeContext),
|
|
'current_location' => $this->normalizer->normalize(null, $format, $locationContext),
|
|
]);
|
|
}
|
|
|
|
$data = [
|
|
'type' => 'user',
|
|
'id' => $user->getId(),
|
|
'username' => $user->getUsername(),
|
|
'text' => $this->userRender->renderString($user, []),
|
|
'label' => $user->getLabel(),
|
|
'email' => (string) $user->getEmail(),
|
|
'user_job' => $this->normalizer->normalize($user->getUserJob(), $format, $userJobContext),
|
|
'main_center' => $this->normalizer->normalize($user->getMainCenter(), $format, $centerContext),
|
|
'main_scope' => $this->normalizer->normalize($user->getMainScope(), $format, $scopeContext),
|
|
];
|
|
|
|
if ('docgen' === $format) {
|
|
$data['current_location'] = $this->normalizer->normalize($user->getCurrentLocation(), $format, $locationContext);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function supportsNormalization($data, $format = null, array $context = []): bool
|
|
{
|
|
if ($data instanceof User && ('json' === $format || 'docgen' === $format)) {
|
|
return true;
|
|
}
|
|
|
|
if (null === $data && 'docgen' === $format && User::class === ($context['docgen:expects'] ?? null)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|