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

api-platform / core / 15023181448

14 May 2025 02:19PM UTC coverage: 0.0% (-8.4%) from 8.418%
15023181448

Pull #7139

github

web-flow
Merge 9f45709da into 1862d03b7
Pull Request #7139: refactor(symfony): remove obsolete option `validator.query-parameter-validation`

0 of 4 new or added lines in 1 file covered. (0.0%)

11266 existing lines in 366 files now uncovered.

0 of 50828 relevant lines covered (0.0%)

0.0 hits per line

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

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

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

75
    public function getSupportedTypes($format): array
76
    {
UNCOV
77
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
×
78
    }
79

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

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

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

UNCOV
99
        $context = $this->initContext($resourceClass, $context);
×
100

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

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

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

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

120
        // Do not include primary resources
UNCOV
121
        $context['api_included_resources'] = [$context['iri']];
×
122

UNCOV
123
        $includedResourcesData = $this->getRelatedResources($object, $format, $context, $allRelationshipsData);
×
124

UNCOV
125
        $resourceData = [
×
UNCOV
126
            'id' => $context['iri'],
×
UNCOV
127
            'type' => $this->getResourceShortName($resourceClass),
×
UNCOV
128
        ];
×
129

UNCOV
130
        if ($data) {
×
UNCOV
131
            $resourceData['attributes'] = $data;
×
132
        }
133

UNCOV
134
        if ($relationshipsData) {
×
UNCOV
135
            $resourceData['relationships'] = $relationshipsData;
×
136
        }
137

UNCOV
138
        $document = [];
×
139

UNCOV
140
        if ($links) {
×
141
            $document['links'] = $links;
×
142
        }
143

UNCOV
144
        $document['data'] = $resourceData;
×
145

UNCOV
146
        if ($includedResourcesData) {
×
147
            $document['included'] = $includedResourcesData;
×
148
        }
149

UNCOV
150
        return $document;
×
151
    }
152

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

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

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

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

186
        return parent::denormalize(
×
187
            $dataToDenormalize,
×
188
            $class,
×
189
            $format,
×
190
            $context
×
191
        );
×
192
    }
193

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

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

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

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

240
            return null;
×
241
        }
242
    }
243

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

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

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

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

270
            return $normalizedRelatedObject;
×
271
        }
272

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

280
        $context['iri'] = $iri;
×
281
        $context['object'] = $relatedObject;
×
282
        unset($context['property_metadata']);
×
283
        unset($context['api_attribute']);
×
284

285
        if ($this->tagCollector) {
×
286
            $this->tagCollector->collect($context);
×
287
        }
288

289
        return $context['data'];
×
290
    }
291

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

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

UNCOV
307
        if (isset($this->componentsCache[$cacheKey])) {
×
UNCOV
308
            return $this->componentsCache[$cacheKey];
×
309
        }
310

UNCOV
311
        $attributes = parent::getAttributes($object, $format, $context);
×
312

UNCOV
313
        $options = $this->getFactoryOptions($context);
×
314

UNCOV
315
        $components = [
×
UNCOV
316
            'links' => [],
×
UNCOV
317
            'relationships' => [],
×
UNCOV
318
            'attributes' => [],
×
UNCOV
319
            'meta' => [],
×
UNCOV
320
        ];
×
321

UNCOV
322
        foreach ($attributes as $attribute) {
×
UNCOV
323
            $propertyMetadata = $this
×
UNCOV
324
                ->propertyMetadataFactory
×
UNCOV
325
                ->create($context['resource_class'], $attribute, $options);
×
326

327
            // prevent declaring $attribute as attribute if it's already declared as relationship
UNCOV
328
            $isRelationship = false;
×
329

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

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

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

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

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

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

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

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

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

UNCOV
378
                    $typeIsResourceClass = function (Type $type) use (&$className): bool {
×
UNCOV
379
                        return $type instanceof ObjectType && $this->resourceClassResolver->isResourceClass($className = $type->getClassName());
×
UNCOV
380
                    };
×
381

UNCOV
382
                    foreach ($type instanceof CompositeTypeInterface ? $type->getTypes() : [$type] as $t) {
×
UNCOV
383
                        $isOne = $isMany = false;
×
384

UNCOV
385
                        if (TypeHelper::getCollectionValueType($t)?->isSatisfiedBy($typeIsResourceClass)) {
×
UNCOV
386
                            $isMany = true;
×
UNCOV
387
                        } elseif ($t->isSatisfiedBy($typeIsResourceClass)) {
×
388
                            $isOne = true;
×
389
                        }
390

UNCOV
391
                        if (!$className || (!$isOne && !$isMany)) {
×
392
                            // don't declare it as an attribute too quick: maybe the next type is a valid resource
UNCOV
393
                            continue;
×
394
                        }
395

UNCOV
396
                        $relation = [
×
UNCOV
397
                            'name' => $attribute,
×
UNCOV
398
                            'type' => $this->getResourceShortName($className),
×
UNCOV
399
                            'cardinality' => $isOne ? 'one' : 'many',
×
UNCOV
400
                        ];
×
401

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

410
                            $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation(
×
411
                                operationName: $itemUriTemplate,
×
412
                                httpOperation: true
×
413
                            );
×
414

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

UNCOV
418
                        $components['relationships'][] = $relation;
×
UNCOV
419
                        $isRelationship = true;
×
420
                    }
421
                }
