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

api-platform / core / 13722973459

07 Mar 2025 02:39PM UTC coverage: 8.518% (-0.001%) from 8.519%
13722973459

push

github

web-flow
fix(laravel): query extensions with item operations (#7001)

0 of 13 new or added lines in 4 files covered. (0.0%)

2480 existing lines in 163 files now uncovered.

13377 of 157041 relevant lines covered (8.52%)

22.89 hits per line

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

99.07
/src/Hydra/Serializer/DocumentationNormalizer.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\Hydra\Serializer;
15

16
use ApiPlatform\Documentation\Documentation;
17
use ApiPlatform\JsonLd\ContextBuilder;
18
use ApiPlatform\JsonLd\ContextBuilderInterface;
19
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
20
use ApiPlatform\Metadata\ApiProperty;
21
use ApiPlatform\Metadata\ApiResource;
22
use ApiPlatform\Metadata\CollectionOperationInterface;
23
use ApiPlatform\Metadata\ErrorResource;
24
use ApiPlatform\Metadata\HttpOperation;
25
use ApiPlatform\Metadata\Operation;
26
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
27
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
28
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
29
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
30
use ApiPlatform\Metadata\ResourceClassResolverInterface;
31
use ApiPlatform\Metadata\UrlGeneratorInterface;
32
use Symfony\Component\PropertyInfo\Type;
33
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
34
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
35
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
36

37
use const ApiPlatform\JsonLd\HYDRA_CONTEXT;
38

39
/**
40
 * Creates a machine readable Hydra API documentation.
41
 *
42
 * @author Kévin Dunglas <dunglas@gmail.com>
43
 */
44
final class DocumentationNormalizer implements NormalizerInterface
45
{
46
    use HydraPrefixTrait;
47
    public const FORMAT = 'jsonld';
48

49
    public function __construct(
50
        private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory,
51
        private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
52
        private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory,
53
        private readonly ResourceClassResolverInterface $resourceClassResolver,
54
        private readonly UrlGeneratorInterface $urlGenerator,
55
        private readonly ?NameConverterInterface $nameConverter = null,
56
        private readonly ?array $defaultContext = [],
57
    ) {
58
    }
2,004✔
59

60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
64
    {
65
        $classes = [];
10✔
66
        $entrypointProperties = [];
10✔
67
        $hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
10✔
68

69
        foreach ($object->getResourceNameCollection() as $resourceClass) {
10✔
70
            $resourceMetadataCollection = $this->resourceMetadataFactory->create($resourceClass);
10✔
71

72
            $resourceMetadata = $resourceMetadataCollection[0];
10✔
73
            if (true === $resourceMetadata->getHideHydraOperation()) {
10✔
74
                continue;
10✔
75
            }
76

77
            $shortName = $resourceMetadata->getShortName();
10✔
78
            $prefixedShortName = $resourceMetadata->getTypes()[0] ?? "#$shortName";
10✔
79

80
            $this->populateEntrypointProperties($resourceMetadata, $shortName, $prefixedShortName, $entrypointProperties, $hydraPrefix, $resourceMetadataCollection);
10✔
81
            $classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix, $resourceMetadataCollection);
10✔
82
        }
83

84
        return $this->computeDoc($object, $this->getClasses($entrypointProperties, $classes, $hydraPrefix), $hydraPrefix);
10✔
85
    }
86

87
    /**
88
     * Populates entrypoint properties.
89
     */
90
    private function populateEntrypointProperties(ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array &$entrypointProperties, string $hydraPrefix, ?ResourceMetadataCollection $resourceMetadataCollection = null): void
91
    {
92
        $hydraCollectionOperations = $this->getHydraOperations(true, $resourceMetadataCollection, $hydraPrefix);
10✔
93
        if (empty($hydraCollectionOperations)) {
10✔
94
            return;
10✔
95
        }
96

UNCOV
97
        $entrypointProperty = [
8✔
UNCOV
98
            '@type' => $hydraPrefix.'SupportedProperty',
8✔
UNCOV
99
            $hydraPrefix.'property' => [
8✔
UNCOV
100
                '@id' => \sprintf('#Entrypoint/%s', lcfirst($shortName)),
8✔
UNCOV
101
                '@type' => $hydraPrefix.'Link',
8✔
UNCOV
102
                'domain' => '#Entrypoint',
8✔
UNCOV
103
                'owl:maxCardinality' => 1,
8✔
UNCOV
104
                'range' => [
8✔
UNCOV
105
                    ['@id' => $hydraPrefix.'Collection'],
8✔
UNCOV
106
                    [
8✔
UNCOV
107
                        'owl:equivalentClass' => [
8✔
UNCOV
108
                            'owl:onProperty' => ['@id' => $hydraPrefix.'member'],
8✔
UNCOV
109
                            'owl:allValuesFrom' => ['@id' => $prefixedShortName],
8✔
UNCOV
110
                        ],
8✔
UNCOV
111
                    ],
8✔
UNCOV
112
                ],
8✔
UNCOV
113
                $hydraPrefix.'supportedOperation' => $hydraCollectionOperations,
8✔
UNCOV
114
            ],
8✔
UNCOV
115
            $hydraPrefix.'title' => "get{$shortName}Collection",
8✔
UNCOV
116
            $hydraPrefix.'description' => "The collection of $shortName resources",
8✔
UNCOV
117
            $hydraPrefix.'readable' => true,
8✔
UNCOV
118
            $hydraPrefix.'writeable' => false,
8✔
UNCOV
119
        ];
8✔
120

UNCOV
121
        if ($resourceMetadata->getDeprecationReason()) {
8✔
UNCOV
122
            $entrypointProperty['owl:deprecated'] = true;
8✔
123
        }
124

UNCOV
125
        $entrypointProperties[] = $entrypointProperty;
8✔
126
    }
127

128
    /**
129
     * Gets a Hydra class.
130
     */
131
    private function getClass(string $resourceClass, ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array $context, string $hydraPrefix, ?ResourceMetadataCollection $resourceMetadataCollection = null): array
132
    {
133
        $description = $resourceMetadata->getDescription();
10✔
134
        $isDeprecated = $resourceMetadata->getDeprecationReason();
10✔
135

136
        $class = [
10✔
137
            '@id' => $prefixedShortName,
10✔
138
            '@type' => $hydraPrefix.'Class',
10✔
139
            $hydraPrefix.'title' => $shortName,
10✔
140
            $hydraPrefix.'supportedProperty' => $this->getHydraProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix),
10✔
141
            $hydraPrefix.'supportedOperation' => $this->getHydraOperations(false, $resourceMetadataCollection, $hydraPrefix),
10✔
142
        ];
10✔
143

144
        if (null !== $description) {
10✔
145
            $class[$hydraPrefix.'description'] = $description;
10✔
146
        }
147

148
        if ($resourceMetadata instanceof ErrorResource) {
10✔
149
            $class['subClassOf'] = 'Error';
10✔
150
        }
151

152
        if ($isDeprecated) {
10✔
UNCOV
153
            $class['owl:deprecated'] = true;
8✔
154
        }
155

156
        return $class;
10✔
157
    }
