• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.63
/src/JsonApi/Serializer/ItemNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\JsonApi\Serializer;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
18
use ApiPlatform\Metadata\IriConverterInterface;
19
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
23
use ApiPlatform\Metadata\ResourceClassResolverInterface;
24
use ApiPlatform\Metadata\UrlGeneratorInterface;
25
use ApiPlatform\Metadata\Util\ClassInfoTrait;
26
use ApiPlatform\Serializer\AbstractItemNormalizer;
27
use ApiPlatform\Serializer\CacheKeyTrait;
28
use ApiPlatform\Serializer\ContextTrait;
29
use ApiPlatform\Serializer\TagCollectorInterface;
30
use Symfony\Component\ErrorHandler\Exception\FlattenException;
31
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
32
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
33
use Symfony\Component\Serializer\Exception\LogicException;
34
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
35
use Symfony\Component\Serializer\Exception\RuntimeException;
36
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
37
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
38
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
39
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
40
use Symfony\Component\TypeInfo\Type;
41
use Symfony\Component\TypeInfo\Type\CollectionType;
42
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
43
use Symfony\Component\TypeInfo\Type\ObjectType;
44
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
45

46
/**
47
 * Converts between objects and array.
48
 *
49
 * @author Kévin Dunglas <dunglas@gmail.com>
50
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
51
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
52
 */
53
final class ItemNormalizer extends AbstractItemNormalizer
54
{
55
    use CacheKeyTrait;
56
    use ClassInfoTrait;
57
    use ContextTrait;
58

59
    public const FORMAT = 'jsonapi';
60

61
    private array $componentsCache = [];
62

63
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
64
    {
65
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
1,160✔
66
    }
67

68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
72
    {
73
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context) && !($data instanceof \Exception || $data instanceof FlattenException);
83✔
74
    }
75

76
    public function getSupportedTypes($format): array
77
    {
78
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
1,048✔
79
    }
80

81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
85
    {
86
        $resourceClass = $this->getObjectClass($object);
84✔
87
        if ($this->getOutputClass($context)) {
84✔
88
            return parent::normalize($object, $format, $context);
4✔
89
        }
90

91
        $previousResourceClass = $context['resource_class'] ?? null;
84✔
92
        if ($this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
84✔
93
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
80✔
94
        }
95

96
        if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
84✔
UNCOV
97
            $context['item_uri_template'] = $operation->getItemUriTemplate();
9✔
98
        }
99

100
        $context = $this->initContext($resourceClass, $context);
84✔
101

102
        $iri = $context['iri'] ??= $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
84✔
103
        $context['object'] = $object;
84✔
104
        $context['format'] = $format;
84✔
105
        $context['api_normalize'] = true;
84✔
106

107
        if (!isset($context['cache_key'])) {
84✔
108
            $context['cache_key'] = $this->getCacheKey($format, $context);
84✔
109
        }
110

111
        $data = parent::normalize($object, $format, $context);
84✔
112
        if (!\is_array($data)) {
84✔
113
            return $data;
×
114
        }
115

116
        // Get and populate relations
117
        ['relationships' => $allRelationshipsData, 'links' => $links] = $this->getComponents($object, $format, $context);
84✔
118
        $populatedRelationContext = $context;
84✔
119
        $relationshipsData = $this->getPopulatedRelations($object, $format, $populatedRelationContext, $allRelationshipsData);
84✔
120

121
        // Do not include primary resources
122
        $context['api_included_resources'] = [$context['iri']];
84✔
123

124
        $includedResourcesData = $this->getRelatedResources($object, $format, $context, $allRelationshipsData);
84✔
125

126
        $resourceData = [
84✔
127
            'id' => $context['iri'],
84✔
128
            'type' => $this->getResourceShortName($resourceClass),
84✔
129
        ];
84✔
130

131
        if ($data) {
84✔
132
            $resourceData['attributes'] = $data;
80✔
133
        }
134

135
        if ($relationshipsData) {
84✔
136
            $resourceData['relationships'] = $relationshipsData;
47✔
137
        }
138

139
        $document = [];
84✔
140

141
        if ($links) {
84✔
UNCOV
142
            $document['links'] = $links;
1✔
143
        }
144

145
        $document['data'] = $resourceData;
84✔
146

147
        if ($includedResourcesData) {
84✔
UNCOV
148
            $document['included'] = $includedResourcesData;
15✔
149
        }
150

151
        return $document;
84✔
152
    }
153

154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
158
    {
UNCOV
159
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
7✔
160
    }
161