422
            }
423

424
            // if all types are not relationships, declare it as an attribute
UNCOV
425
            if (!$isRelationship) {
×
UNCOV
426
                $components['attributes'][] = $attribute;
×
427
            }
428
        }
429

UNCOV
430
        if (false !== $context['cache_key']) {
×
UNCOV
431
            $this->componentsCache[$cacheKey] = $components;
×
432
        }
433

UNCOV
434
        return $components;
×
435
    }
436

437
    /**
438
     * Populates relationships keys.
439
     *
440
     * @throws UnexpectedValueException
441
     */
442
    private function getPopulatedRelations(object $object, ?string $format, array $context, array $relationships): array
443
    {
UNCOV
444
        $data = [];
×
445

UNCOV
446
        if (!isset($context['resource_class'])) {
×
447
            return $data;
×
448
        }
449

UNCOV
450
        unset($context['api_included']);
×
UNCOV
451
        foreach ($relationships as $relationshipDataArray) {
×
UNCOV
452
            $relationshipName = $relationshipDataArray['name'];
×
453

UNCOV
454
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);
×
455

UNCOV
456
            if ($this->nameConverter) {
×
UNCOV
457
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context);
×
458
            }
459

UNCOV
460
            $data[$relationshipName] = [
×
UNCOV
461
                'data' => [],
×
UNCOV
462
            ];
×
463

UNCOV
464
            if (!$attributeValue) {
×
UNCOV
465
                continue;
×
466
            }
467

468
            // Many to one relationship
469
            if ('one' === $relationshipDataArray['cardinality']) {
×
470
                unset($attributeValue['data']['attributes']);
×
471
                $data[$relationshipName] = $attributeValue;
×
472

473
                continue;
×
474
            }
475

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

UNCOV
486
        return $data;
×
487
    }
488

489
    /**
490
     * Populates included keys.
491
     */
492
    private function getRelatedResources(object $object, ?string $format, array $context, array $relationships): array
493
    {
UNCOV
494
        if (!isset($context['api_included'])) {
×
UNCOV
495
            return [];
×
496
        }
497

498
        $included = [];
×
499
        foreach ($relationships as $relationshipDataArray) {
×
500
            $relationshipName = $relationshipDataArray['name'];
×
501

502
            if (!$this->shouldIncludeRelation($relationshipName, $context)) {
×
503
                continue;
×
504
            }
505

506
            $relationContext = $context;
×
507
            $relationContext['api_included'] = $this->getIncludedNestedResources($relationshipName, $context);
×
508

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

511
            if (!$attributeValue) {
×
512
                continue;
×
513
            }
514

515
            // Many to many relationship
516
            $attributeValues = $attributeValue;
×
517
            // Many to one relationship
518
            if ('one' === $relationshipDataArray['cardinality']) {
×
519
                $attributeValues = [$attributeValue];
×
520
            }
521

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

534
        return $included;
×
535
    }
536

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

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

556
        return \in_array($normalizedName, $context['api_included'], true) || \count($this->getIncludedNestedResources($relationshipName, $context)) > 0;
×
557
    }
558

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

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

568
        return array_map(static fn (string $nested): string => substr($nested, strpos($nested, '.') + 1), $filtered);
×
569
    }
570

571
    // TODO: this code is similar to the one used in JsonLd
572
    private function getResourceShortName(string $resourceClass): string
573
    {
UNCOV
574
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
×
UNCOV
575
            $resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
×
576

UNCOV
577
            return $resourceMetadata->getOperation()->getShortName();
×
578
        }
579

UNCOV
580
        return (new \ReflectionClass($resourceClass))->getShortName();
×
581
    }
582
}
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