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

api-platform / core / 13718525764

07 Mar 2025 10:15AM UTC coverage: 7.289%. Remained the same
13718525764

push

github

web-flow
fix(doctrine): Add a proper exception when a doctrine manager could not be found for a resource class (#6995)

* fix(doctrine): Add a proper exception when a doctrine manager could not be found for a resource class

* Update ItemProvider.php

---------

Co-authored-by: Antoine Bluchet <soyuka@users.noreply.github.com>

1 of 2 new or added lines in 1 file covered. (50.0%)

2387 existing lines in 157 files now uncovered.

12437 of 170624 relevant lines covered (7.29%)

11.99 hits per line

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

99.09
/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 ApiPlatform\Validator\Exception\ValidationException;
33
use Symfony\Component\PropertyInfo\Type;
34
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
35
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
36
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
37

38
use const ApiPlatform\JsonLd\HYDRA_CONTEXT;
39

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

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

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

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

UNCOV
73
            $resourceMetadata = $resourceMetadataCollection[0];
4✔
UNCOV
74
            if ($resourceMetadata instanceof ErrorResource && ValidationException::class === $resourceMetadata->getClass()) {
4✔
UNCOV
75
                continue;
4✔
76
            }
77

UNCOV
78
            $shortName = $resourceMetadata->getShortName();
4✔
79

UNCOV
80
            $prefixedShortName = $resourceMetadata->getTypes()[0] ?? "#$shortName";
4✔
UNCOV
81
            $this->populateEntrypointProperties($resourceMetadata, $shortName, $prefixedShortName, $entrypointProperties, $hydraPrefix, $resourceMetadataCollection);
4✔
UNCOV
82
            $classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix, $resourceMetadataCollection);
4✔
83
        }
84

UNCOV
85
        return $this->computeDoc($object, $this->getClasses($entrypointProperties, $classes, $hydraPrefix), $hydraPrefix);
4✔
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
    {
UNCOV
93
        $hydraCollectionOperations = $this->getHydraOperations(true, $resourceMetadataCollection, $hydraPrefix);
4✔
UNCOV
94
        if (empty($hydraCollectionOperations)) {
4✔
UNCOV
95
            return;
4✔
96
        }
97

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

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

UNCOV
125
        $entrypointProperties[] = $entrypointProperty;
4✔
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
    {
UNCOV
133
        $description = $resourceMetadata->getDescription();
4✔
UNCOV
134
        $isDeprecated = $resourceMetadata->getDeprecationReason();
4✔
135

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

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

UNCOV
149
        if ($isDeprecated) {
4✔
UNCOV
150
            $class['owl:deprecated'] = true;
4✔
151
        }
152

UNCOV
153
        return $class;
4✔
154
    }
155

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

UNCOV
169
        if ($normalizationGroups) {
4✔
UNCOV
170
            $propertyNameContext['serializer_groups'] = $normalizationGroups;
4✔
171
        }
172

UNCOV
173
        if (!$denormalizationGroups) {
4✔
UNCOV
174
            return [$propertyNameContext, $propertyContext];
4✔
175
        }
176

UNCOV
177
        if (!isset($propertyNameContext['serializer_groups'])) {
4✔
178
            $propertyNameContext['serializer_groups'] = $denormalizationGroups;
×
179

180
            return [$propertyNameContext, $propertyContext];
×
181
        }
182

UNCOV
183
        foreach ($denormalizationGroups as $group) {
4✔
UNCOV
184
            $propertyNameContext['serializer_groups'][] = $group;
4✔
185
        }
186

UNCOV
187
        return [$propertyNameContext, $propertyContext];
4✔
188
    }
189

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

UNCOV
197
        $classes[$resourceClass] = true;
4✔
UNCOV
198
        foreach ($resourceMetadata->getOperations() as $operation) {
4✔
199
            /** @var Operation $operation */
UNCOV
200
            if (!$operation instanceof CollectionOperationInterface) {
4✔
UNCOV
201
                continue;
4✔
202
            }
203

UNCOV
204
            $inputMetadata = $operation->getInput();
4✔
UNCOV
205
            if (null !== $inputClass = $inputMetadata['class'] ?? null) {
4✔
UNCOV
206
                $classes[$inputClass] = true;
4✔
207
            }
208

UNCOV
209
            $outputMetadata = $operation->getOutput();
4✔
UNCOV
210
            if (null !== $outputClass = $outputMetadata['class'] ?? null) {
4✔
UNCOV
211
                $classes[$outputClass] = true;
4✔
212
            }
213
        }
