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

ItemNormalizer::normalize()   B

Complexity

Conditions 7
Paths 19

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 19
nop 3
dl 0
loc 50
rs 8.5386
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\JsonApi\Serializer;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use ApiPlatform\Core\Api\OperationType;
18
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
19
use ApiPlatform\Core\Exception\ItemNotFoundException;
20
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
23
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
24
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
25
use ApiPlatform\Core\Serializer\ContextTrait;
26
use ApiPlatform\Core\Util\ClassInfoTrait;
27
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
28
use Symfony\Component\Serializer\Exception\LogicException;
29
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
30
use Symfony\Component\Serializer\Exception\RuntimeException;
31
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
32
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
33
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
34
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
35
36
/**
37
 * Converts between objects and array.
38
 *
39
 * @author Kévin Dunglas <[email protected]>
40
 * @author Amrouche Hamza <[email protected]>
41
 * @author Baptiste Meyer <[email protected]>
42
 */
43
final class ItemNormalizer extends AbstractItemNormalizer
44
{
45
    use ClassInfoTrait;
46
    use ContextTrait;
47
48
    public const FORMAT = 'jsonapi';
49
50
    private $componentsCache = [];
51
52
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ?PropertyAccessorInterface $propertyAccessor, ?NameConverterInterface $nameConverter, ResourceMetadataFactoryInterface $resourceMetadataFactory, array $defaultContext = [], iterable $dataTransformers = [])
53
    {
54
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, null, null, false, $defaultContext, $dataTransformers, $resourceMetadataFactory);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function supportsNormalization($data, $format = null, array $context = []): bool
61
    {
62
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function normalize($object, $format = null, array $context = [])
69
    {
70
        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...
71
            return parent::normalize($object, $format, $context);
72
        }
73
74
        if (!isset($context['cache_key'])) {
75
            $context['cache_key'] = $this->getJsonApiCacheKey($format, $context);
76
        }
77
78
        // Use resolved resource class instead of given resource class to support multiple inheritance child types
79
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
80
        $context = $this->initContext($resourceClass, $context);
81
        $iri = $this->iriConverter->getIriFromItem($object);
82
        $context['iri'] = $iri;
83
        $context['api_normalize'] = true;
84
85
        $data = parent::normalize($object, $format, $context);
86
        if (!\is_array($data)) {
87
            return $data;
88
        }
89
90
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
91
92
        // Get and populate relations
93
        $allRelationshipsData = $this->getComponents($object, $format, $context)['relationships'];
94
        $populatedRelationContext = $context;
95
        $relationshipsData = $this->getPopulatedRelations($object, $format, $populatedRelationContext, $allRelationshipsData);
96
        $includedResourcesData = $this->getRelatedResources($object, $format, $context, $allRelationshipsData);
97
98
        $resourceData = [
99
            'id' => $context['iri'],
100
            'type' => $resourceMetadata->getShortName(),
101
        ];
102
103
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
            $resourceData['attributes'] = $data;
105
        }
106
107
        if ($relationshipsData) {
108
            $resourceData['relationships'] = $relationshipsData;
109
        }
110
111
        $document = ['data' => $resourceData];
112
113
        if ($includedResourcesData) {
114
            $document['included'] = $includedResourcesData;
115
        }
116
117
        return $document;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
