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

api-platform / core / 14246153067

03 Apr 2025 02:56PM UTC coverage: 7.286% (-0.002%) from 7.288%
14246153067

push

github

web-flow
Merge commit from fork

12 of 160 new or added lines in 7 files covered. (7.5%)

2343 existing lines in 152 files now uncovered.

12450 of 170870 relevant lines covered (7.29%)

12.06 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,023✔
50
            $this->schemaFactory->setSchemaFactory($this);
1,023✔
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);
162✔
60
        if ('jsonhal' !== $format) {
162✔
61
            return $schema;
132✔
62
        }
63

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

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

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

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

133
            return $schema;
45✔
134
        }
135

UNCOV
136
        return $schema;
12✔
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

© 2025 Coveralls, Inc