Completed
Push — master ( a1da24...44a686 )
by Antoine
18s queued 11s
created

ItemNormalizer::normalize()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 5
nop 3
dl 0
loc 34
rs 9.6
c 0
b 0
f 0
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\Serializer\AbstractItemNormalizer;
17
use ApiPlatform\Core\Serializer\ContextTrait;
18
use ApiPlatform\Core\Util\ClassInfoTrait;
19
use Symfony\Component\Serializer\Exception\LogicException;
20
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
21
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
22
23
/**
24
 * Converts between objects and array including HAL metadata.
25
 *
26
 * @author Kévin Dunglas <[email protected]>
27
 */
28
final class ItemNormalizer extends AbstractItemNormalizer
29
{
30
    use ContextTrait;
31
    use ClassInfoTrait;
32
33
    public const FORMAT = 'jsonhal';
34
35
    private $componentsCache = [];
36
    private $attributesMetadataCache = [];
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function supportsNormalization($data, $format = null, array $context = []): bool
42
    {
43
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function normalize($object, $format = null, array $context = [])
50
    {
51
        if (null !== $outputClass = $this->getOutputClass($this->getObjectClass($object), $context)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $outputClass is dead and can be removed.
Loading history...
52
            return parent::normalize($object, $format, $context);
53
        }
54
55
        if (!isset($context['cache_key'])) {
56
            $context['cache_key'] = $this->getHalCacheKey($format, $context);
57
        }
58
59
        // Use resolved resource class instead of given resource class to support multiple inheritance child types
60
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
61
        $context = $this->initContext($resourceClass, $context);
62
        $iri = $this->iriConverter->getIriFromItem($object);
63
        $context['iri'] = $iri;
64
        $context['api_normalize'] = true;
65
66
        $data = parent::normalize($object, $format, $context);
67
        if (!\is_array($data)) {
68
            return $data;
69
        }
70
71
        $metadata = [
72
            '_links' => [
73
                'self' => [
74
                    'href' => $iri,
75
                ],
76
            ],
77
        ];
78
        $components = $this->getComponents($object, $format, $context);
79
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'links');
80
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'embedded');
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
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function getAttributes($object, $format = null, array $context): array
108
    {
109
        return $this->getComponents($object, $format, $context)['states'];
110
    }
111
112
    /**
113
     * Gets HAL components of the resource: states, links and embedded.
114
     *
115
     * @param object $object
116
     */
117
    private function getComponents($object, ?string $format, array $context): array
118
    {
119
        $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key'];
120
121
        if (isset($this->componentsCache[$cacheKey])) {
122
            return $this->componentsCache[$cacheKey];
123
        }
124
125
        $attributes = parent::getAttributes($object, $format, $context);
126
        $options = $this->getFactoryOptions($context);
127
128
        $components = [
129
            'states' => [],
130
            'links' => [],
131
            'embedded' => [],
132
        ];
133
134
        foreach ($attributes as $attribute) {
135
            $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
136
137
            $type = $propertyMetadata->getType();
138
            $isOne = $isMany = false;
139
140
            if (null !== $type) {
141
                if ($type->isCollection()) {
142
                    $valueType = $type->getCollectionValueType();
143
                    $isMany = null !== $valueType && ($className = $valueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
144
                } else {
145
                    $className = $type->getClassName();
146
                    $isOne = $className && $this->resourceClassResolver->isResourceClass($className);
147
                }
148
            }
149
150
            if (!$isOne && !$isMany) {
151
                $components['states'][] = $attribute;
152
                continue;
153
            }
154
155
            $relation = ['name' => $attribute, 'cardinality' => $isOne ? 'one' : 'many'];
156
            if ($propertyMetadata->isReadableLink()) {
157
                $components['embedded'][] = $relation;
158
            }
159
160
            $components['links'][] = $relation;
161
        }
162
163
        if (false !== $context['cache_key']) {
164
            $this->componentsCache[$cacheKey] = $components;
165
        }
166
167
        return $components;
168
    }
169
170
    /**
171
     * Populates _links and _embedded keys.
172
     *
173
     * @param object $object
174
     */
175
    private function populateRelation(array $data, $object, ?string $format, array $context, array $components, string $type): array
176
    {
177
        $class = $this->getObjectClass($object);
178
179
        $attributesMetadata = \array_key_exists($class, $this->attributesMetadataCache) ?
180
            $this->attributesMetadataCache[$class] :
181
            $this->attributesMetadataCache[$class] = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
182
183
        $key = '_'.$type;
184
        foreach ($components[$type] as $relation) {
185
            if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $relation['name'], $context)) {
186
                continue;
187
            }
188
189
            $attributeValue = $this->getAttributeValue($object, $relation['name'], $format, $context);
190
            if (empty($attributeValue)) {
191
                continue;
192
            }
193
194
            $relationName = $relation['name'];
195
            if ($this->nameConverter) {
196
                $relationName = $this->nameConverter->normalize($relationName, $class, $format, $context);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Serial...rInterface::normalize() 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 ignore-call  annotation

196
                /** @scrutinizer ignore-call */ 
197
                $relationName = $this->nameConverter->normalize($relationName, $class, $format, $context);

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...
197
            }
198
199
            if ('one' === $relation['cardinality']) {
200
                if ('links' === $type) {
201
                    $data[$key][$relationName]['href'] = $this->getRelationIri($attributeValue);
202
                    continue;
203
                }
204
205
                $data[$key][$relationName] = $attributeValue;
206
                continue;
207
            }
208
209
            // many
210
            $data[$key][$relationName] = [];
211
            foreach ($attributeValue as $rel) {
212
                if ('links' === $type) {
213
                    $rel = ['href' => $this->getRelationIri($rel)];
214
                }
215
216
                $data[$key][$relationName][] = $rel;
217
            }
218
        }
219
220
        return $data;
221
    }
222
223
    /**
224
     * Gets the IRI of the given relation.
225
     *
226
     * @throws UnexpectedValueException
227
     */
228
    private function getRelationIri($rel): string
229
    {
230
        if (!(\is_array($rel) || \is_string($rel))) {
231
            throw new UnexpectedValueException('Expected relation to be an IRI or array');
232
        }
233
234
        return \is_string($rel) ? $rel : $rel['_links']['self']['href'];
235
    }
236
237
    /**
238
     * Gets the cache key to use.
239
     *
240
     * @return bool|string
241
     */
242
    private function getHalCacheKey(?string $format, array $context)
243
    {
244
        try {
245
            return md5($format.serialize($context));
246
        } catch (\Exception $exception) {
247
            // The context cannot be serialized, skip the cache
248
            return false;
249
        }
250
    }
251
252
    /**
253
     * Is the max depth reached for the given attribute?
254
     *
255
     * @param AttributeMetadataInterface[] $attributesMetadata
256
     */
257
    private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
258
    {
259
        if (
260
            !($context[self::ENABLE_MAX_DEPTH] ?? false) ||
261
            !isset($attributesMetadata[$attribute]) ||
262
            null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
263
        ) {
264
            return false;
265
        }
266
267
        $key = sprintf(self::DEPTH_KEY_PATTERN, $class, $attribute);
268
        if (!isset($context[$key])) {
269
            $context[$key] = 1;
270
271
            return false;
272
        }
273
274
        if ($context[$key] === $maxDepth) {
275
            return true;
276
        }
277
278
        ++$context[$key];
279
280
        return false;
281
    }
282
}
283