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

api-platform / core / 7275126587

20 Dec 2023 12:09PM UTC coverage: 37.298% (+0.04%) from 37.262%
7275126587

push

github

soyuka
Merge 3.2

24 of 134 new or added lines in 24 files covered. (17.91%)

4 existing lines in 2 files now uncovered.

10338 of 27717 relevant lines covered (37.3%)

28.58 hits per line

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

98.41
/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
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
        $this->addDistinctFormat('jsonhal');
162✔
50
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
162✔
NEW
51
            $this->schemaFactory->setSchemaFactory($this);
×
52
        }
53
    }
54

55
    /**
56
     * {@inheritdoc}
57
     */
58
    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
59
    {
60
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
96✔
61
        if ('jsonhal' !== $format) {
96✔
62
            return $schema;
84✔
63
        }
64

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

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

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

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

124
            return $schema;
24✔
125
        }
126

127
        return $schema;
20✔
128
    }
129

130
    public function addDistinctFormat(string $format): void
131
    {
132
        if (method_exists($this->schemaFactory, 'addDistinctFormat')) {
162✔
133
            $this->schemaFactory->addDistinctFormat($format);
162✔
134
        }
135
    }
136
}
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