162
    /**
163
     * {@inheritdoc}
164
     *
165
     * @throws NotNormalizableValueException
166
     */
167
    public function denormalize(mixed $data, string $class, ?string $format = null, array $context = []): mixed
168
    {
169
        // Avoid issues with proxies if we populated the object
UNCOV
170
        if (!isset($context[self::OBJECT_TO_POPULATE]) && isset($data['data']['id'])) {
7✔
171
            if (true !== ($context['api_allow_update'] ?? true)) {
×
172
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
×
173
            }
174

175
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri(
×
176
                $data['data']['id'],
×
177
                $context + ['fetch_data' => false]
×
178
            );
×
179
        }
180

181
        // Merge attributes and relationships, into format expected by the parent normalizer
UNCOV
182
        $dataToDenormalize = array_merge(
7✔
UNCOV
183
            $data['data']['attributes'] ?? [],
7✔
UNCOV
184
            $data['data']['relationships'] ?? []
7✔
UNCOV
185
        );
7✔
186

UNCOV
187
        return parent::denormalize(
7✔
UNCOV
188
            $dataToDenormalize,
7✔
UNCOV
189
            $class,
7✔
UNCOV
190
            $format,
7✔
UNCOV
191
            $context
7✔
UNCOV
192
        );
7✔
193
    }
194

195
    /**
196
     * {@inheritdoc}
197
     */
198
    protected function getAttributes(object $object, ?string $format = null, array $context = []): array
199
    {
200
        return $this->getComponents($object, $format, $context)['attributes'];
84✔
201
    }
202

203
    /**
204
     * {@inheritdoc}
205
     */
206
    protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []): void
207
    {
UNCOV
208
        parent::setAttributeValue($object, $attribute, \is_array($value) && \array_key_exists('data', $value) ? $value['data'] : $value, $format, $context);
7✔
209
    }
210

211
    /**
212
     * {@inheritdoc}
213
     *
214
     * @see http://jsonapi.org/format/#document-resource-object-linkage
215
     *
216
     * @throws RuntimeException
217
     * @throws UnexpectedValueException
218
     */
219
    protected function denormalizeRelation(string $attributeName, ApiProperty $propertyMetadata, string $className, mixed $value, ?string $format, array $context): ?object
220
    {
UNCOV
221
        if (!\is_array($value) || !isset($value['id'], $value['type'])) {
4✔
222
            throw new UnexpectedValueException('Only resource linkage supported currently, see: http://jsonapi.org/format/#document-resource-object-linkage.');
×
223
        }
224

225
        try {
UNCOV
226
            return $this->iriConverter->getResourceFromIri($value['id'], $context + ['fetch_data' => true]);
4✔
227
        } catch (ItemNotFoundException $e) {
×
228
            if (!isset($context['not_normalizable_value_exceptions'])) {
×
229
                throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
×
230
            }
231
            $context['not_normalizable_value_exceptions'][] = NotNormalizableValueException::createForUnexpectedDataType(
×
232
                $e->getMessage(),
×
233
                $value,
×
234
                [$className],
×
235
                $context['deserialization_path'] ?? null,
×
236
                true,
×
237
                $e->getCode(),
×
238
                $e
×
239
            );
×
240

241
            return null;
×
242
        }
243
    }
244

245
    /**
246
     * {@inheritdoc}
247
     *
248
     * @see http://jsonapi.org/format/#document-resource-object-linkage
249
     */
250
    protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $relatedObject, string $resourceClass, ?string $format, array $context): \ArrayObject|array|string|null
251
    {
UNCOV
252
        if (null !== $relatedObject) {
43✔
UNCOV
253
            $iri = $this->iriConverter->getIriFromResource($relatedObject);
31✔
UNCOV
254
            $context['iri'] = $iri;
31✔
255

UNCOV
256
            if (!$this->tagCollector && isset($context['resources'])) {
31✔
257
                $context['resources'][$iri] = $iri;
×
258
            }
259
        }
260

UNCOV
261
        if (null === $relatedObject || isset($context['api_included'])) {
43✔
UNCOV
262
            if (!$this->serializer instanceof NormalizerInterface) {
31✔
263
                throw new LogicException(\sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
×
264
            }
265

UNCOV
266
            $normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $context);
31✔
UNCOV
267
            if (!\is_string($normalizedRelatedObject) && !\is_array($normalizedRelatedObject) && !$normalizedRelatedObject instanceof \ArrayObject && null !== $normalizedRelatedObject) {
31✔
268
                throw new UnexpectedValueException('Expected normalized relation to be an IRI, array, \ArrayObject or null');
×
269
            }
270

UNCOV
271
            return $normalizedRelatedObject;
31✔
272
        }
273

UNCOV
274
        $context['data'] = [
31✔
UNCOV
275
            'data' => [
31✔
UNCOV
276
                'type' => $this->getResourceShortName($resourceClass),
31✔
UNCOV
277
                'id' => $iri,
31✔
UNCOV
278
            ],
31✔
UNCOV
279
        ];
31✔
280

UNCOV
281
        $context['iri'] = $iri;
31✔
UNCOV
282
        $context['object'] = $relatedObject;
31✔
UNCOV
283
        unset($context['property_metadata']);
31✔
UNCOV
284
        unset($context['api_attribute']);
31✔
285

UNCOV
286
        if ($this->tagCollector) {
31✔
UNCOV
287
            $this->tagCollector->collect($context);
31✔
288
        }
289

UNCOV
290
        return $context['data'];
31✔
291
    }
