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

api-platform / core / 18089937549

29 Sep 2025 07:56AM UTC coverage: 21.764% (-0.3%) from 22.093%
18089937549

Pull #7416

github

web-flow
Merge 061bcc790 into abe0438be
Pull Request #7416: fix(laravel): serializer attributes on Eloquent methods

0 of 151 new or added lines in 11 files covered. (0.0%)

5028 existing lines in 173 files now uncovered.

11889 of 54626 relevant lines covered (21.76%)

25.32 hits per line

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

42.26
/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\Metadata\Util\TypeHelper;
27
use ApiPlatform\Serializer\AbstractItemNormalizer;
28
use ApiPlatform\Serializer\CacheKeyTrait;
29
use ApiPlatform\Serializer\ContextTrait;
30
use ApiPlatform\Serializer\TagCollectorInterface;
31
use Symfony\Component\ErrorHandler\Exception\FlattenException;
32
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
33
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
34
use Symfony\Component\Serializer\Exception\LogicException;
35
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
36
use Symfony\Component\Serializer\Exception\RuntimeException;
37
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
38
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
39
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
40
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
41
use Symfony\Component\TypeInfo\Type;
42
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
43
use Symfony\Component\TypeInfo\Type\ObjectType;
44

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

58
    public const FORMAT = 'jsonapi';
59

60
    private array $componentsCache = [];
61

62
    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)
63
    {
64
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
654✔
65
    }
66

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

75
    /**
76
     * @param string|null $format
77
     */
78
    public function getSupportedTypes($format): array
79
    {
80
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
556✔
81
    }
82

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

93
        $previousResourceClass = $context['resource_class'] ?? null;
30✔
94
        if ($this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
30✔
95
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
28✔
96
        }
97

98
        if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
30✔
UNCOV
99
            $context['item_uri_template'] = $operation->getItemUriTemplate();
×
100
        }
101

102
        $context = $this->initContext($resourceClass, $context);
30✔
103

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

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

113
        $data = parent::normalize($object, $format, $context);
30✔
114
        if (!\is_array($data)) {
30✔
UNCOV
115
            return $data;
×
116
        }
117

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

123
        // Do not include primary resources
124
        $context['api_included_resources'] = [$context['iri']];
30✔
125

126
        $includedResourcesData = $this->getRelatedResources($object, $format, $context, $allRelationshipsData);
30✔
127

128
        $resourceData = [
30✔
129
            'id' => $context['iri'],
30✔
130
            'type' => $this->getResourceShortName($resourceClass),
30✔
131
        ];
30✔
132

133
        if ($data) {
30✔
134
            $resourceData['attributes'] = $data;
28✔
135
        }
136

137
        if ($relationshipsData) {
30✔
138
            $resourceData['relationships'] = $relationshipsData;
2✔
139
        }
140

141
        $document = [];
30✔
142

143
        if ($links) {
30✔
UNCOV
144
            $document['links'] = $links;
×
145
        }
146

147
        $document['data'] = $resourceData;
30✔
148

149
        if ($includedResourcesData) {
30✔
UNCOV
150
            $document['included'] = $includedResourcesData;
×
151
        }
152

153
        return $document;
30✔
154
    }
155

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

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

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

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

UNCOV
189
        return parent::denormalize(
×
UNCOV
190
            $dataToDenormalize,
×
UNCOV
191
            $class,
×
UNCOV
192
            $format,
×
UNCOV
193
            $context
×
UNCOV
194
        );
×
195
    }
196

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

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

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

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

UNCOV
243
            return null;
×
244
        }
245
    }
246

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

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

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

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

273
            return $normalizedRelatedObject;
×
274
        }
275

276
        $context['data'] = [
×
277
            'data' => [
×
278
                'type' => $this->getResourceShortName($resourceClass),
×
UNCOV
279
                'id' => $iri,
×
280
            ],
×
281
        ];
×
282

UNCOV
283
        $context['iri'] = $iri;
×
284
        $context['object'] = $relatedObject;
×
UNCOV
285
        unset($context['property_metadata']);
×
UNCOV
286
        unset($context['api_attribute']);
×
287

UNCOV
288
        if ($this->tagCollector) {
×
UNCOV
289
            $this->tagCollector->collect($context);
×
290
        }
291

UNCOV
292
        return $context['data'];
×
293
    }
294

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

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

310
        if (isset($this->componentsCache[$cacheKey])) {
30✔
311
            return $this->componentsCache[$cacheKey];
30✔
312
        }
313

