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. |
18
|
|
|
* |
19
|
|
|
* @see https://json-schema.org/latest/json-schema-core.html |
20
|
|
|
* |
21
|
|
|
* @author Kévin Dunglas <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class Schema |
24
|
|
|
{ |
25
|
|
|
public const VERSION_JSON_SCHEMA = 'json-schema'; |
26
|
|
|
public const VERSION_SWAGGER = 'swagger'; |
27
|
|
|
public const VERSION_OPENAPI = 'openapi'; |
28
|
|
|
|
29
|
|
|
private $version; |
30
|
|
|
private $schema; |
31
|
|
|
private $definitions; |
32
|
|
|
private $rootDefinitionId; |
33
|
|
|
|
34
|
|
|
public function __construct(string $version = self::VERSION_JSON_SCHEMA, ?\ArrayObject $baseSchema = null, ?string $rootDefinitionId = null, ?\ArrayObject $baseDefinitions = null) |
35
|
|
|
{ |
36
|
|
|
$this->version = $version; |
37
|
|
|
$this->schema = $baseSchema ?? new \ArrayObject(); |
38
|
|
|
$this->rootDefinitionId = $rootDefinitionId; |
39
|
|
|
|
40
|
|
|
if (null !== $baseDefinitions) { |
41
|
|
|
$this->definitions = $baseDefinitions; |
42
|
|
|
} elseif (self::VERSION_OPENAPI === $version && isset($this->schema['components']['schemas'])) { |
43
|
|
|
$this->definitions = $this->schema['components']['schemas'] instanceof \ArrayObject ? $this->schema['components']['schemas'] : new \ArrayObject(); |
44
|
|
|
} elseif (isset($this->schema['definitions'])) { |
45
|
|
|
$this->definitions = $this->schema['definitions'] instanceof \ArrayObject ? $this->schema['definitions'] : new \ArrayObject(); |
46
|
|
|
} else { |
47
|
|
|
$this->definitions = new \ArrayObject(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getVersion(): string |
52
|
|
|
{ |
53
|
|
|
return $this->version; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getSchema(): \ArrayObject |
57
|
|
|
{ |
58
|
|
|
if (self::VERSION_JSON_SCHEMA === $this->version) { |
59
|
|
|
$this->schema['$schema'] = 'http://json-schema.org/draft-07/schema#'; |
60
|
|
|
$this->schema['$ref'] = '#/definitions/'.$this->rootDefinitionId; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (self::VERSION_OPENAPI === $this->version) { |
64
|
|
|
$this->schema['components']['schemas'] = $this->definitions; |
65
|
|
|
} else { |
66
|
|
|
$this->schema['definitions'] = $this->definitions; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->schema; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @see https://json-schema.org/latest/json-schema-core.html#rfc.section.8.2.4 |
74
|
|
|
* |
75
|
|
|
* @internal |
76
|
|
|
*/ |
77
|
|
|
public function getDefinitions(): \ArrayObject |
78
|
|
|
{ |
79
|
|
|
return $this->definitions; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getRootDefinitionId(): ?string |
83
|
|
|
{ |
84
|
|
|
return $this->rootDefinitionId; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|