292

293
    /**
294
     * {@inheritdoc}
295
     */
296
    protected function isAllowedAttribute(object|string $classOrObject, string $attribute, ?string $format = null, array $context = []): bool
297
    {
298
        return preg_match('/^\\w[-\\w_]*$/', $attribute) && parent::isAllowedAttribute($classOrObject, $attribute, $format, $context);
84✔
299
    }
300

301
    /**
302
     * Gets JSON API components of the resource: attributes, relationships, meta and links.
303
     */
304
    private function getComponents(object $object, ?string $format, array $context): array
305
    {
306
        $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key'];
84✔
307

308
        if (isset($this->componentsCache[$cacheKey])) {
84✔
309
            return $this->componentsCache[$cacheKey];
84✔
310
        }
311

312
        $attributes = parent::getAttributes($object, $format, $context);
84✔
313

314
        $options = $this->getFactoryOptions($context);
84✔
315

316
        $components = [
84✔
317
            'links' => [],
84✔
318
            'relationships' => [],
84✔
319
            'attributes' => [],
84✔
320
            'meta' => [],
84✔
321
        ];
84✔
322

323
        foreach ($attributes as $attribute) {
84✔
324
            $propertyMetadata = $this
82✔
325
                ->propertyMetadataFactory
82✔
326
                ->create($context['resource_class'], $attribute, $options);
82✔
327

328
            // prevent declaring $attribute as attribute if it's already declared as relationship
329
            $isRelationship = false;
82✔
330

331
            if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
82✔
332
                $types = $propertyMetadata->getBuiltinTypes() ?? [];
×
333

334
                foreach ($types as $type) {
×
335
                    $isOne = $isMany = false;
×
336

337
                    if ($type->isCollection()) {
×
338
                        $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
×
339
                        $isMany = $collectionValueType && ($className = $collectionValueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
×
340
                    } else {
341
                        $isOne = ($className = $type->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
×
342
                    }
343

344
                    if (!isset($className) || !$isOne && !$isMany) {
×
345
                        // don't declare it as an attribute too quick: maybe the next type is a valid resource
346
                        continue;
×
347
                    }
348

349
                    $relation = [
×
350
                        'name' => $attribute,
×
351
                        'type' => $this->getResourceShortName($className),
×
352
                        'cardinality' => $isOne ? 'one' : 'many',
×
353
                    ];
×
354

355
                    // if we specify the uriTemplate, generates its value for link definition
356
                    // @see ApiPlatform\Serializer\AbstractItemNormalizer:getAttributeValue logic for intentional duplicate content
357
                    if ($itemUriTemplate = $propertyMetadata->getUriTemplate()) {
×
358
                        $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
×
359
                        $resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
×
360
                        $childContext = $this->createChildContext($context, $attribute, $format);
×
361
                        unset($childContext['iri'], $childContext['uri_variables'], $childContext['resource_class'], $childContext['operation']);
×
362

363
                        $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation(
×
364
                            operationName: $itemUriTemplate,
×
365
                            httpOperation: true
×
366
                        );
×
367

368
                        $components['links'][$attribute] = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
×
369
                    }
370

371
                    $components['relationships'][] = $relation;
×
372
                    $isRelationship = true;
×
373
                }
374
            } else {
375
                if ($type = $propertyMetadata->getNativeType()) {
82✔
376
                    /** @var class-string|null $className */
377
                    $className = null;
78✔
378

379
                    $typeIsResourceClass = function (Type $type) use (&$typeIsResourceClass, &$className): bool {
78✔
380
                        return match (true) {
381
                            $type instanceof ObjectType => $this->resourceClassResolver->isResourceClass($className = $type->getClassName()),
78✔
382
                            $type instanceof WrappingTypeInterface => $type->wrappedTypeIsSatisfiedBy($typeIsResourceClass),
76✔
383
                            $type instanceof CompositeTypeInterface => $type->composedTypesAreSatisfiedBy($typeIsResourceClass),
76✔
384
                            default => false,
78✔
385
                        };
386
                    };
78✔
387

388
                    $collectionValueIsResourceClass = function (Type $type) use ($typeIsResourceClass, &$collectionValueIsResourceClass): bool {
78✔
389
                        return match (true) {
390
                            $type instanceof CollectionType => $type->getCollectionValueType()->isSatisfiedBy($typeIsResourceClass),
78✔
391
                            $type instanceof WrappingTypeInterface => $type->wrappedTypeIsSatisfiedBy($collectionValueIsResourceClass),
78✔
392
                            $type instanceof CompositeTypeInterface => $type->composedTypesAreSatisfiedBy($collectionValueIsResourceClass),
78✔
393
                            default => false,
78✔
394
                        };
395
                    };
78✔
396

397
                    foreach ($type instanceof CompositeTypeInterface ? $type->getTypes() : [$type] as $t) {
78✔
398
                        $isOne = $isMany = false;
78✔
399

400
                        if ($t->isSatisfiedBy($collectionValueIsResourceClass)) {
78✔
401
                            $isMany = true;
37✔
402
                        } elseif ($t->isSatisfiedBy($typeIsResourceClass)) {
78✔
UNCOV
403
                            $isOne = true;
43✔
404
                        }
405

406
                        if (!$className || (!$isOne && !$isMany)) {
78✔
407
                            // don't declare it as an attribute too quick: maybe the next type is a valid resource
408
                            continue;
76✔
409
                        }
410

411
                        $relation = [
47✔
412
                            'name' => $attribute,
47✔
413
                            'type' => $this->getResourceShortName($className),
47✔
414
                            'cardinality' => $isOne ? 'one' : 'many',
47✔
415
                        ];
47✔
416

417
                        // if we specify the uriTemplate, generates its value for link definition
418
                        // @see ApiPlatform\Serializer\AbstractItemNormalizer:getAttributeValue logic for intentional duplicate content
419
                        if ($itemUriTemplate = $propertyMetadata->getUriTemplate()) {
47✔
UNCOV
420
                            $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
1✔
UNCOV
421
                            $resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
1✔
UNCOV
422
                            $childContext = $this->createChildContext($context, $attribute, $format);
1✔
UNCOV
423
                            unset($childContext['iri'], $childContext['uri_variables'], $childContext['resource_class'], $childContext['operation']);
1✔
424

UNCOV
425
                            $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation(
1✔
UNCOV
426
                                operationName: $itemUriTemplate,
1✔
UNCOV
427
                                httpOperation: true
1✔
UNCOV
428
                            );
1✔
429

UNCOV
430
                            $components['links'][$attribute] = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
1✔
431
                        }
432

433
                        $components['relationships'][] = $relation;
47✔
434
                        $isRelationship = true;
47✔
435
                    }
436
                }
437
            }
438

439
            // if all types are not relationships, declare it as an attribute
440
            if (!$isRelationship) {
82✔
441
                $components['attributes'][] = $attribute;
80✔
442
            }
443
        }
444

445
        if (false !== $context['cache_key']) {
84✔
446
            $this->componentsCache[$cacheKey] = $components;
84✔
447
        }
448

449
        return $components;
84✔
450
    }
451

452
    /**
453
     * Populates relationships keys.
454
     *
455
     * @throws UnexpectedValueException
456
     */
457
    private function getPopulatedRelations(object $object, ?string $format, array $context, array $relationships): array
458
    {
459
        $data = [];
84✔
460

461
        if (!isset($context['resource_class'])) {
84✔
462
            return $data;
×
463
        }
464

465
        unset($context['api_included']);
84✔
466
        foreach ($relationships as $relationshipDataArray) {
84✔
467
            $relationshipName = $relationshipDataArray['name'];
47✔
468

469
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);
47✔
470

471
            if ($this->nameConverter) {
47✔
472
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context);
47✔
473
            }
474

475
            $data[$relationshipName] = [
47✔
476
                'data' => [],
47✔
477
            ];
47✔
478

479
            if (!$attributeValue) {
47✔
480
                continue;
35✔
481
            }
482

483
            // Many to one relationship
UNCOV
484
            if ('one' === $relationshipDataArray['cardinality']) {
31✔
UNCOV
485
                unset($attributeValue['data']['attributes']);
30✔
UNCOV
486
                $data[$relationshipName] = $attributeValue;
30✔
487

UNCOV
488
                continue;
30✔
489
            }
490

491
            // Many to many relationship
UNCOV
492
            foreach ($attributeValue as $attributeValueElement) {
11✔
UNCOV
493
                if (!isset($attributeValueElement['data'])) {
11✔
494
                    throw new UnexpectedValueException(\sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName));
×
495
                }
UNCOV
496
                unset($attributeValueElement['data']['attributes']);
11✔
UNCOV
497
                $data[$relationshipName]['data'][] = $attributeValueElement['data'];
11✔
498
            }
