1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the API Platform project. |
||
5 | * |
||
6 | * (c) Kévin Dunglas <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace ApiPlatform\Core\Hal\Serializer; |
||
15 | |||
16 | use ApiPlatform\Core\Api\IriConverterInterface; |
||
17 | use Symfony\Component\Serializer\Exception\LogicException; |
||
18 | use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
||
19 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
||
20 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
||
21 | |||
22 | /** |
||
23 | * Decorates the output with JSON HAL metadata when appropriate, but otherwise |
||
24 | * just passes through to the decorated normalizer. |
||
25 | */ |
||
26 | final class ObjectNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface |
||
27 | { |
||
28 | public const FORMAT = 'jsonhal'; |
||
29 | |||
30 | private $decorated; |
||
31 | private $iriConverter; |
||
32 | |||
33 | public function __construct(NormalizerInterface $decorated, IriConverterInterface $iriConverter) |
||
34 | { |
||
35 | $this->decorated = $decorated; |
||
36 | $this->iriConverter = $iriConverter; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function supportsNormalization($data, $format = null, array $context = []): bool |
||
43 | { |
||
44 | return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context); |
||
0 ignored issues
–
show
|
|||
45 | } |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function hasCacheableSupportsMethod(): bool |
||
51 | { |
||
52 | return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function normalize($object, $format = null, array $context = []) |
||
59 | { |
||
60 | if (isset($context['api_resource'])) { |
||
61 | $originalResource = $context['api_resource']; |
||
62 | unset($context['api_resource']); |
||
63 | } |
||
64 | |||
65 | $data = $this->decorated->normalize($object, $format, $context); |
||
66 | if (!\is_array($data)) { |
||
67 | return $data; |
||
68 | } |
||
69 | |||
70 | if (!isset($originalResource)) { |
||
71 | return $data; |
||
72 | } |
||
73 | |||
74 | $metadata = [ |
||
75 | '_links' => [ |
||
76 | 'self' => [ |
||
77 | 'href' => $this->iriConverter->getIriFromItem($originalResource), |
||
78 | ], |
||
79 | ], |
||
80 | ]; |
||
81 | |||
82 | return $metadata + $data; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
||
89 | { |
||
90 | // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format |
||
91 | return self::FORMAT === $format; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | * |
||
97 | * @throws LogicException |
||
98 | */ |
||
99 | public function denormalize($data, $class, $format = null, array $context = []) |
||
100 | { |
||
101 | throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT)); |
||
102 | } |
||
103 | } |
||
104 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.