Skip to content

Commit

Permalink
Internal: set user full name by NameConvention service when normalizi…
Browse files Browse the repository at this point in the history
…ng user data
  • Loading branch information
AngelFQC committed Jun 7, 2024
1 parent 007c75a commit 8456c08
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 53 deletions.
2 changes: 0 additions & 2 deletions src/CoreBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Chamilo\CoreBundle\Entity\Listener\UserListener;
use Chamilo\CoreBundle\Filter\SearchOr;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\CoreBundle\State\UserStateProvider;
use Chamilo\CoreBundle\Traits\UserCreatorTrait;
use Chamilo\CourseBundle\Entity\CGroupRelTutor;
use Chamilo\CourseBundle\Entity\CGroupRelUser;
Expand Down Expand Up @@ -64,7 +63,6 @@
],
normalizationContext: ['groups' => ['user:read']],
denormalizationContext: ['groups' => ['user:write']],
provider: UserStateProvider::class,
security: 'is_granted("ROLE_USER")'
)]
#[ORM\Table(name: 'user')]
Expand Down
49 changes: 49 additions & 0 deletions src/CoreBundle/Serializer/Normalizer/UserNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/* For licensing terms, see /license.txt */

declare(strict_types=1);

namespace Chamilo\CoreBundle\Serializer\Normalizer;

use Chamilo\CoreBundle\Component\Utils\NameConvention;
use Chamilo\CoreBundle\Entity\User;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class UserNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

private const ALREADY_CALLED = 'USER_NORMALIZER_ALREADY_CALLED';

public function __construct(
private readonly NameConvention $nameConvention,
) {}

public function normalize($object, ?string $format = null, array $context = []): array
{
$context[self::ALREADY_CALLED] = true;

$data = $this->normalizer->normalize($object, $format, $context);

$data['fullName'] = $this->nameConvention->getPersonName($object);

return $data;
}

public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
if (isset($context[self::ALREADY_CALLED])) {
return false;
}

return $data instanceof User;
}

public function getSupportedTypes(?string $format): array
{
return [User::class => false];
}
}
51 changes: 0 additions & 51 deletions src/CoreBundle/State/UserStateProvider.php

This file was deleted.

0 comments on commit 8456c08

Please sign in to comment.