1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Descriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\PhpApi\Descriptions\Description\Schema; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\ComplexType; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Represents standard JSON Schema but with support for complex types |
15
|
|
|
* |
16
|
|
|
* @author John Kleijn <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ObjectSchema extends Schema |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \stdClass|null |
22
|
|
|
*/ |
23
|
|
|
protected $propertySchemas; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ComplexType |
27
|
|
|
*/ |
28
|
|
|
protected $complexType; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string|null |
32
|
|
|
*/ |
33
|
|
|
protected $xType; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string|null |
37
|
|
|
*/ |
38
|
|
|
protected $xRefId; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* ObjectSchema constructor. |
42
|
|
|
* |
43
|
|
|
* @param \stdClass $definition |
44
|
|
|
* @param null|\stdClass $propertySchemas |
45
|
|
|
* @param ComplexType $complexType |
46
|
|
|
* @param null|string $xType |
47
|
|
|
* @param null|string $xRefId |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
\stdClass $definition, |
51
|
|
|
$propertySchemas = null, |
52
|
|
|
ComplexType $complexType = null, |
53
|
|
|
$xType = null, |
54
|
|
|
$xRefId = null |
55
|
|
|
) { |
56
|
|
|
parent::__construct($definition); |
57
|
|
|
|
58
|
|
|
$this->propertySchemas = $propertySchemas; |
59
|
|
|
$this->complexType = $complexType; |
60
|
|
|
$this->xType = $xType; |
61
|
|
|
$this->xRefId = $xRefId; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return \stdClass |
66
|
|
|
*/ |
67
|
|
|
public function getPropertySchemas(): \stdClass |
68
|
|
|
{ |
69
|
|
|
return $this->propertySchemas; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name |
74
|
|
|
* |
75
|
|
|
* @return Schema |
76
|
|
|
*/ |
77
|
|
|
public function getPropertySchema(string $name): Schema |
78
|
|
|
{ |
79
|
|
|
if (!isset($this->propertySchemas->$name)) { |
80
|
|
|
throw new \OutOfBoundsException("Property '$name' does not exist"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->propertySchemas->$name; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $name |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function hasPropertySchema(string $name): bool |
92
|
|
|
{ |
93
|
|
|
return isset($this->propertySchemas->$name); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param ComplexType $complexType |
98
|
|
|
* |
99
|
|
|
* @return Schema |
100
|
|
|
*/ |
101
|
|
|
public function setComplexType(ComplexType $complexType): Schema |
102
|
|
|
{ |
103
|
|
|
if ($this->complexType && $this->complexType !== $complexType) { |
104
|
|
|
throw new \LogicException("Cannot change complex type of schema"); |
105
|
|
|
} |
106
|
|
|
$this->complexType = $complexType; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param ComplexType $type |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function isComplexType(ComplexType $type): bool |
117
|
|
|
{ |
118
|
|
|
return $type === $this->complexType; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function hasComplexType(): bool |
125
|
|
|
{ |
126
|
|
|
return null !== $this->complexType; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return ComplexType|null |
131
|
|
|
*/ |
132
|
|
|
public function getComplexType() |
133
|
|
|
{ |
134
|
|
|
return $this->complexType; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string|null |
139
|
|
|
*/ |
140
|
|
|
public function getXType() |
141
|
|
|
{ |
142
|
|
|
return $this->xType; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $xType |
147
|
|
|
*/ |
148
|
|
|
public function setXType(string $xType): void |
149
|
|
|
{ |
150
|
|
|
$this->xType = $xType; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return string|null |
155
|
|
|
*/ |
156
|
|
|
public function getXRefId() |
157
|
|
|
{ |
158
|
|
|
return $this->xRefId; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string|ComplexType $type |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function isType($type): bool |
167
|
|
|
{ |
168
|
|
|
if ($type instanceof ComplexType) { |
169
|
|
|
return $this->isComplexType($type); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->isPrimitive($type); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|