158

159
    /**
160
     * Creates context for property metatata factories.
161
     */
162
    private function getPropertyMetadataFactoryContext(ApiResource $resourceMetadata): array
163
    {
164
        $normalizationGroups = $resourceMetadata->getNormalizationContext()[AbstractNormalizer::GROUPS] ?? null;
10✔
165
        $denormalizationGroups = $resourceMetadata->getDenormalizationContext()[AbstractNormalizer::GROUPS] ?? null;
10✔
166
        $propertyContext = [
10✔
167
            'normalization_groups' => $normalizationGroups,
10✔
168
            'denormalization_groups' => $denormalizationGroups,
10✔
169
        ];
10✔
170
        $propertyNameContext = [];
10✔
171

172
        if ($normalizationGroups) {
10✔
UNCOV
173
            $propertyNameContext['serializer_groups'] = $normalizationGroups;
8✔
174
        }
175

176
        if (!$denormalizationGroups) {
10✔
177
            return [$propertyNameContext, $propertyContext];
10✔
178
        }
179

UNCOV
180
        if (!isset($propertyNameContext['serializer_groups'])) {
8✔
181
            $propertyNameContext['serializer_groups'] = $denormalizationGroups;
×
182

183
            return [$propertyNameContext, $propertyContext];
×
184
        }
185

UNCOV
186
        foreach ($denormalizationGroups as $group) {
8✔
UNCOV
187
            $propertyNameContext['serializer_groups'][] = $group;
8✔
188
        }
189

UNCOV
190
        return [$propertyNameContext, $propertyContext];
8✔
191
    }
