1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Serializer\Normalizer; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
10
|
|
|
use Chamilo\CoreBundle\ServiceHelper\UserHelper; |
11
|
|
|
use LogicException; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
13
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
14
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
15
|
|
|
|
16
|
|
|
class SessionNormalizer implements NormalizerInterface, NormalizerAwareInterface |
17
|
|
|
{ |
18
|
|
|
use NormalizerAwareTrait; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
private readonly UserHelper $userHelper, |
22
|
|
|
) {} |
23
|
|
|
|
24
|
|
|
private const ALREADY_CALLED = 'SESSION_NORMALIZER_ALREADY_CALLED'; |
25
|
|
|
|
26
|
|
|
public function normalize($object, ?string $format = null, array $context = []): array |
27
|
|
|
{ |
28
|
|
|
$context[self::ALREADY_CALLED] = true; |
29
|
|
|
|
30
|
|
|
\assert($object instanceof Session); |
31
|
|
|
|
32
|
|
|
try { |
33
|
|
|
$object->getAccessVisibility(); |
34
|
|
|
} catch (LogicException) { |
35
|
|
|
$object->setAccessVisibilityByUser( |
36
|
|
|
$this->userHelper->getCurrent() |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function supportsNormalization($data, ?string $format = null, array $context = []): bool |
44
|
|
|
{ |
45
|
|
|
if (isset($context[self::ALREADY_CALLED])) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $data instanceof Session; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getSupportedTypes(?string $format): array |
53
|
|
|
{ |
54
|
|
|
return [Session::class => false]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|