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

api-platform / core / 10884379752

16 Sep 2024 01:01PM UTC coverage: 7.281% (-0.4%) from 7.672%
10884379752

push

github

soyuka
Merge 3.4

0 of 100 new or added lines in 7 files covered. (0.0%)

5332 existing lines in 181 files now uncovered.

11994 of 164725 relevant lines covered (7.28%)

9.52 hits per line

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

97.22
/src/Hal/JsonSchema/SchemaFactory.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\Hal\JsonSchema;
15

16
use ApiPlatform\JsonSchema\Schema;
17
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
18
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
19
use ApiPlatform\Metadata\Operation;
20

21
/**
22
 * Decorator factory which adds HAL properties to the JSON Schema document.
23
 *
24
 * @author Kévin Dunglas <dunglas@gmail.com>
25
 * @author Jachim Coudenys <jachimcoudenys@gmail.com>
26
 */
27
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
28
{
29
    private const HREF_PROP = [
30
        'href' => [
31
            'type' => 'string',
32
            'format' => 'iri-reference',
33
        ],
34
    ];
35
    private const BASE_PROPS = [
36
        '_links' => [
37
            'type' => 'object',
38
            'properties' => [
39
                'self' => [
40
                    'type' => 'object',
41
                    'properties' => self::HREF_PROP,
42
                ],
43
            ],
44
        ],
45
    ];
46

47
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
48
    {
49
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
1,111✔
50
            $this->schemaFactory->setSchemaFactory($this);
1,111✔
51
        }
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function buildSchema(string $className, string $format = 'jsonhal', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
58
    {
59
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
152✔
60
        if ('jsonhal' !== $format) {
152✔
61
            return $schema;
122✔
62
        }
63

64
        $definitions = $schema->getDefinitions();
65✔
65
        if ($key = $schema->getRootDefinitionKey()) {
65✔
66
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
59✔
67

68
            return $schema;
59✔
69
        }
70
        if ($key = $schema->getItemsDefinitionKey()) {
47✔
71
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
47✔
72
        }
73

74
        if (($schema['type'] ?? '') === 'array') {
47✔
75
            $items = $schema['items'];
47✔
76
            unset($schema['items']);
47✔
77

78
            $schema['type'] = 'object';
47✔
79
            $schema['properties'] = [
47✔
80
                '_embedded' => [
47✔
81
                    'anyOf' => [
47✔
82
                        [
47✔
83
                            'type' => 'object',
47✔
84
                            'properties' => [
47✔
85
                                'item' => [
47✔
86
                                    'type' => 'array',
47✔
87
                                    'items' => $items,
47✔
88
                                ],
47✔
89
                            ],
47✔
90
                        ],
47✔
91
                        ['type' => 'object'],
47✔
92
                    ],
47✔
93
                ],
47✔
94
                'totalItems' => [
47✔
95
                    'type' => 'integer',
47✔
96
                    'minimum' => 0,
47✔
97
                ],
47✔
98
                'itemsPerPage' => [
47✔
99
                    'type' => 'integer',
47✔
100
                    'minimum' => 0,
47✔
101
                ],
47✔
102
                '_links' => [
47✔
103
                    'type' => 'object',
47✔
104
                    'properties' => [
47✔
105
                        'self' => [
47✔
106
                            'type' => 'object',
47✔
107
                            'properties' => self::HREF_PROP,
47✔
108
                        ],
47✔
109
                        'first' => [
47✔
110
                            'type' => 'object',
47✔
111
                            'properties' => self::HREF_PROP,
47✔
112
                        ],
47✔
113
                        'last' => [
47✔
114
                            'type' => 'object',
47✔
115
                            'properties' => self::HREF_PROP,
47✔
116
                        ],
47✔
117
                        'next' => [
47✔
118
                            'type' => 'object',
47✔
119
                            'properties' => self::HREF_PROP,
47✔
120
                        ],
47✔
121
                        'previous' => [
47✔
122
                            'type' => 'object',
47✔
123
                            'properties' => self::HREF_PROP,
47✔
124
                        ],
47✔
125
                    ],
47✔
126
                ],
47✔
127
            ];
47✔
128
            $schema['required'] = [
47✔
129
                '_links',
47✔
130
                '_embedded',
47✔
131
            ];
47✔
132

133
            return $schema;
47✔
134
        }
135

UNCOV
136
        return $schema;
14✔
137
    }
138

139
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
140
    {
141
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
142
            $this->schemaFactory->setSchemaFactory($schemaFactory);
×
143
        }
144
    }
145
}
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

© 2026 Coveralls, Inc