314
        $attributes = parent::getAttributes($object, $format, $context);
30✔
315

316
        $options = $this->getFactoryOptions($context);
30✔
317

318
        $components = [
30✔
319
            'links' => [],
30✔
320
            'relationships' => [],
30✔
321
            'attributes' => [],
30✔
322
            'meta' => [],
30✔
323
        ];
30✔
324

325
        foreach ($attributes as $attribute) {
30✔
326
            $propertyMetadata = $this
28✔
327
                ->propertyMetadataFactory
28✔
328
                ->create($context['resource_class'], $attribute, $options);
28✔
329

330
            // prevent declaring $attribute as attribute if it's already declared as relationship
331
            $isRelationship = false;
28✔
332

333
            if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
28✔
UNCOV
334
                $types = $propertyMetadata->getBuiltinTypes() ?? [];
×
335

UNCOV
336
                foreach ($types as $type) {
×
UNCOV
337
                    $isOne = $isMany = false;
×
338

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

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

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

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

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

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

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

381
                    $typeIsResourceClass = function (Type $type) use (&$className): bool {
28✔
382
                        return $type instanceof ObjectType && $this->resourceClassResolver->isResourceClass($className = $type->getClassName());
28✔
383
                    };
28✔
384

385
                    foreach ($type instanceof CompositeTypeInterface ? $type->getTypes() : [$type] as $t) {
28✔
386
                        $isOne = $isMany = false;
28✔
387

388
                        if (TypeHelper::getCollectionValueType($t)?->isSatisfiedBy($typeIsResourceClass)) {
28✔
389
                            $isMany = true;
2✔
390
                        } elseif ($t->isSatisfiedBy($typeIsResourceClass)) {
28✔
391
                            $isOne = true;
×
392
                        }
393

394
                        if (!$className || (!$isOne && !$isMany)) {
28✔
395
                            // don't declare it as an attribute too quick: maybe the next type is a valid resource
396
                            continue;
28✔
397
                        }
398

399
                        $relation = [
2✔
400
                            'name' => $attribute,
2✔
401
                            'type' => $this->getResourceShortName($className),
2✔
402
                            'cardinality' => $isOne ? 'one' : 'many',
2✔
403
                        ];
2✔
404

405
                        // if we specify the uriTemplate, generates its value for link definition
406
                        // @see ApiPlatform\Serializer\AbstractItemNormalizer:getAttributeValue logic for intentional duplicate content
407
                        if ($itemUriTemplate = $propertyMetadata->getUriTemplate()) {
2✔
UNCOV
408
                            $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
×
UNCOV
409
                            $resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
×
UNCOV
410
                            $childContext = $this->createChildContext($context, $attribute, $format);
×
UNCOV
411
                            unset($childContext['iri'], $childContext['uri_variables'], $childContext['resource_class'], $childContext['operation']);
×
412

413
                            $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation(
×
414
                                operationName: $itemUriTemplate,
×
415
                                httpOperation: true
×
UNCOV
416
                            );
×
417

UNCOV
418
                            $components['links'][$attribute] = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
×
419
                        }
420

421
                        $components['relationships'][] = $relation;
2✔
422
                        $isRelationship = true;
2✔
423
                    }
424
                }
425
            }
426

427
            // if all types are not relationships, declare it as an attribute
428
            if (!$isRelationship) {
28✔
429
                $components['attributes'][] = $attribute;
28✔
430
            }
431
        }
432

433
        if (false !== $context['cache_key']) {
30✔
434
            $this->componentsCache[$cacheKey] = $components;
30✔
435
        }
436

437
        return $components;
30✔
438
    }
439

440
    /**
441
     * Populates relationships keys.
442
     *
443
     * @throws UnexpectedValueException
444
     */
445
    private function getPopulatedRelations(object $object, ?string $format, array $context, array $relationships): array
446
    {
447
        $data = [];
30✔
448

449
        if (!isset($context['resource_class'])) {
30✔
450
            return $data;
×
451
        }
452

453
        unset($context['api_included']);
30✔
454
        foreach ($relationships as $relationshipDataArray) {
30✔
455
            $relationshipName = $relationshipDataArray['name'];
2✔
456

457
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);
2✔
458

459
            if ($this->nameConverter) {
2✔
460
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context);
2✔
461
            }
462

463
            $data[$relationshipName] = [
2✔
464
                'data' => [],
2✔
465
            ];
2✔
466

467
            if (!$attributeValue) {
2✔
468
                continue;
2✔
469
            }
470

471
            // Many to one relationship
