forked from martin-helmich/php-schema2class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.php
More file actions
203 lines (172 loc) · 5.71 KB
/
User.php
File metadata and controls
203 lines (172 loc) · 5.71 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
<?php
declare(strict_types=1);
namespace Example\Basic;
class User
{
/**
* Schema used to validate input for creating instances of this class
*
* @var array
*/
private static array $_schema = ['type' => 'object', 'required' => ['name', 'status'], 'properties' => ['name' => ['type' => 'string'], 'address' => ['$ref' => '#/definitions/Address'], 'status' => ['anyOf' => [['enum' => ['customer', 'manager'], 'type' => 'string'], ['type' => 'null']]]], 'definitions' => ['Address' => ['type' => 'object', 'properties' => ['street' => ['type' => 'string'], 'house' => ['type' => 'integer']]]]];
private string $name;
private ?Address $address = null;
/**
* @var 'customer'|'manager'|null
*/
private ?string $status;
/**
* @param 'customer'|'manager'|null $status
*/
public function __construct(string $name, ?string $status, ?Address $address = null)
{
$this->name = $name;
$this->status = $status;
$this->address = $address;
}
/**
* Name of the user - required field.
*/
public function getName(): string
{
return $this->name;
}
/**
* Name of the user - required field.
*/
public function withName(string $name): self
{
$clone = clone $this;
$clone->name = $name;
return $clone;
}
/**
* Object representing address of the user, field is optional.
*/
public function getAddress(): ?Address
{
return $this->address;
}
/**
* Object representing address of the user, field is optional.
*/
public function withAddress(Address $address): self
{
$clone = clone $this;
$clone->address = $address;
return $clone;
}
public function withoutAddress(): self
{
$clone = clone $this;
unset($clone->address);
return $clone;
}
/**
* User status. Field is obligatory, but nullable.
*
* If target PHP is 8.1+ the type will be an `enum` with cases `CUSTOMER = 'customer'` and `MANAGER = 'manager'`
*
* @return 'customer'|'manager'|null
*/
public function getStatus(): ?string
{
return $this->status;
}
/**
* User status. Field is obligatory, but nullable.
*
* If target PHP is 8.1+ the type will be an `enum` with cases `CUSTOMER = 'customer'` and `MANAGER = 'manager'`
*
* @param 'customer'|'manager'|null $status
*/
public function withStatus(?string $status, bool $validate = true): self
{
if ($validate) {
$validator = new \JsonSchema\Validator();
$validator->validate($status, self::$_schema['properties']['status']);
if (!$validator->isValid()) {
throw new \InvalidArgumentException($validator->getErrors()[0]['message']);
}
}
$clone = clone $this;
$clone->status = $status;
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 User Created instance
* @throws \InvalidArgumentException
*/
public static function fromInput($input, bool $validate = true): User
{
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);
}
$name = $input->{'name'};
$status = ($input->{'status'} !== null ? $input->{'status'} : null);
$address = isset($input->{'address'}) ? Address::fromInput($input->{'address'}, $validate) : null;
$obj = new self($name, $status, $address);
return $obj;
}
/**
* Converts this object back to a simple array that can be JSON-serialized
*
* @return array Converted array
*/
public function toArray(): array
{
$output = [];
$output['name'] = $this->name;
if (isset($this->address)) {
$output['address'] = $this->address->toArray();
}
$output['status'] = $this->status;
return $output;
}
/**
* Converts this object to a stdClass that can be JSON-serialized
*
* @return \stdClass Converted object
*/
public function toStdClass(): \stdClass
{
$output = new \stdClass();
$output->{'name'} = $this->name;
if (isset($this->address)) {
$output->{'address'} = $this->address->toStdClass();
}
$output->{'status'} = $this->status;
return $output;
}
/**
* Validates an input array
*
* @param array|object $input Input data
* @param bool $return Return instead of throwing errors
* @return bool Validation result
* @throws \InvalidArgumentException
*/
public static function validateInput($input, bool $return = false): bool
{
$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): string {
return ($e["property"] ? $e["property"] . ": " : "") . $e["message"];
}, $validator->getErrors());
throw new \InvalidArgumentException(join(".\n", $errors));
}
return $validator->isValid();
}
}