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

api-platform / core / 14854709983

06 May 2025 08:07AM UTC coverage: 8.457% (-0.002%) from 8.459%
14854709983

Pull #7122

github

web-flow
Merge 6b1cde96f into f55606b01
Pull Request #7122: Cherry picks from main (deprecations)

0 of 35 new or added lines in 9 files covered. (0.0%)

2157 existing lines in 159 files now uncovered.

13397 of 158416 relevant lines covered (8.46%)

22.88 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
        private readonly ?bool $entrypointEnabled = true,
58
    ) {
59
    }
2,020✔
60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

157
        return $class;
10✔
158
    }
159

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

243
        return $properties;
10✔
244
    }
245

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

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

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

266
        return $hydraOperations;
10✔
267
    }
268

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

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

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

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

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

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

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

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

343
        ksort($hydraOperation);
10✔
344

345
        return $hydraOperation;
10✔
346
    }
347

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

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

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

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

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

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

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

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

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

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

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

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

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

439
        return false;
10✔
440
    }
441

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

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

500
        return $classes;
10✔
501
    }
502

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

512
        if (!isset($iri)) {
10✔
513
            $iri = "#$shortName/$propertyName";
10✔
514
        }
515

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

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

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

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

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

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

548
        return $property;
10✔
549
    }
550

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

558
        if ('' !== $object->getTitle()) {
10✔
559
            $doc[$hydraPrefix.'title'] = $object->getTitle();
10✔
560
        }
561

562
        if ('' !== $object->getDescription()) {
10✔
563
            $doc[$hydraPrefix.'description'] = $object->getDescription();
10✔
564
        }
565

566
        if ($this->entrypointEnabled) {
10✔
567
            $doc[$hydraPrefix.'entrypoint'] = $this->urlGenerator->generate('api_entrypoint');
10✔
568
        }
569
        $doc[$hydraPrefix.'supportedClass'] = $classes;
10✔
570

571
        return $doc;
10✔
572
    }
573

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

596
    /**
597
     * {@inheritdoc}
598
     */
599
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
600
    {
601
        return self::FORMAT === $format && $data instanceof Documentation;
10✔
602
    }
603

604
    public function getSupportedTypes($format): array
605
    {
606
        return self::FORMAT === $format ? [Documentation::class => true] : [];
1,853✔
607
    }
608
}
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