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\JsonApi\Serializer; |
||||||
15 | |||||||
16 | use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface; |
||||||
17 | use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
||||||
18 | |||||||
19 | /** |
||||||
20 | * Reserved attribute name converter. |
||||||
21 | * |
||||||
22 | * @author Baptiste Meyer <[email protected]> |
||||||
23 | */ |
||||||
24 | final class ReservedAttributeNameConverter implements AdvancedNameConverterInterface |
||||||
25 | { |
||||||
26 | public const JSON_API_RESERVED_ATTRIBUTES = [ |
||||||
27 | 'id' => '_id', |
||||||
28 | 'type' => '_type', |
||||||
29 | 'links' => '_links', |
||||||
30 | 'relationships' => '_relationships', |
||||||
31 | 'included' => '_included', |
||||||
32 | ]; |
||||||
33 | |||||||
34 | private $nameConverter; |
||||||
35 | |||||||
36 | public function __construct(NameConverterInterface $nameConverter = null) |
||||||
37 | { |
||||||
38 | $this->nameConverter = $nameConverter; |
||||||
39 | } |
||||||
40 | |||||||
41 | /** |
||||||
42 | * {@inheritdoc} |
||||||
43 | */ |
||||||
44 | public function normalize($propertyName, string $class = null, string $format = null, array $context = []) |
||||||
45 | { |
||||||
46 | if (null !== $this->nameConverter) { |
||||||
47 | $propertyName = $this->nameConverter->normalize($propertyName, $class, $format, $context); |
||||||
0 ignored issues
–
show
|
|||||||
48 | } |
||||||
49 | |||||||
50 | if (isset(self::JSON_API_RESERVED_ATTRIBUTES[$propertyName])) { |
||||||
51 | $propertyName = self::JSON_API_RESERVED_ATTRIBUTES[$propertyName]; |
||||||
52 | } |
||||||
53 | |||||||
54 | return $propertyName; |
||||||
55 | } |
||||||
56 | |||||||
57 | /** |
||||||
58 | * {@inheritdoc} |
||||||
59 | */ |
||||||
60 | public function denormalize($propertyName, string $class = null, string $format = null, array $context = []) |
||||||
61 | { |
||||||
62 | if (\in_array($propertyName, self::JSON_API_RESERVED_ATTRIBUTES, true)) { |
||||||
63 | $propertyName = substr($propertyName, 1); |
||||||
64 | } |
||||||
65 | |||||||
66 | if (null !== $this->nameConverter) { |
||||||
67 | $propertyName = $this->nameConverter->denormalize($propertyName, $class, $format, $context); |
||||||
0 ignored issues
–
show
The call to
Symfony\Component\Serial...nterface::denormalize() has too many arguments starting with $class .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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.
Loading history...
|
|||||||
68 | } |
||||||
69 | |||||||
70 | return $propertyName; |
||||||
71 | } |
||||||
72 | } |
||||||
73 |
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.