192

193
    /**
194
     * Gets Hydra properties.
195
     */
196
    private function getHydraProperties(string $resourceClass, ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array $context, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
197
    {
198
        $classes = [];
10✔
199

200
        $classes[$resourceClass] = true;
10✔
201
        foreach ($resourceMetadata->getOperations() as $operation) {
10✔
202
            /** @var Operation $operation */
203
            if (!$operation instanceof CollectionOperationInterface) {
10✔
204
                continue;
10✔
205
            }
206

207
            $inputMetadata = $operation->getInput();
10✔
208
            if (null !== $inputClass = $inputMetadata['class'] ?? null) {
10✔
UNCOV
209
                $classes[$inputClass] = true;
8✔
210
            }
211

212
            $outputMetadata = $operation->getOutput();
10✔
213
            if (null !== $outputClass = $outputMetadata['class'] ?? null) {
10✔
UNCOV
214
                $classes[$outputClass] = true;
8✔
215
            }
216
        }
217

218
        /** @var string[] $classes */
219
        $classes = array_keys($classes);
10✔
220
        $properties = [];
10✔
221
        [$propertyNameContext, $propertyContext] = $this->getPropertyMetadataFactoryContext($resourceMetadata);
10✔
222
        foreach ($classes as $class) {
10✔
223
            foreach ($this->propertyNameCollectionFactory->create($class, $propertyNameContext) as $propertyName) {
10✔
224
                $propertyMetadata = $this->propertyMetadataFactory->create($class, $propertyName, $propertyContext);
10✔
225

226
                if (true === $propertyMetadata->isIdentifier() && false === $propertyMetadata->isWritable()) {
10✔
227
                    continue;
10✔
228
                }
229

230
                if ($this->nameConverter) {
10✔
231
                    $propertyName = $this->nameConverter->normalize($propertyName, $class, self::FORMAT, $context);
10✔
232
                }
233

234
                if (false === $propertyMetadata->getHydra()) {
10✔
235
                    continue;
10✔
236
                }
237

238
                $properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName, $hydraPrefix);
10✔
239
            }
240
        }
241

242
        return $properties;
10✔
243
    }
244

245
    /**
246
     * Gets Hydra operations.
247
     */
