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\GraphQl\Serializer; |
||
15 | |||
16 | use ApiPlatform\Core\Api\IdentifiersExtractorInterface; |
||
17 | use ApiPlatform\Core\Api\IriConverterInterface; |
||
18 | use ApiPlatform\Core\Util\ClassInfoTrait; |
||
19 | use Symfony\Component\Serializer\Exception\UnexpectedValueException; |
||
20 | use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
||
21 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
||
22 | |||
23 | /** |
||
24 | * Decorates the output with GraphQL metadata when appropriate, but otherwise just |
||
25 | * passes through to the decorated normalizer. |
||
26 | */ |
||
27 | final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface |
||
28 | { |
||
29 | use ClassInfoTrait; |
||
30 | |||
31 | public const FORMAT = 'graphql'; |
||
32 | public const ITEM_RESOURCE_CLASS_KEY = '#itemResourceClass'; |
||
33 | public const ITEM_IDENTIFIERS_KEY = '#itemIdentifiers'; |
||
34 | |||
35 | private $decorated; |
||
36 | private $iriConverter; |
||
37 | private $identifiersExtractor; |
||
38 | |||
39 | public function __construct(NormalizerInterface $decorated, IriConverterInterface $iriConverter, IdentifiersExtractorInterface $identifiersExtractor) |
||
40 | { |
||
41 | $this->decorated = $decorated; |
||
42 | $this->iriConverter = $iriConverter; |
||
43 | $this->identifiersExtractor = $identifiersExtractor; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function supportsNormalization($data, $format = null, array $context = []): bool |
||
50 | { |
||
51 | return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context); |
||
0 ignored issues
–
show
|
|||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function hasCacheableSupportsMethod(): bool |
||
58 | { |
||
59 | return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | * |
||
65 | * @throws UnexpectedValueException |
||
66 | */ |
||
67 | public function normalize($object, $format = null, array $context = []) |
||
68 | { |
||
69 | if (isset($context['api_resource'])) { |
||
70 | $originalResource = $context['api_resource']; |
||
71 | unset($context['api_resource']); |
||
72 | } |
||
73 | |||
74 | $data = $this->decorated->normalize($object, $format, $context); |
||
75 | if (!\is_array($data)) { |
||
76 | throw new UnexpectedValueException('Expected data to be an array'); |
||
77 | } |
||
78 | |||
79 | if (!isset($originalResource)) { |
||
80 | return $data; |
||
81 | } |
||
82 | |||
83 | if (isset($data['id'])) { |
||
84 | $data['_id'] = $data['id']; |
||
85 | $data['id'] = $this->iriConverter->getIriFromItem($originalResource); |
||
86 | } |
||
87 | |||
88 | $data[self::ITEM_RESOURCE_CLASS_KEY] = $this->getObjectClass($originalResource); |
||
89 | $data[self::ITEM_IDENTIFIERS_KEY] = $this->identifiersExtractor->getIdentifiersFromItem($originalResource); |
||
90 | |||
91 | return $data; |
||
92 | } |
||
93 | } |
||
94 |
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.