1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonSchema\Meta\MetaHolder; |
6
|
|
|
use Swaggest\JsonSchema\Structure\Nested; |
7
|
|
|
|
8
|
|
|
class Wrapper implements SchemaContract, MetaHolder, SchemaExporter, \JsonSerializable |
9
|
|
|
{ |
10
|
|
|
/** @var Schema */ |
11
|
|
|
private $schema; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Keeps reference to original instance for centralized Meta storage |
15
|
|
|
* @var Schema |
16
|
|
|
*/ |
17
|
|
|
private $originalSchema; |
18
|
|
|
|
19
|
|
|
public $objectItemClass; |
20
|
|
|
|
21
|
|
|
private $cloned = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ImmutableSchema constructor. |
25
|
|
|
* @param Schema $schema |
26
|
|
|
*/ |
27
|
10 |
|
public function __construct(Schema $schema) |
28
|
|
|
{ |
29
|
10 |
|
$this->schema = $schema; |
30
|
10 |
|
$this->originalSchema = $schema; |
31
|
10 |
|
$this->objectItemClass = $schema->objectItemClass; |
32
|
10 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param mixed $data |
36
|
|
|
* @param Context $options |
37
|
|
|
* @param string $path |
38
|
|
|
* @param mixed|null $result |
39
|
|
|
* @return array|mixed|null|object|\stdClass |
40
|
|
|
* @throws InvalidValue |
41
|
|
|
* @throws \Exception |
42
|
|
|
* @throws \Swaggest\JsonDiff\Exception |
43
|
|
|
*/ |
44
|
1578 |
|
public function process($data, Context $options, $path = '#', $result = null) |
45
|
|
|
{ |
46
|
1578 |
|
return $this->schema->process($data, $options, $path, $result); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param mixed $data |
51
|
|
|
* @param Context|null $options |
52
|
|
|
* @return array|mixed|null|object|\stdClass |
53
|
|
|
* @throws Exception |
54
|
|
|
* @throws InvalidValue |
55
|
|
|
*/ |
56
|
3239 |
|
public function in($data, Context $options = null) |
57
|
|
|
{ |
58
|
3239 |
|
return $this->schema->in($data, $options); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param mixed $data |
63
|
|
|
* @param Context|null $options |
64
|
|
|
* @return array|mixed|null|object|\stdClass |
65
|
|
|
* @throws InvalidValue |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
37 |
|
public function out($data, Context $options = null) |
69
|
|
|
{ |
70
|
37 |
|
return $this->schema->out($data, $options); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string[] |
75
|
|
|
*/ |
76
|
|
|
public function getPropertyNames() |
77
|
|
|
{ |
78
|
|
|
return $this->schema->getPropertyNames(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string[] |
83
|
|
|
*/ |
84
|
97 |
|
public function getNestedPropertyNames() |
85
|
|
|
{ |
86
|
97 |
|
return $this->schema->getNestedPropertyNames(); |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
public function nested() |
90
|
|
|
{ |
91
|
4 |
|
return new Nested($this); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return null|Constraint\Properties |
96
|
|
|
*/ |
97
|
101 |
|
public function getProperties() |
98
|
|
|
{ |
99
|
101 |
|
return $this->schema->properties; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $name |
104
|
|
|
* @return null|Schema|SchemaContract |
105
|
|
|
*/ |
106
|
5 |
|
public function getProperty($name) |
107
|
|
|
{ |
108
|
5 |
|
if ($name === Schema::CONST_PROPERTY || $name === Schema::DEFAULT_PROPERTY) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
return isset($this->schema->properties[$name]) ? $this->schema->properties[$name] : null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $name |
116
|
|
|
* @param array $arguments |
117
|
|
|
* @return mixed |
118
|
|
|
* @throws Exception |
119
|
|
|
*/ |
120
|
|
|
public function __call($name, $arguments) |
121
|
|
|
{ |
122
|
|
|
if (substr($name, 0, 3) === 'set') { |
123
|
|
|
if (!$this->cloned) { |
124
|
|
|
$this->schema = clone $this->schema; |
125
|
|
|
$this->cloned = true; |
126
|
|
|
} |
127
|
|
|
$this->schema->$name($arguments[0]); // todo performance check direct set |
128
|
|
|
return $this; |
129
|
|
|
} else { |
130
|
|
|
throw new Exception('Unknown method:' . $name); |
131
|
7 |
|
} |
132
|
|
|
} |
133
|
7 |
|
|
134
|
|
|
public function getDefault() |
135
|
|
|
{ |
136
|
|
|
return $this->schema->default; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
1 |
|
* @param mixed $default |
141
|
|
|
* @return $this |
142
|
1 |
|
*/ |
143
|
1 |
|
public function setDefault($default) |
144
|
1 |
|
{ |
145
|
|
|
if (!$this->cloned) { |
146
|
|
|
$this->schema = clone $this->schema; |
147
|
1 |
|
$this->cloned = true; |
148
|
1 |
|
} |
149
|
|
|
|
150
|
|
|
$this->schema->default = $default; |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $name |
156
|
|
|
* @throws Exception |
157
|
|
|
*/ |
158
|
|
|
public function __get($name) |
159
|
|
|
{ |
160
|
|
|
throw new Exception('Unexpected get: ' . $name); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $name |
165
|
|
|
* @param mixed $value |
166
|
|
|
* @return Wrapper |
167
|
|
|
*/ |
168
|
|
|
public function __set($name, $value) |
169
|
|
|
{ |
170
|
|
|
if (!$this->cloned) { |
171
|
|
|
$this->schema = clone $this->schema; |
172
|
|
|
$this->cloned = true; |
173
|
|
|
} |
174
|
|
|
$this->schema->$name = $value; |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $name |
180
|
|
|
* @throws Exception |
181
|
|
|
*/ |
182
|
|
|
public function __isset($name) |
183
|
|
|
{ |
184
|
|
|
throw new Exception('Unexpected isset: ' . $name); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function addMeta($meta, $name = null) |
188
|
|
|
{ |
189
|
|
|
$this->originalSchema->addMeta($meta, $name); |
190
|
1 |
|
return $this; |
191
|
|
|
} |
192
|
1 |
|
|
193
|
|
|
public function getMeta($name) |
194
|
|
|
{ |
195
|
|
|
return $this->originalSchema->getMeta($name); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
5 |
|
* @param Context|null $options |
200
|
|
|
* @return Structure\ObjectItemContract |
201
|
5 |
|
*/ |
202
|
|
|
public function makeObjectItem(Context $options = null) |
203
|
|
|
{ |
204
|
5 |
|
return $this->schema->makeObjectItem($options); |
205
|
|
|
} |
206
|
5 |
|
|
207
|
|
|
public function getObjectItemClass() |
208
|
|
|
{ |
209
|
3 |
|
return $this->objectItemClass; |
210
|
|
|
} |
211
|
3 |
|
|
212
|
|
|
public function exportSchema() |
213
|
|
|
{ |
214
|
|
|
return clone $this->schema; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function jsonSerialize() |
218
|
|
|
{ |
219
|
|
|
return $this->schema->jsonSerialize(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|