214

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

UNCOV
223
                if (true === $propertyMetadata->isIdentifier() && false === $propertyMetadata->isWritable()) {
4✔
UNCOV
224
                    continue;
4✔
225
                }
226

UNCOV
227
                if ($this->nameConverter) {
4✔
UNCOV
228
                    $propertyName = $this->nameConverter->normalize($propertyName, $class, self::FORMAT, $context);
4✔
229
                }
230

UNCOV
231
                $properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName, $hydraPrefix);
4✔
232
            }
233
        }
234

UNCOV
235
        return $properties;
4✔
236
    }
237

238
    /**
239
     * Gets Hydra operations.
240
     */
241
    private function getHydraOperations(bool $collection, ?ResourceMetadataCollection $resourceMetadataCollection = null, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
242
    {
UNCOV
243
        $hydraOperations = [];
4✔
UNCOV
244
        foreach ($resourceMetadataCollection as $resourceMetadata) {
4✔
UNCOV
245
            foreach ($resourceMetadata->getOperations() as $operation) {
4✔
UNCOV
246
                if (('POST' === $operation->getMethod() || $operation instanceof CollectionOperationInterface) !== $collection) {
4✔
UNCOV
247
                    continue;
4✔
248
                }
UNCOV
249
                $hydraOperations[] = $this->getHydraOperation($operation, $operation->getShortName(), $hydraPrefix);
4✔
250
            }
251
        }
252

UNCOV
253
        return $hydraOperations;
4✔
254
    }
255

256
    /**
257
     * Gets and populates if applicable a Hydra operation.
258
     */
259
    private function getHydraOperation(HttpOperation $operation, string $prefixedShortName, string $hydraPrefix): array
260
    {
UNCOV
261
        $method = $operation->getMethod() ?: 'GET';
4✔
262

UNCOV
263
        $hydraOperation = $operation->getHydraContext() ?? [];
4✔
UNCOV
264
        if ($operation->getDeprecationReason()) {
4✔
UNCOV
265
            $hydraOperation['owl:deprecated'] = true;
4✔
266
        }
267

UNCOV
268
        $shortName = $operation->getShortName();
4✔
UNCOV
269
        $inputMetadata = $operation->getInput() ?? [];
4✔
UNCOV
270
        $outputMetadata = $operation->getOutput() ?? [];
4✔
271

UNCOV
272
        $inputClass = \array_key_exists('class', $inputMetadata) ? $inputMetadata['class'] : false;
4✔
UNCOV
273
        $outputClass = \array_key_exists('class', $outputMetadata) ? $outputMetadata['class'] : false;
4✔
274

UNCOV
275
        if ('GET' === $method && $operation instanceof CollectionOperationInterface) {
4✔
UNCOV
276
            $hydraOperation += [
4✔
UNCOV
277
                '@type' => [$hydraPrefix.'Operation', 'schema:FindAction'],
4✔
UNCOV
278
                $hydraPrefix.'title' => "Retrieves the collection of $shortName resources.",
4✔
UNCOV
279
                'returns' => null === $outputClass ? 'owl:Nothing' : $hydraPrefix.'Collection',
4✔
UNCOV
280
            ];
4✔
UNCOV
281
        } elseif ('GET' === $method) {
4✔
UNCOV
282
            $hydraOperation += [
4✔
UNCOV
283
                '@type' => [$hydraPrefix.'Operation', 'schema:FindAction'],
4✔
UNCOV
284
                $hydraPrefix.'title' => "Retrieves a $shortName resource.",
4✔
UNCOV
285
                'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
4✔
UNCOV
286
            ];
4✔
UNCOV
287
        } elseif ('PATCH' === $method) {
4✔
UNCOV
288
            $hydraOperation += [
4✔
UNCOV
289
                '@type' => $hydraPrefix.'Operation',
4✔
UNCOV
290
                $hydraPrefix.'title' => "Updates the $shortName resource.",
4✔
UNCOV
291
                'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
4✔
UNCOV
292
                'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName,
4✔
UNCOV
293
            ];
4✔
UNCOV
294
        } elseif ('POST' === $method) {
4✔
UNCOV
295
            $hydraOperation += [
4✔
UNCOV
296
                '@type' => [$hydraPrefix.'Operation', 'schema:CreateAction'],
4✔
UNCOV
297
                $hydraPrefix.'title' => "Creates a $shortName resource.",
4✔
UNCOV
298
                'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
4✔
UNCOV
299
                'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName,
4✔
UNCOV
300
            ];
4✔
UNCOV
301
        } elseif ('PUT' === $method) {
4✔
UNCOV
302
            $hydraOperation += [
4✔
UNCOV
303
                '@type' => [$hydraPrefix.'Operation', 'schema:ReplaceAction'],
4✔
UNCOV
304
                $hydraPrefix.'title' => "Replaces the $shortName resource.",
4✔
UNCOV
305
                'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
4✔
UNCOV
306
                'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName,
4✔
UNCOV
307
            ];
4✔
UNCOV
308
        } elseif ('DELETE' === $method) {
4✔
UNCOV
309
            $hydraOperation += [
4✔
UNCOV
310
                '@type' => [$hydraPrefix.'Operation', 'schema:DeleteAction'],
4✔
UNCOV
311
                $hydraPrefix.'title' => "Deletes the $shortName resource.",
4✔
UNCOV
312
                'returns' => 'owl:Nothing',
4✔
UNCOV
313
            ];
4✔
314
        }
315

UNCOV
316
        $hydraOperation[$hydraPrefix.'method'] ?? $hydraOperation[$hydraPrefix.'method'] = $method;
4✔
317

UNCOV
318
        if (!isset($hydraOperation['rdfs:label']) && isset($hydraOperation[$hydraPrefix.'title'])) {
4✔
UNCOV
319
            $hydraOperation['rdfs:label'] = $hydraOperation[$hydraPrefix.'title'];
4✔
320
        }
321

UNCOV
322
        ksort($hydraOperation);
4✔
323

UNCOV
324
        return $hydraOperation;
4✔
325
    }
326

327
    /**
328
     * Gets the range of the property.
329
     */
330
    private function getRange(ApiProperty $propertyMetadata): array|string|null
331
    {
UNCOV
332
        $jsonldContext = $propertyMetadata->getJsonldContext();
4✔
333

UNCOV
334
        if (isset($jsonldContext['@type'])) {
4✔
UNCOV
335
            return $jsonldContext['@type'];
4✔
336
        }
337

UNCOV
338
        $builtInTypes = $propertyMetadata->getBuiltinTypes() ?? [];
4✔
UNCOV
339
        $types = [];
4✔
340

UNCOV
341
        foreach ($builtInTypes as $type) {
4✔
UNCOV
342
            if ($type->isCollection() && null !== $collectionType = $type->getCollectionValueTypes()[0] ?? null) {
4✔
UNCOV
343
                $type = $collectionType;
4✔
344
            }
345

UNCOV
346
            switch ($type->getBuiltinType()) {
4✔
347
                case Type::BUILTIN_TYPE_STRING:
4✔
UNCOV
348
                    if (!\in_array('xmls:string', $types, true)) {
4✔
UNCOV
349
                        $types[] = 'xmls:string';
4✔
350
                    }
UNCOV
351
                    break;
4✔
352
                case Type::BUILTIN_TYPE_INT:
4✔
UNCOV
353
                    if (!\in_array('xmls:integer', $types, true)) {
4✔
UNCOV
354
                        $types[] = 'xmls:integer';
4✔
355
                    }
UNCOV
356
                    break;
4✔
357
                case Type::BUILTIN_TYPE_FLOAT:
4✔
UNCOV
358
                    if (!\in_array('xmls:decimal', $types, true)) {
4✔
UNCOV
359
                        $types[] = 'xmls:decimal';
4✔
360
                    }
UNCOV
361
                    break;
4✔
362
                case Type::BUILTIN_TYPE_BOOL:
4✔
UNCOV
363
                    if (!\in_array('xmls:boolean', $types, true)) {
4✔
UNCOV
364
                        $types[] = 'xmls:boolean';
4✔
365
                    }
UNCOV
366
                    break;
4✔
367
                case Type::BUILTIN_TYPE_OBJECT:
4✔
UNCOV
368
                    if (null === $className = $type->getClassName()) {
4✔
369
                        continue 2;
×
370
                    }
371

UNCOV
372
                    if (is_a($className, \DateTimeInterface::class, true)) {
4✔
UNCOV
373
                        if (!\in_array('xmls:dateTime', $types, true)) {
4✔
UNCOV
374
                            $types[] = 'xmls:dateTime';
4✔
375
                        }
UNCOV
376
                        break;
4✔
377
                    }
378

UNCOV
379
                    if ($this->resourceClassResolver->isResourceClass($className)) {
4✔
UNCOV
380
                        $resourceMetadata = $this->resourceMetadataFactory->create($className);
4✔
UNCOV
381
                        $operation = $resourceMetadata->getOperation();
4✔
382

UNCOV
383
                        if (!$operation instanceof HttpOperation || !$operation->getTypes()) {
4✔
UNCOV
384
                            if (!\in_array("#{$operation->getShortName()}", $types, true)) {
4✔
UNCOV
385
                                $types[] = "#{$operation->getShortName()}";
4✔
386
                            }
UNCOV
387
                            break;
4✔
388
                        }
389

UNCOV
390
                        $types = array_unique(array_merge($types, $operation->getTypes()));
4✔
UNCOV
391
                        break;
4✔
392
                    }
393
            }
394
        }
395

UNCOV
396
        if ([] === $types) {
4✔
UNCOV
397
            return null;
4✔
398
        }
399

UNCOV
400
        return 1 === \count($types) ? $types[0] : $types;
4✔
401
    }
402

403
    private function isSingleRelation(ApiProperty $propertyMetadata): bool
404
    {
UNCOV
405
        $builtInTypes = $propertyMetadata->getBuiltinTypes() ?? [];
4✔
406

UNCOV
407
        foreach ($builtInTypes as $type) {
4✔
UNCOV
408
            $className = $type->getClassName();
4✔
409
            if (
UNCOV
410
                !$type->isCollection()
4✔
UNCOV
411
                && null !== $className
4✔
UNCOV
412
                && $this->resourceClassResolver->isResourceClass($className)
4✔
413
            ) {
UNCOV
414
                return true;
4✔
415
            }
416
        }
417

UNCOV
418
        return false;
4✔
419
    }
420

421
    /**
422
     * Builds the classes array.
423
     */
424
    private function getClasses(array $entrypointProperties, array $classes, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
425
    {
UNCOV
426
        $classes[] = [
4✔
UNCOV
427
            '@id' => '#Entrypoint',
4✔
UNCOV
428
            '@type' => $hydraPrefix.'Class',
4✔
UNCOV
429
            $hydraPrefix.'title' => 'The API entrypoint',
4✔
UNCOV
430
            $hydraPrefix.'supportedProperty' => $entrypointProperties,
4✔
UNCOV
431
            $hydraPrefix.'supportedOperation' => [
4✔
UNCOV
432
                '@type' => $hydraPrefix.'Operation',
4✔
UNCOV
433
                $hydraPrefix.'method' => 'GET',
4✔
UNCOV
434
                'rdfs:label' => 'The API entrypoint.',
4✔
UNCOV
435
                'returns' => 'EntryPoint',
4✔
UNCOV
436
            ],
4✔
UNCOV
437
        ];
4✔
438

439
        // Constraint violation
UNCOV
440
        $classes[] = [
4✔
UNCOV
441
            '@id' => '#ConstraintViolation',
4✔
UNCOV
442
            '@type' => $hydraPrefix.'Class',
4✔
UNCOV
443
            $hydraPrefix.'title' => 'A constraint violation',
4✔
UNCOV
444
            $hydraPrefix.'supportedProperty' => [
4✔
UNCOV
445
                [
4✔
UNCOV
446
                    '@type' => $hydraPrefix.'SupportedProperty',
4✔
UNCOV
447
                    $hydraPrefix.'property' => [
4✔
UNCOV
448
                        '@id' => '#ConstraintViolation/propertyPath',
4✔
UNCOV
449
                        '@type' => 'rdf:Property',
4✔
UNCOV
450
                        'rdfs:label' => 'propertyPath',
4✔
UNCOV
451
                        'domain' => '#ConstraintViolation',
4✔
UNCOV
452
                        'range' => 'xmls:string',
4✔
UNCOV
453
                    ],
4✔
UNCOV
454
                    $hydraPrefix.'title' => 'propertyPath',
4✔
UNCOV
455
                    $hydraPrefix.'description' => 'The property path of the violation',
4✔
UNCOV
456
                    $hydraPrefix.'readable' => true,
4✔
UNCOV
457
                    $hydraPrefix.'writeable' => false,
4✔
UNCOV
458
                ],
4✔
UNCOV
459
                [
4✔
UNCOV
460
                    '@type' => $hydraPrefix.'SupportedProperty',
4✔
UNCOV
461
                    $hydraPrefix.'property' => [
4✔
UNCOV
462
                        '@id' => '#ConstraintViolation/message',
4✔
UNCOV
463
                        '@type' => 'rdf:Property',
4✔
UNCOV
464
                        'rdfs:label' => 'message',
4✔
UNCOV
465
                        'domain' => '#ConstraintViolation',
4✔
UNCOV
466
                        'range' => 'xmls:string',
4✔
UNCOV
467
                    ],
4✔
UNCOV
468
                    $hydraPrefix.'title' => 'message',
4✔
UNCOV
469
                    $hydraPrefix.'description' => 'The message associated with the violation',
4✔
UNCOV
470
                    $hydraPrefix.'readable' => true,
4✔
UNCOV
471
                    $hydraPrefix.'writeable' => false,
4✔
UNCOV
472
                ],
4✔
UNCOV
473
            ],
4✔
UNCOV
474
        ];
4✔
475

476
        // Constraint violation list
UNCOV
477
        $classes[] = [
4✔
UNCOV
478
            '@id' => '#ConstraintViolationList',
4✔
UNCOV
479
            '@type' => $hydraPrefix.'Class',
4✔
UNCOV
480
            'subClassOf' => $hydraPrefix.'Error',
4✔
UNCOV
481
            $hydraPrefix.'title' => 'A constraint violation list',
4✔
UNCOV
482
            $hydraPrefix.'supportedProperty' => [
4✔
UNCOV
483
                [
4✔
UNCOV
484
                    '@type' => $hydraPrefix.'SupportedProperty',
4✔
UNCOV
485
                    $hydraPrefix.'property' => [
4✔
UNCOV
486
                        '@id' => '#ConstraintViolationList/violations',
4✔
UNCOV
487
                        '@type' => 'rdf:Property',
4✔
UNCOV
488
                        'rdfs:label' => 'violations',
4✔
UNCOV
489
                        'domain' => '#ConstraintViolationList',
4✔
UNCOV
490
                        'range' => '#ConstraintViolation',
4✔
UNCOV
491
                    ],
4✔
UNCOV
492
                    $hydraPrefix.'title' => 'violations',
4✔
UNCOV
493
                    $hydraPrefix.'description' => 'The violations',
4✔
UNCOV
494
                    $hydraPrefix.'readable' => true,
4✔
UNCOV
495
                    $hydraPrefix.'writeable' => false,
4✔
UNCOV
496
                ],
4✔
UNCOV
497
            ],
4✔
UNCOV
498
        ];
4✔
499

UNCOV
500
        return $classes;
4✔
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
    {
UNCOV
508
        if ($iri = $propertyMetadata->getIris()) {
4✔
UNCOV
509
            $iri = 1 === (is_countable($iri) ? \count($iri) : 0) ? $iri[0] : $iri;
4✔
510
        }
511

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

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

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

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

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

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

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

UNCOV
548
        return $property;
4✔
549
    }
550

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

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

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

UNCOV
566
        $doc[$hydraPrefix.'entrypoint'] = $this->urlGenerator->generate('api_entrypoint');
4✔
UNCOV
567
        $doc[$hydraPrefix.'supportedClass'] = $classes;
4✔
568

UNCOV
569
        return $doc;
4✔
570
    }
571

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

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

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