499
        }
500

501
        return $data;
84✔
502
    }
503

504
    /**
505
     * Populates included keys.
506
     */
507
    private function getRelatedResources(object $object, ?string $format, array $context, array $relationships): array
508
    {
509
        if (!isset($context['api_included'])) {
84✔
510
            return [];
68✔
511
        }
512

UNCOV
513
        $included = [];
16✔
UNCOV
514
        foreach ($relationships as $relationshipDataArray) {
16✔
UNCOV
515
            $relationshipName = $relationshipDataArray['name'];
16✔
516

UNCOV
517
            if (!$this->shouldIncludeRelation($relationshipName, $context)) {
16✔
UNCOV
518
                continue;
13✔
519
            }
520

UNCOV
521
            $relationContext = $context;
15✔
UNCOV
522
            $relationContext['api_included'] = $this->getIncludedNestedResources($relationshipName, $context);
15✔
523

UNCOV
524
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $relationContext);
15✔
525

UNCOV
526
            if (!$attributeValue) {
15✔
527
                continue;
×
528
            }
529

530
            // Many to many relationship
UNCOV
531
            $attributeValues = $attributeValue;
15✔
532
            // Many to one relationship
UNCOV
533
            if ('one' === $relationshipDataArray['cardinality']) {
15✔
UNCOV
534
                $attributeValues = [$attributeValue];
13✔
535
            }
536

UNCOV
537
            foreach ($attributeValues as $attributeValueElement) {
15✔
UNCOV
538
                if (isset($attributeValueElement['data'])) {
15✔
UNCOV
539
                    $this->addIncluded($attributeValueElement['data'], $included, $context);
15✔
UNCOV
540
                    if (isset($attributeValueElement['included']) && \is_array($attributeValueElement['included'])) {
15✔
UNCOV
541
                        foreach ($attributeValueElement['included'] as $include) {
5✔
UNCOV
542
                            $this->addIncluded($include, $included, $context);
5✔
543
                        }
544
                    }
545
                }
546
            }
547
        }