UNCOV
472
            if ('one' === $relationshipDataArray['cardinality']) {
×
UNCOV
473
                unset($attributeValue['data']['attributes']);
×
UNCOV
474
                $data[$relationshipName] = $attributeValue;
×
475

UNCOV
476
                continue;
×
477
            }
478

479
            // Many to many relationship
UNCOV
480
            foreach ($attributeValue as $attributeValueElement) {
×
UNCOV
481
                if (!isset($attributeValueElement['data'])) {
×
UNCOV
482
                    throw new UnexpectedValueException(\sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName));
×
483
                }
UNCOV
484
                unset($attributeValueElement['data']['attributes']);
×
UNCOV
485
                $data[$relationshipName]['data'][] = $attributeValueElement['data'];
×
486
            }
487
        }
488

489
        return $data;
30✔
490
    }
491

492
    /**
493
     * Populates included keys.
494
     */
495
    private function getRelatedResources(object $object, ?string $format, array $context, array $relationships): array
496
    {
497
        if (!isset($context['api_included'])) {
30✔
498
            return [];
30✔
499
        }
500

UNCOV
501
        $included = [];
×
UNCOV
502
        foreach ($relationships as $relationshipDataArray) {
×
UNCOV
503
            $relationshipName = $relationshipDataArray['name'];
×
504

UNCOV
505
            if (!$this->shouldIncludeRelation($relationshipName, $context)) {
×
UNCOV
506
                continue;
×
507
            }
508

UNCOV
509
            $relationContext = $context;
×
510
            $relationContext['api_included'] = $this->getIncludedNestedResources($relationshipName, $context);
×
511

512
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $relationContext);
×
513

UNCOV
514
            if (!$attributeValue) {
×
UNCOV
515
                continue;
×
516
            }
517

518
            // Many to many relationship
UNCOV
519
            $attributeValues = $attributeValue;
×
520
            // Many to one relationship
UNCOV
521
            if ('one' === $relationshipDataArray['cardinality']) {
×
UNCOV
522
                $attributeValues = [$attributeValue];
×
523
            }
524

UNCOV
525
            foreach ($attributeValues as $attributeValueElement) {
×
UNCOV
526
                if (isset($attributeValueElement['data'])) {
×
UNCOV
527
                    $this->addIncluded($attributeValueElement['data'], $included, $context);
×
UNCOV
528
                    if (isset($attributeValueElement['included']) && \is_array($attributeValueElement['included'])) {
×
UNCOV
529
                        foreach ($attributeValueElement['included'] as $include) {
×
UNCOV
530
                            $this->addIncluded($include, $included, $context);
×
531
                        }
532
                    }
533
                }
534
            }
535
        }
536

UNCOV
537
        return $included;
×
538
    }
539

540
    /**
541
     * Add data to included array if it's not already included.
542
     */
543
    private function addIncluded(array $data, array &$included, array &$context): void
544
    {
UNCOV
545
        if (isset($data['id']) && !\in_array($data['id'], $context['api_included_resources'], true)) {
×
UNCOV
546
            $included[] = $data;
×
547
            // Track already included resources
UNCOV
548
            $context['api_included_resources'][] = $data['id'];
×
549
        }
550
    }
551

552
    /**
553
     * Figures out if the relationship is in the api_included hash or has included nested resources (path).
554
     */
555
    private function shouldIncludeRelation(string $relationshipName, array $context): bool
556
    {
UNCOV
557
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
×
558

UNCOV
559
        return \in_array($normalizedName, $context['api_included'], true) || \count($this->getIncludedNestedResources($relationshipName, $context)) > 0;
×
560
    }
561

562
    /**
563
     * Returns the names of the nested resources from a path relationship.
564
     */
565
    private function getIncludedNestedResources(string $relationshipName, array $context): array
566
    {
UNCOV
567
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
×
568

UNCOV
569
        $filtered = array_filter($context['api_included'] ?? [], static fn (string $included): bool => str_starts_with($included, $normalizedName.'.'));
×
570

UNCOV
571
        return array_map(static fn (string $nested): string => substr($nested, strpos($nested, '.') + 1), $filtered);
×
572
    }
573

574
    // TODO: this code is similar to the one used in JsonLd
575
    private function getResourceShortName(string $resourceClass): string
576
    {
577
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
30✔
578
            $resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
30✔
579

580
            return $resourceMetadata->getOperation()->getShortName();
30✔
581
        }
582

583
        return (new \ReflectionClass($resourceClass))->getShortName();
2✔
584
    }
585
}
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