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

api-platform / core / 10638675968

30 Aug 2024 08:19PM UTC coverage: 74.332% (+10.4%) from 63.901%
10638675968

push

github

web-flow
chore: deprecations (#6563)

* chore: deprecations

* chore: deprecations

5 of 5 new or added lines in 3 files covered. (100.0%)

100 existing lines in 9 files now uncovered.

3226 of 4340 relevant lines covered (74.33%)

77.04 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) {
524✔
50
            $this->schemaFactory->setSchemaFactory($this);
524✔
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);
188✔
60
        if ('jsonhal' !== $format) {
188✔
61
            return $schema;
148✔
62
        }
63

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

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

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

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

133
            return $schema;
44✔
134
        }
135

136
        return $schema;
28✔
137
    }
138

139
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
140
    {
UNCOV
141
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
UNCOV
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