124
    {
125
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     *
131
     * @throws NotNormalizableValueException
132
     */
133
    public function denormalize($data, $class, $format = null, array $context = [])
134
    {
135
        // Avoid issues with proxies if we populated the object
136
        if (!isset($context[self::OBJECT_TO_POPULATE]) && isset($data['data']['id'])) {
137
            if (true !== ($context['api_allow_update'] ?? true)) {
138
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
139
            }
140
141
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri(
142
                $data['data']['id'],
143
                $context + ['fetch_data' => false]
144
            );
145
        }
146
147
        // Merge attributes and relationships, into format expected by the parent normalizer
148
        $dataToDenormalize = array_merge(
149
            $data['data']['attributes'] ?? [],
150
            $data['data']['relationships'] ?? []
151
        );
152
153
        return parent::denormalize(
154
            $dataToDenormalize,
155
            $class,
156
            $format,
157
            $context
158
        );
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    protected function getAttributes($object, $format = null, array $context): array
165
    {
166
        return $this->getComponents($object, $format, $context)['attributes'];
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = []): void
173
    {
174
        parent::setAttributeValue($object, $attribute, \is_array($value) && \array_key_exists('data', $value) ? $value['data'] : $value, $format, $context);
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     *
180
     * @see http://jsonapi.org/format/#document-resource-object-linkage
181
     *
182
     * @throws LogicException
183
     * @throws RuntimeException
184
     * @throws NotNormalizableValueException
185
     */
186
    protected function denormalizeRelation(string $attributeName, PropertyMetadata $propertyMetadata, string $className, $value, ?string $format, array $context)
187
    {
188
        // Give a chance to other normalizers (e.g.: DateTimeNormalizer)
189
        if (!$this->resourceClassResolver->isResourceClass($className)) {
190
            $context['resource_class'] = $className;
191
192
            if ($this->serializer instanceof DenormalizerInterface) {
193
                return $this->serializer->denormalize($value, $className, $format, $context);
194
            }
195
            throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', DenormalizerInterface::class));
196
        }
197
198
        if (!\is_array($value) || !isset($value['id'], $value['type'])) {
199
            throw new NotNormalizableValueException('Only resource linkage supported currently, see: http://jsonapi.org/format/#document-resource-object-linkage.');
200
        }
201
202
        try {
203
            return $this->iriConverter->getItemFromIri($value['id'], $context + ['fetch_data' => true]);
204
        } catch (ItemNotFoundException $e) {
205
            throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
206
        }
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     *
212
     * @see http://jsonapi.org/format/#document-resource-object-linkage
213
     *
214
     * @throws LogicException
215
     */
216
    protected function normalizeRelation(PropertyMetadata $propertyMetadata, $relatedObject, string $resourceClass, ?string $format, array $context)
217
    {
218
        if (null === $relatedObject) {
219
            if (isset($context['operation_type'], $context['subresource_resources'][$resourceClass]) && OperationType::SUBRESOURCE === $context['operation_type']) {
220
                $iri = $this->iriConverter->getItemIriFromResourceClass($resourceClass, $context['subresource_resources'][$resourceClass]);
221
            } else {
222
                unset($context['resource_class']);
223
224
                if ($this->serializer instanceof NormalizerInterface) {
225
                    return $this->serializer->normalize($relatedObject, $format, $context);
226
                }
227
                throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
228
            }
229
        } else {
230
            $iri = $this->iriConverter->getIriFromItem($relatedObject);
231
            $context['iri'] = $iri;
232
233
            if (isset($context['resources'])) {
234
                $context['resources'][$iri] = $iri;
235
            }
236
            if (isset($context['api_included'])) {
237
                if (!$this->serializer instanceof NormalizerInterface) {
238
                    throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
239
                }
240
241
                return $this->serializer->normalize($relatedObject, $format, $context);
242
            }
243
        }
244
245
        return [
246
            'data' => [
247
                'type' => $this->resourceMetadataFactory->create($resourceClass)->getShortName(),
248
                'id' => $iri,
249
            ],
250
        ];
251
    }
252
253
    /**
254
     * {@inheritdoc}
255
     */
256
    protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = []): bool
257
    {
258
        return preg_match('/^\\w[-\\w_]*$/', $attribute) && parent::isAllowedAttribute($classOrObject, $attribute, $format, $context);
259
    }
260
261
    /**
262
     * Gets JSON API components of the resource: attributes, relationships, meta and links.
263
     *
264
     * @param object $object
265
     */
266
    private function getComponents($object, ?string $format, array $context): array
267
    {
268
        $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key'];
269
270
        if (isset($this->componentsCache[$cacheKey])) {
271
            return $this->componentsCache[$cacheKey];
272
        }
273
274
        $attributes = parent::getAttributes($object, $format, $context);
275
276
        $options = $this->getFactoryOptions($context);
277
278
        $components = [
279
            'links' => [],
280
            'relationships' => [],
281
            'attributes' => [],
282
            'meta' => [],
283
        ];
284
285
        foreach ($attributes as $attribute) {
286
            $propertyMetadata = $this
287
                ->propertyMetadataFactory
288
                ->create($context['resource_class'], $attribute, $options);
289
290
            $type = $propertyMetadata->getType();
291
            $isOne = $isMany = false;
292
293
            if (null !== $type) {
294
                if ($type->isCollection()) {
295
                    $isMany = ($type->getCollectionValueType() && $className = $type->getCollectionValueType()->getClassName()) ? $this->resourceClassResolver->isResourceClass($className) : false;
296
                } else {
297
                    $isOne = ($className = $type->getClassName()) ? $this->resourceClassResolver->isResourceClass($className) : false;
298
                }
299
            }
300
301
            if (!isset($className) || !$isOne && !$isMany) {
302
                $components['attributes'][] = $attribute;
303
304
                continue;
305
            }
306
307
            $relation = [
308
                'name' => $attribute,
309
                'type' => $this->resourceMetadataFactory->create($className)->getShortName(),
310
                'cardinality' => $isOne ? 'one' : 'many',
311
            ];
312
313
            $components['relationships'][] = $relation;
314
        }
315
316
        if (false !== $context['cache_key']) {
317
            $this->componentsCache[$cacheKey] = $components;
318
        }
319
320
        return $components;
321
    }
322
323
    /**
324
     * Populates relationships keys.
325
     *
326
     * @param object $object
327
     *
328
     * @throws UnexpectedValueException
329
     */
330
    private function getPopulatedRelations($object, ?string $format, array $context, array $relationships): array
331
    {
332
        $data = [];
333
334
        if (!isset($context['resource_class'])) {
335
            return $data;
336
        }
337
338
        unset($context['api_included']);
339
        foreach ($relationships as $relationshipDataArray) {
340
            $relationshipName = $relationshipDataArray['name'];
341
342
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);
343
344
            if ($this->nameConverter) {
345
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Serial...rInterface::normalize() has too many arguments starting with $context['resource_class']. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

345
                /** @scrutinizer ignore-call */ 
346
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::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...
346
            }
347
348
            if (!$attributeValue) {
349
                continue;
350
            }
351
352
            $data[$relationshipName] = [
353
                'data' => [],
354
            ];
355
356
            // Many to one relationship
357
            if ('one' === $relationshipDataArray['cardinality']) {
358
                unset($attributeValue['data']['attributes']);
359
                $data[$relationshipName] = $attributeValue;
360
361
                continue;
362
            }
363
364
            // Many to many relationship
365
            foreach ($attributeValue as $attributeValueElement) {
366
                if (!isset($attributeValueElement['data'])) {
367
                    throw new UnexpectedValueException(sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName));
368
                }
369
                unset($attributeValueElement['data']['attributes']);
370
                $data[$relationshipName]['data'][] = $attributeValueElement['data'];
371
            }
372
        }
373
374
        return $data;
375
    }
376
377
    /**
378
     * Populates included keys.
379
     */
380
    private function getRelatedResources($object, ?string $format, array $context, array $relationships): array
381
    {
382
        if (!isset($context['api_included'])) {
383
            return [];
384
        }
385
386
        $included = [];
387
        foreach ($relationships as $relationshipDataArray) {
388
            if (!\in_array($relationshipDataArray['name'], $context['api_included'], true)) {
389
                continue;
390
            }
391
392
            $relationshipName = $relationshipDataArray['name'];
393
            $relationContext = $context;
394
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $relationContext);
395
396
            if (!$attributeValue) {
397
                continue;
398
            }
399
400
            // Many to one relationship
401
            if ('one' === $relationshipDataArray['cardinality']) {
402
                $included[] = $attributeValue['data'];
403
404
                continue;
405
            }
406
            // Many to many relationship
407
            foreach ($attributeValue as $attributeValueElement) {
408
                if (isset($attributeValueElement['data'])) {
409
                    $included[] = $attributeValueElement['data'];
410
                }
411
            }
412
        }
413
414
        return $included;
415
    }
416
417
    /**
418
     * Gets the cache key to use.
419
     *
420
     * @return bool|string
421
     */
422
    private function getJsonApiCacheKey(?string $format, array $context)
423
    {
424
        try {
425
            return md5($format.serialize($context));
426
        } catch (\Exception $exception) {
427
            // The context cannot be serialized, skip the cache
428
            return false;
429
        }
430
    }
431
}
432