548

UNCOV
549
        return $included;
16✔
550
    }
551

552
    /**
553
     * Add data to included array if it's not already included.
554
     */
555
    private function addIncluded(array $data, array &$included, array &$context): void
556
    {
UNCOV
557
        if (isset($data['id']) && !\in_array($data['id'], $context['api_included_resources'], true)) {
15✔
UNCOV
558
            $included[] = $data;
15✔
559
            // Track already included resources
UNCOV
560
            $context['api_included_resources'][] = $data['id'];
15✔
561
        }
562
    }
563

564
    /**
565
     * Figures out if the relationship is in the api_included hash or has included nested resources (path).
566
     */
567
    private function shouldIncludeRelation(string $relationshipName, array $context): bool
568
    {
UNCOV
569
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
16✔
570

UNCOV
571
        return \in_array($normalizedName, $context['api_included'], true) || \count($this->getIncludedNestedResources($relationshipName, $context)) > 0;
16✔
572
    }
573

574
    /**
575
     * Returns the names of the nested resources from a path relationship.
576
     */
577
    private function getIncludedNestedResources(string $relationshipName, array $context): array
578
    {
UNCOV
579
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
16✔
580

UNCOV
581
        $filtered = array_filter($context['api_included'] ?? [], static fn (string $included): bool => str_starts_with($included, $normalizedName.'.'));
16✔
582

UNCOV
583
        return array_map(static fn (string $nested): string => substr($nested, strpos($nested, '.') + 1), $filtered);
16✔
584
    }
585

586
    // TODO: this code is similar to the one used in JsonLd
587
    private function getResourceShortName(string $resourceClass): string
588
    {
589
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
84✔
590
            $resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
82✔
591

592
            return $resourceMetadata->getOperation()->getShortName();
82✔
593
        }
594

595
        return (new \ReflectionClass($resourceClass))->getShortName();
4✔
596
    }
597
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc