forked from martin-helmich/php-schema2class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyClass.php
More file actions
310 lines (271 loc) · 8.28 KB
/
Copy pathMyClass.php
File metadata and controls
310 lines (271 loc) · 8.28 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace Ns\ObjectWithoutPropsUnion_5_6;
class MyClass
{
/**
* Schema used to validate input for creating instances of this class
*
* @var array
*/
private static $_schema = [
'properties' => [
'foo' => [
'type' => [
'string',
'object',
],
],
'bar' => [
'type' => [
'string',
'object',
],
],
],
'required' => [
'foo',
],
];
/**
* Mapping of schema property names to this class's property names.
*
* @var array
*/
private static $_namesMap = [
'foo' => 'foo',
'bar' => 'bar',
];
/**
* Map of name/value pairs for properties not specified in the schema.
*
* @var \stdClass
*/
private $_additionalProperties;
/**
* @var string|array|object
*/
private $foo;
/**
* @var string|array|object|null
*/
private $bar = null;
/**
* @param string|array|object $foo
* @param string|array|object|null $bar
*/
public function __construct($foo, $bar = null)
{
$this->_additionalProperties = new \stdClass();
$this->foo = $foo;
$this->bar = $bar;
}
/**
* Object (`stdClass`) or array with name/value pairs for properties not specified in the schema.
*
* @param bool $asArray Whether return an associative array instead of `stdClass` object.
* @return array|\stdClass
*/
public function getAdditionalProperties($asArray = true)
{
return $asArray
? json_decode(json_encode($this->_additionalProperties), true)
: $this->_additionalProperties;
}
/**
* Allows adding properties not specified in the schema.
*
* @param \stdClass|array $additionalProperties Map of property name/value pairs to add.
* @return self
*/
public function withAdditionalProperties($additionalProperties)
{
$clone = clone $this;
$clone->_additionalProperties = is_array($additionalProperties)
? \JsonSchema\Validator::arrayToObjectRecursive($additionalProperties)
: $additionalProperties;
return $clone;
}
/**
* Removes all extra properties not specified in the schema.
*
* @return self
*/
public function withoutAdditionalProperties()
{
$clone = clone $this;
$clone->_additionalProperties = new \stdClass();
return $clone;
}
/**
* @return string|array|object
*/
public function getFoo()
{
return $this->foo;
}
/**
* @param string|array|object $foo
* @param bool $validate
* @return self
*/
public function withFoo($foo, $validate = true)
{
if ($validate) {
$validator = new \JsonSchema\Validator();
$validator->validate($foo, self::$_schema['properties']['foo']);
if (!$validator->isValid()) {
throw new \InvalidArgumentException($validator->getErrors()[0]['message']);
}
}
$clone = clone $this;
$clone->foo = $foo;
return $clone;
}
/**
* @return string|array|object|null
*/
public function getBar()
{
return isset($this->bar) ? $this->bar : null;
}
/**
* @param string|array|object $bar
* @param bool $validate
* @return self
*/
public function withBar($bar, $validate = true)
{
if ($validate) {
$validator = new \JsonSchema\Validator();
$validator->validate($bar, self::$_schema['properties']['bar']);
if (!$validator->isValid()) {
throw new \InvalidArgumentException($validator->getErrors()[0]['message']);
}
}
$clone = clone $this;
$clone->bar = $bar;
return $clone;
}
/**
* @return self
*/
public function withoutBar()
{
$clone = clone $this;
unset($clone->bar);
return $clone;
}
/**
* Builds a new instance from an input array or object
*
* @param array|object $input Input data
* @param bool $validate If `false`, validation against the schema will be skipped.
* @return MyClass Created instance
* @throws \InvalidArgumentException
*/
public static function fromInput($input, $validate = true)
{
if (!is_array($input) && !is_object($input)) {
throw new \InvalidArgumentException(
'Input to fromInput must be array or object, got ' . gettype($input)
);
}
$input = is_array($input) ? \JsonSchema\Validator::arrayToObjectRecursive($input) : $input;
if ($validate) {
static::validateInput($input);
}
$foo = $input->{'foo'};
$bar = isset($input->{'bar'})
? ((is_string($input->{'bar'}) || is_object($input->{'bar'})) ? $input->{'bar'} : null)
: null;
$obj = new self($foo, $bar);
$_additionalProperties = array_diff_key(get_object_vars($input), self::$_namesMap);
if (!empty($_additionalProperties)) {
$obj->_additionalProperties = (object) $_additionalProperties;
}
return $obj;
}
/**
* Converts this object to array that can be JSON-serialized
*
* @return array Converted array
*/
public function toArray()
{
$output = json_decode(json_encode($this->_additionalProperties), true);
if (is_string($this->foo)) {
$output['foo'] = $this->foo;
} elseif (is_object($this->foo)) {
$output['foo'] = json_decode(json_encode($this->foo), true);
}
if (isset($this->bar)) {
if (is_string($this->bar)) {
$output['bar'] = $this->bar;
} elseif (is_object($this->bar)) {
$output['bar'] = json_decode(json_encode($this->bar), true);
}
}
return $output;
}
/**
* Converts this object to a stdClass that can be JSON-serialized
*
* @return \stdClass Converted object
*/
public function toStdClass()
{
$output = $this->_additionalProperties;
if (is_string($this->foo)) {
$output->{'foo'} = $this->foo;
} elseif (is_object($this->foo)) {
$output->{'foo'} = json_decode(json_encode($this->foo));
}
if (isset($this->bar)) {
if (is_string($this->bar)) {
$output->{'bar'} = $this->bar;
} elseif (is_object($this->bar)) {
$output->{'bar'} = json_decode(json_encode($this->bar));
}
}
return $output;
}
/**
* Validates the current instance against its schema
*
* @param bool $return Return instead of throwing errors
* @return bool Validation result if `$return` is `true`
* @throws \InvalidArgumentException
*/
public function validate($return = false)
{
return self::validateInput($this->toStdClass(), $return);
}
/**
* Validates an input array
*
* @param array|object $input Input data
* @param bool $return Return instead of throwing errors
* @return bool Validation result if `$return` is `true`
* @throws \InvalidArgumentException
*/
public static function validateInput($input, $return = false)
{
$validator = new \JsonSchema\Validator();
$input = is_array($input) ? \JsonSchema\Validator::arrayToObjectRecursive($input) : $input;
$validator->validate($input, self::$_schema);
if (!$validator->isValid() && !$return) {
$errors = array_map(function(array $e) {
return ($e["property"] ? $e["property"] . ": " : "") . $e["message"];
}, $validator->getErrors());
throw new \InvalidArgumentException(join(".\n", $errors));
}
return $validator->isValid();
}
public function __clone()
{
$this->foo = ((is_string($this->foo) || is_object($this->foo)) ? $this->foo : $this->foo);
if (isset($this->bar)) {
$this->bar = ((is_string($this->bar) || is_object($this->bar)) ? $this->bar : $this->bar);
}
}
}