248
    private function getHydraOperations(bool $collection, ?ResourceMetadataCollection $resourceMetadataCollection = null, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
249
    {
250
        $hydraOperations = [];
10✔
251
        foreach ($resourceMetadataCollection as $resourceMetadata) {
10✔
252
            foreach ($resourceMetadata->getOperations() as $operation) {
10✔
253
                if (true === $operation->getHideHydraOperation()) {
10✔
254
                    continue;
10✔
255
                }
256

257
                if (('POST' === $operation->getMethod() || $operation instanceof CollectionOperationInterface) !== $collection) {
10✔
258
                    continue;
10✔
259
                }
260

261
                $hydraOperations[] = $this->getHydraOperation($operation, $operation->getShortName(), $hydraPrefix);
10✔
262
            }
263
        }
264

265
        return $hydraOperations;
10✔
266
    }
267

268
    /**
269
     * Gets and populates if applicable a Hydra operation.
270
     */
271
    private function getHydraOperation(HttpOperation $operation, string $prefixedShortName, string $hydraPrefix): array
272
    {
273
        $method = $operation->getMethod() ?: 'GET';
10✔
274

275
        $hydraOperation = $operation->getHydraContext() ?? [];
10✔
276
        if ($operation->getDeprecationReason()) {
10✔
UNCOV
277
            $hydraOperation['owl:deprecated'] = true;
8✔
278
        }
279

280
        $shortName = $operation->getShortName();
10✔
281
        $inputMetadata = $operation->getInput() ?? [];
10✔
282
        $outputMetadata = $operation->getOutput() ?? [];
10✔
283

284
        $inputClass = \array_key_exists('class', $inputMetadata) ? $inputMetadata['class'] : false;
10✔
285
        $outputClass = \array_key_exists('class', $outputMetadata) ? $outputMetadata['class'] : false;
10✔
286

287
        if ('GET' === $method && $operation instanceof CollectionOperationInterface) {
10✔
UNCOV
288
            $hydraOperation += [
8✔
UNCOV
289
                '@type' => [$hydraPrefix.'Operation', 'schema:FindAction'],
8✔
UNCOV
290
                $hydraPrefix.'description' => "Retrieves the collection of $shortName resources.",
8✔
UNCOV
291
                'returns' => null === $outputClass ? 'owl:Nothing' : $hydraPrefix.'Collection',
8✔
UNCOV
292
            ];
8✔
293
        } elseif ('GET' === $method) {
10✔
294
            $hydraOperation += [
10✔
295
                '@type' => [$hydraPrefix.'Operation', 'schema:FindAction'],
10✔
296
                $hydraPrefix.'description' => "Retrieves a $shortName resource.",
10✔
297
                'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
10✔
298
            ];
10✔
UNCOV
299
        } elseif ('PATCH' === $method) {
8✔
UNCOV
300
            $hydraOperation += [
8✔
UNCOV
301
                '@type' => $hydraPrefix.'Operation',
8✔
UNCOV
302
                $hydraPrefix.'description' => "Updates the $shortName resource.",
8✔
UNCOV
303
                'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
8✔
UNCOV
304
                'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName,
8✔
UNCOV
305
            ];
8✔
306

UNCOV
307
            if (null !== $inputClass) {
8✔
UNCOV
308
                $possibleValue = [];
8✔
UNCOV
309
                foreach ($operation->getInputFormats() as $mimeTypes) {
8✔
UNCOV
310
                    foreach ($mimeTypes as $mimeType) {
8✔
UNCOV
311
                        $possibleValue[] = $mimeType;
8✔
312
                    }
313
                }
314

UNCOV
315
                $hydraOperation['expectsHeader'] = [['headerName' => 'Content-Type', 'possibleValue' => $possibleValue]];
8✔
316
            }
UNCOV
317
        } elseif ('POST' === $method) {
8✔
UNCOV
318
            $hydraOperation += [
8✔
UNCOV
319
                '@type' => [$hydraPrefix.'Operation', 'schema:CreateAction'],
8✔
UNCOV
320
                $hydraPrefix.'description' => "Creates a $shortName resource.",
8✔
UNCOV
321
                'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
8✔
UNCOV
322
                'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName,
8✔
UNCOV
323
            ];
8✔
UNCOV
324
        } elseif ('PUT' === $method) {
8✔
UNCOV
325
            $hydraOperation += [
8✔
UNCOV
326
                '@type' => [$hydraPrefix.'Operation', 'schema:ReplaceAction'],
8✔
UNCOV
327
                $hydraPrefix.'description' => "Replaces the $shortName resource.",
8✔
UNCOV
328
                'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
8✔
UNCOV
329
                'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName,
8✔
UNCOV
330
            ];
8✔
UNCOV
331
        } elseif ('DELETE' === $method) {
8✔
UNCOV
332
            $hydraOperation += [
8✔
UNCOV
333
                '@type' => [$hydraPrefix.'Operation', 'schema:DeleteAction'],
8✔
UNCOV
334
                $hydraPrefix.'description' => "Deletes the $shortName resource.",
8✔
UNCOV
335
                'returns' => 'owl:Nothing',
8✔
UNCOV
336
            ];
8✔
337
        }
338

339
        $hydraOperation[$hydraPrefix.'method'] ??= $method;
10✔
340
        $hydraOperation[$hydraPrefix.'title'] ??= strtolower($method).$shortName.($operation instanceof CollectionOperationInterface ? 'Collection' : '');
10✔
341

342
        ksort($hydraOperation);
10✔
343

344
        return $hydraOperation;
10✔
345
    }
346

347
    /**
348
     * Gets the range of the property.
349
     */
350
    private function getRange(ApiProperty $propertyMetadata): array|string|null
351
    {
352
        $jsonldContext = $propertyMetadata->getJsonldContext();
10✔
353

354
        if (isset($jsonldContext['@type'])) {
10✔
355
            return $jsonldContext['@type'];
10✔
356
        }
357

358
        $builtInTypes = $propertyMetadata->getBuiltinTypes() ?? [];
10✔
359
        $types = [];
10✔
360

361
        foreach ($builtInTypes as $type) {
10✔
362
            if ($type->isCollection() && null !== $collectionType = $type->getCollectionValueTypes()[0] ?? null) {
10✔
UNCOV
363
                $type = $collectionType;
8✔
364
            }
365

366
            switch ($type->getBuiltinType()) {
10✔
367
                case Type::BUILTIN_TYPE_STRING:
8✔
368
                    if (!\in_array('xmls:string', $types, true)) {
10✔
369
                        $types[] = 'xmls:string';
10✔
370
                    }
371
                    break;
10✔
372
                case Type::BUILTIN_TYPE_INT:
8✔
373
                    if (!\in_array('xmls:integer', $types, true)) {
10✔
374
                        $types[] = 'xmls:integer';
10✔
375
                    }
376
                    break;
10✔
377
                case Type::BUILTIN_TYPE_FLOAT:
8✔
UNCOV
378
                    if (!\in_array('xmls:decimal', $types, true)) {
8✔
UNCOV
379
                        $types[] = 'xmls:decimal';
8✔
380
                    }
UNCOV
381
                    break;
8✔
382
                case Type::BUILTIN_TYPE_BOOL:
8✔
UNCOV
383
                    if (!\in_array('xmls:boolean', $types, true)) {
8✔
UNCOV
384
                        $types[] = 'xmls:boolean';
8✔
385
                    }
UNCOV
386
                    break;
8✔
387
                case Type::BUILTIN_TYPE_OBJECT:
8✔
388
                    if (null === $className = $type->getClassName()) {
10✔
389
                        continue 2;
×
390
                    }
391

392
                    if (is_a($className, \DateTimeInterface::class, true)) {
10✔
UNCOV
393
                        if (!\in_array('xmls:dateTime', $types, true)) {
8✔
UNCOV
394
                            $types[] = 'xmls:dateTime';
8✔
395
                        }
UNCOV
396
                        break;
8✔
397
                    }
398

399
                    if ($this->resourceClassResolver->isResourceClass($className)) {
10✔
UNCOV
400
                        $resourceMetadata = $this->resourceMetadataFactory->create($className);
8✔
UNCOV
401
                        $operation = $resourceMetadata->getOperation();
8✔
402

UNCOV
403
                        if (!$operation instanceof HttpOperation || !$operation->getTypes()) {
8✔
UNCOV
404
                            if (!\in_array("#{$operation->getShortName()}", $types, true)) {
8✔
UNCOV
405
                                $types[] = "#{$operation->getShortName()}";
8✔
406
                            }
UNCOV
407
                            break;
8✔
408
                        }
409

UNCOV
410
                        $types = array_unique(array_merge($types, $operation->getTypes()));
8✔
UNCOV
411
                        break;
8✔
412
                    }
413
            }
414
        }
415

416
        if ([] === $types) {
10✔
417
            return null;
10✔
418
        }
419

420
        return 1 === \count($types) ? $types[0] : $types;
10✔
421
    }
422

423
    private function isSingleRelation(ApiProperty $propertyMetadata): bool
424
    {
425
        $builtInTypes = $propertyMetadata->getBuiltinTypes() ?? [];
10✔
426

427
        foreach ($builtInTypes as $type) {
10✔
428
            $className = $type->getClassName();
10✔
429
            if (
430
                !$type->isCollection()
10✔
431
                && null !== $className
10✔
432
                && $this->resourceClassResolver->isResourceClass($className)
10✔
433
            ) {
UNCOV
434
                return true;
8✔
435
            }
436
        }
437

438
        return false;
10✔
439
    }
440

441
    /**
442
     * Builds the classes array.
443
     */
444
    private function getClasses(array $entrypointProperties, array $classes, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
445
    {
446
        $classes[] = [
10✔
447
            '@id' => '#Entrypoint',
10✔
448
            '@type' => $hydraPrefix.'Class',
10✔
449
            $hydraPrefix.'title' => 'Entrypoint',
10✔
450
            $hydraPrefix.'supportedProperty' => $entrypointProperties,
10✔
451
            $hydraPrefix.'supportedOperation' => [
10✔
452
                '@type' => $hydraPrefix.'Operation',
10✔
453
                $hydraPrefix.'method' => 'GET',
10✔
454
                $hydraPrefix.'title' => 'index',
10✔
455
                $hydraPrefix.'description' => 'The API Entrypoint.',
10✔
456
                $hydraPrefix.'returns' => 'Entrypoint',
10✔
457
            ],
10✔
458
        ];
10✔
459

460
        $classes[] = [
10✔
461
            '@id' => '#ConstraintViolationList',
10✔
462
            '@type' => $hydraPrefix.'Class',
10✔
463
            $hydraPrefix.'title' => 'ConstraintViolationList',
10✔
464
            $hydraPrefix.'description' => 'A constraint violation List.',
10✔
465
            $hydraPrefix.'supportedProperty' => [
10✔
466
                [
10✔
467
                    '@type' => $hydraPrefix.'SupportedProperty',
10✔
468
                    $hydraPrefix.'property' => [
10✔
469
                        '@id' => '#ConstraintViolationList/propertyPath',
10✔
470
                        '@type' => 'rdf:Property',
10✔
471
                        'rdfs:label' => 'propertyPath',
10✔
472
                        'domain' => '#ConstraintViolationList',
10✔
473
                        'range' => 'xmls:string',
10✔
474
                    ],
10✔
475
                    $hydraPrefix.'title' => 'propertyPath',
10✔
476
                    $hydraPrefix.'description' => 'The property path of the violation',
10✔
477
                    $hydraPrefix.'readable' => true,
10✔
478
                    $hydraPrefix.'writeable' => false,
10✔
479
                ],
10✔
480
                [
10✔
481
                    '@type' => $hydraPrefix.'SupportedProperty',
10✔
482
                    $hydraPrefix.'property' => [
10✔
483
                        '@id' => '#ConstraintViolationList/message',
10✔
484
                        '@type' => 'rdf:Property',
10✔
485
                        'rdfs:label' => 'message',
10✔
486
                        'domain' => '#ConstraintViolationList',
10✔
487
                        'range' => 'xmls:string',
10✔
488
                    ],
10✔
489
                    $hydraPrefix.'title' => 'message',
10✔
490
                    $hydraPrefix.'description' => 'The message associated with the violation',
10✔
491
                    $hydraPrefix.'readable' => true,
10✔
492
                    $hydraPrefix.'writeable' => false,
10✔
493
                ],
10✔
494
            ],
10✔
495
        ];
10✔
496

497
        return $classes;
10✔
498
    }
499

500
    /**
501
     * Gets a property definition.
502
     */
503
    private function getProperty(ApiProperty $propertyMetadata, string $propertyName, string $prefixedShortName, string $shortName, string $hydraPrefix): array
504
    {
505
        if ($iri = $propertyMetadata->getIris()) {
10✔
UNCOV
506
            $iri = 1 === (is_countable($iri) ? \count($iri) : 0) ? $iri[0] : $iri;
8✔
507
        }
508

509
        if (!isset($iri)) {
10✔
510
            $iri = "#$shortName/$propertyName";
10✔
511
        }
512

513
        $propertyData = ($propertyMetadata->getJsonldContext()[$hydraPrefix.'property'] ?? []) + [
10✔
514
            '@id' => $iri,
10✔
515
            '@type' => false === $propertyMetadata->isReadableLink() ? $hydraPrefix.'Link' : 'rdf:Property',
10✔
516
            'domain' => $prefixedShortName,
10✔
517
            'label' => $propertyName,
10✔
518
        ];
10✔
519

520
        if (!isset($propertyData['owl:deprecated']) && $propertyMetadata->getDeprecationReason()) {
10✔
UNCOV
521
            $propertyData['owl:deprecated'] = true;
8✔
522
        }
523

524
        if (!isset($propertyData['owl:maxCardinality']) && $this->isSingleRelation($propertyMetadata)) {
10✔
UNCOV
525
            $propertyData['owl:maxCardinality'] = 1;
8✔
526
        }
527

528
        if (!isset($propertyData['range']) && null !== $range = $this->getRange($propertyMetadata)) {
10✔
529
            $propertyData['range'] = $range;
10✔
530
        }
531

532
        $property = [
10✔
533
            '@type' => $hydraPrefix.'SupportedProperty',
10✔
534
            $hydraPrefix.'property' => $propertyData,
10✔
535
            $hydraPrefix.'title' => $propertyName,
10✔
536
            $hydraPrefix.'required' => $propertyMetadata->isRequired() ?? false,
10✔
537
            $hydraPrefix.'readable' => $propertyMetadata->isReadable(),
10✔
538
            $hydraPrefix.'writeable' => $propertyMetadata->isWritable() || $propertyMetadata->isInitializable(),
10✔
539
        ];
10✔
540

541
        if (null !== $description = $propertyMetadata->getDescription()) {
10✔
542
            $property[$hydraPrefix.'description'] = $description;
10✔
543
        }
544

545
        return $property;
10✔
546
    }
547

548
    /**
549
     * Computes the documentation.
550
     */
551
    private function computeDoc(Documentation $object, array $classes, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
552
    {
553
        $doc = ['@context' => $this->getContext($hydraPrefix), '@id' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT]), '@type' => $hydraPrefix.'ApiDocumentation'];
10✔
554

555
        if ('' !== $object->getTitle()) {
10✔
556
            $doc[$hydraPrefix.'title'] = $object->getTitle();
10✔
557
        }
558

559
        if ('' !== $object->getDescription()) {
10✔
560
            $doc[$hydraPrefix.'description'] = $object->getDescription();
10✔
561
        }
562

563
        $doc[$hydraPrefix.'entrypoint'] = $this->urlGenerator->generate('api_entrypoint');
10✔
564
        $doc[$hydraPrefix.'supportedClass'] = $classes;
10✔
565

566
        return $doc;
10✔
567
    }
568

569
    /**
570
     * Builds the JSON-LD context for the API documentation.
571
     */
572
    private function getContext(string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
573
    {
574
        return [
10✔
575
            HYDRA_CONTEXT,
10✔
576
            [
10✔
577
                '@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
10✔
578
                'hydra' => ContextBuilderInterface::HYDRA_NS,
10✔
579
                'rdf' => ContextBuilderInterface::RDF_NS,
10✔
580
                'rdfs' => ContextBuilderInterface::RDFS_NS,
10✔
581
                'xmls' => ContextBuilderInterface::XML_NS,
10✔
582
                'owl' => ContextBuilderInterface::OWL_NS,
10✔
583
                'schema' => ContextBuilderInterface::SCHEMA_ORG_NS,
10✔
584
                'domain' => ['@id' => 'rdfs:domain', '@type' => '@id'],
10✔
585
                'range' => ['@id' => 'rdfs:range', '@type' => '@id'],
10✔
586
                'subClassOf' => ['@id' => 'rdfs:subClassOf', '@type' => '@id'],
10✔
587
            ],
10✔
588
        ];
10✔
589
    }
590

591
    /**
592
     * {@inheritdoc}
593
     */
594
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
595
    {
596
        return self::FORMAT === $format && $data instanceof Documentation;
10✔
597
    }
598

599
    public function getSupportedTypes($format): array
600
    {
601
        return self::FORMAT === $format ? [Documentation::class => true] : [];
1,843✔
602
    }
603
}
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