-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Schema.php
129 lines (108 loc) · 3.35 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\JsonSchema;
/**
* Represents a JSON Schema document.
*
* Both the standard version and the OpenAPI flavors (v2 and v3) are supported.
*
* @see https://json-schema.org/latest/json-schema-core.html
* @see https://github.com/OAI/OpenAPI-Specification
*
* @author Kévin Dunglas <[email protected]>
*/
final class Schema extends \ArrayObject
{
public const TYPE_INPUT = 'input';
public const TYPE_OUTPUT = 'output';
public const VERSION_JSON_SCHEMA = 'json-schema';
public const VERSION_OPENAPI = 'openapi';
public const VERSION_SWAGGER = 'swagger';
public function __construct(private readonly string $version = self::VERSION_JSON_SCHEMA)
{
parent::__construct(self::VERSION_JSON_SCHEMA === $this->version ? ['$schema' => 'http://json-schema.org/draft-07/schema#'] : []);
}
/**
* The flavor used for this document: JSON Schema, OpenAPI v2 or OpenAPI v3.
*/
public function getVersion(): string
{
return $this->version;
}
/**
* {@inheritdoc}
*
* @param bool $includeDefinitions if set to false, definitions will not be included in the resulting array
*/
public function getArrayCopy(bool $includeDefinitions = true): array
{
$schema = parent::getArrayCopy();
if (!$includeDefinitions) {
unset($schema['definitions'], $schema['components']);
}
return $schema;
}
/**
* Retrieves the definitions used by this schema.
*/
public function getDefinitions(): \ArrayObject
{
$definitions = $this['definitions'] ?? $this['components']['schemas'] ?? new \ArrayObject();
$this->setDefinitions($definitions);
return $definitions;
}
/**
* Associates existing definitions to this schema.
*/
public function setDefinitions(\ArrayObject $definitions): void
{
if (self::VERSION_OPENAPI === $this->version) {
$this['components']['schemas'] = $definitions;
return;
}
$this['definitions'] = $definitions;
}
/**
* Returns the name of the root definition, if defined.
*/
public function getRootDefinitionKey(): ?string
{
if (!isset($this['$ref'])) {
return null;
}
return $this->removeDefinitionKeyPrefix($this['$ref']);
}
/**
* Returns the name of the items definition, if defined.
*/
public function getItemsDefinitionKey(): ?string
{
$ref = $this['items']['$ref'] ?? null;
if (null === $ref) {
return null;
}
return $this->removeDefinitionKeyPrefix($ref);
}
/**
* Checks if this schema is initialized.
*/
public function isDefined(): bool
{
return isset($this['$ref']) || isset($this['type']);
}
private function removeDefinitionKeyPrefix(string $definitionKey): string
{
// strlen('#/definitions/') = 14
// strlen('#/components/schemas/') = 21
$prefix = self::VERSION_OPENAPI === $this->version ? 21 : 14;
return substr($definitionKey, $prefix);
}
}