1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
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\Core\JsonSchema; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Represents a JSON Schema document. |
18
|
|
|
* |
19
|
|
|
* Both the standard version and the OpenAPI flavors (v2 and v3) are supported. |
20
|
|
|
* |
21
|
|
|
* @see https://json-schema.org/latest/json-schema-core.html |
22
|
|
|
* @see https://github.com/OAI/OpenAPI-Specification |
23
|
|
|
* |
24
|
|
|
* @experimental |
25
|
|
|
* |
26
|
|
|
* @author Kévin Dunglas <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class Schema extends \ArrayObject |
29
|
|
|
{ |
30
|
|
|
public const VERSION_JSON_SCHEMA = 'json-schema'; |
31
|
|
|
public const VERSION_SWAGGER = 'swagger'; |
32
|
|
|
public const VERSION_OPENAPI = 'openapi'; |
33
|
|
|
|
34
|
|
|
private $version; |
35
|
|
|
|
36
|
|
|
public function __construct(string $version = self::VERSION_JSON_SCHEMA) |
37
|
|
|
{ |
38
|
|
|
$this->version = $version; |
39
|
|
|
|
40
|
|
|
parent::__construct(self::VERSION_JSON_SCHEMA === $this->version ? ['$schema' => 'http://json-schema.org/draft-07/schema#'] : []); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The flavor used for this document: JSON Schema, OpenAPI v2 or OpenAPI v3. |
45
|
|
|
*/ |
46
|
|
|
public function getVersion(): string |
47
|
|
|
{ |
48
|
|
|
return $this->version; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param bool $includeDefinitions if set to false, definitions will not be included in the resulting array |
53
|
|
|
*/ |
54
|
|
|
public function getArrayCopy(bool $includeDefinitions = true): array |
55
|
|
|
{ |
56
|
|
|
$schema = parent::getArrayCopy(); |
57
|
|
|
|
58
|
|
|
if (!$includeDefinitions) { |
59
|
|
|
unset($schema['definitions'], $schema['components']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $schema; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieves the definitions used by this schema. |
67
|
|
|
*/ |
68
|
|
|
public function getDefinitions(): \ArrayObject |
69
|
|
|
{ |
70
|
|
|
$definitions = $this['definitions'] ?? $this['components']['schemas'] ?? new \ArrayObject(); |
71
|
|
|
$this->setDefinitions($definitions); |
72
|
|
|
|
73
|
|
|
return $definitions; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Associates existing definitions to this schema. |
78
|
|
|
*/ |
79
|
|
|
public function setDefinitions(\ArrayObject $definitions): void |
80
|
|
|
{ |
81
|
|
|
if (self::VERSION_OPENAPI === $this->version) { |
82
|
|
|
$this['components']['schemas'] = $definitions; |
83
|
|
|
|
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this['definitions'] = $definitions; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Checks if this schema is initialized. |
92
|
|
|
*/ |
93
|
|
|
public function isDefined(): bool |
94
|
|
|
{ |
95
|
|
|
return isset($this['$ref']) || isset($this['type']); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|