|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of FlexPHP. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Freddie Gar <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace FlexPHP\Schema; |
|
11
|
|
|
|
|
12
|
|
|
use FlexPHP\Schema\Constants\Keyword; |
|
13
|
|
|
use FlexPHP\Schema\Constants\Rule; |
|
14
|
|
|
use FlexPHP\Schema\Validations\SchemaAttributeValidation; |
|
15
|
|
|
|
|
16
|
|
|
final class SchemaAttribute implements SchemaAttributeInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $name; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $dataType; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array<string, mixed> |
|
30
|
|
|
*/ |
|
31
|
|
|
private $constraints = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param mixed $constraints |
|
35
|
|
|
*/ |
|
36
|
74 |
|
public function __construct(string $name, string $dataType, $constraints = null) |
|
37
|
|
|
{ |
|
38
|
74 |
|
$this->setName($name); |
|
39
|
74 |
|
$this->setDataType($dataType); |
|
40
|
74 |
|
$this->setConstraints($constraints); |
|
41
|
|
|
|
|
42
|
74 |
|
$this->validate(); |
|
43
|
71 |
|
} |
|
44
|
|
|
|
|
45
|
74 |
|
public function name(): string |
|
46
|
|
|
{ |
|
47
|
74 |
|
return $this->name; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
74 |
|
public function dataType(): string |
|
51
|
|
|
{ |
|
52
|
74 |
|
return $this->dataType; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
74 |
|
public function constraints(): array |
|
56
|
|
|
{ |
|
57
|
74 |
|
return $this->constraints; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
6 |
|
public function type(): ?string |
|
61
|
|
|
{ |
|
62
|
6 |
|
return $this->constraints[Rule::TYPE] ?? null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
11 |
|
public function isRequired(): bool |
|
66
|
|
|
{ |
|
67
|
11 |
|
return (bool)($this->constraints[Rule::REQUIRED] ?? false); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
10 |
|
public function minLength(): ?int |
|
71
|
|
|
{ |
|
72
|
10 |
|
return $this->constraints[Rule::MINLENGTH] ?? null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
10 |
|
public function maxLength(): ?int |
|
76
|
|
|
{ |
|
77
|
10 |
|
return $this->constraints[Rule::MAXLENGTH] ?? null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
10 |
|
public function minCheck(): ?int |
|
81
|
|
|
{ |
|
82
|
10 |
|
return $this->constraints[Rule::MINCHECK] ?? null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
10 |
|
public function maxCheck(): ?int |
|
86
|
|
|
{ |
|
87
|
10 |
|
return $this->constraints[Rule::MAXCHECK] ?? null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
8 |
|
public function min(): ?int |
|
91
|
|
|
{ |
|
92
|
8 |
|
return $this->constraints[Rule::MIN] ?? null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
8 |
|
public function max(): ?int |
|
96
|
|
|
{ |
|
97
|
8 |
|
return $this->constraints[Rule::MAX] ?? null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
6 |
|
public function equalTo(): ?string |
|
101
|
|
|
{ |
|
102
|
6 |
|
return $this->constraints[Rule::EQUALTO] ?? null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
12 |
|
public function isPk(): bool |
|
106
|
|
|
{ |
|
107
|
12 |
|
return (bool)($this->constraints[Rule::PK] ?? false); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
9 |
|
public function isAi(): bool |
|
111
|
|
|
{ |
|
112
|
9 |
|
return (bool)($this->constraints[Rule::AI] ?? false); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
10 |
|
public function isFk(): bool |
|
116
|
|
|
{ |
|
117
|
10 |
|
return (bool)($this->constraints[Rule::FK] ?? false); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
10 |
|
public function fkTable(): ?string |
|
121
|
|
|
{ |
|
122
|
10 |
|
return $this->constraints[Rule::FK]['table'] ?? null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
10 |
|
public function fkId(): ?string |
|
126
|
|
|
{ |
|
127
|
10 |
|
return $this->constraints[Rule::FK]['id'] ?? null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
10 |
|
public function fkName(): ?string |
|
131
|
|
|
{ |
|
132
|
10 |
|
return $this->constraints[Rule::FK]['name'] ?? null; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
74 |
|
public function properties(): array |
|
136
|
|
|
{ |
|
137
|
|
|
return [ |
|
138
|
74 |
|
Keyword::NAME => $this->name(), |
|
139
|
74 |
|
Keyword::DATATYPE => $this->dataType(), |
|
140
|
74 |
|
Keyword::CONSTRAINTS => $this->constraints(), |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
74 |
|
private function validate(): void |
|
145
|
|
|
{ |
|
146
|
74 |
|
(new SchemaAttributeValidation($this->properties()))->validate(); |
|
147
|
71 |
|
} |
|
148
|
|
|
|
|
149
|
74 |
|
private function setName(string $name): void |
|
150
|
|
|
{ |
|
151
|
74 |
|
$this->name = $name; |
|
152
|
74 |
|
} |
|
153
|
|
|
|
|
154
|
74 |
|
private function setDataType(string $dataType): void |
|
155
|
|
|
{ |
|
156
|
74 |
|
$this->dataType = $dataType; |
|
157
|
74 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param mixed $constraints |
|
161
|
|
|
*/ |
|
162
|
74 |
|
private function setConstraints($constraints): void |
|
163
|
|
|
{ |
|
164
|
74 |
|
if (!empty($constraints)) { |
|
165
|
57 |
|
if (\is_string($constraints)) { |
|
166
|
27 |
|
$this->setConstraintsFromString($constraints); |
|
167
|
|
|
} else { |
|
168
|
30 |
|
$this->setConstraintsFromArray($constraints); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
74 |
|
} |
|
172
|
|
|
|
|
173
|
27 |
|
private function setConstraintsFromString(string $constraints): void |
|
174
|
|
|
{ |
|
175
|
27 |
|
$this->setConstraintsFromArray($this->getConstraintsFromString($constraints)); |
|
176
|
27 |
|
} |
|
177
|
|
|
|
|
178
|
57 |
|
private function setConstraintsFromArray(array $constraints): void |
|
179
|
|
|
{ |
|
180
|
57 |
|
$this->constraints = $this->getConstraintsCast($constraints); |
|
181
|
57 |
|
} |
|
182
|
|
|
|
|
183
|
27 |
|
private function getConstraintsFromString(string $constraints): array |
|
184
|
|
|
{ |
|
185
|
27 |
|
$_constraints = \explode('|', $constraints); |
|
186
|
|
|
|
|
187
|
|
|
/** @var mixed $_constraint */ |
|
188
|
27 |
|
foreach ($_constraints as $index => $_constraint) { |
|
189
|
27 |
|
$_rule = \explode(':', $_constraint); |
|
190
|
|
|
|
|
191
|
27 |
|
if (\count($_rule) === 2) { |
|
192
|
24 |
|
[$_name, $_options] = $_rule; |
|
193
|
|
|
|
|
194
|
24 |
|
if (Rule::FK !== $_name && \strpos($_options, ',') !== false) { // Range |
|
195
|
2 |
|
[$min, $max] = \explode(',', $_options); |
|
196
|
2 |
|
$_options = \compact('min', 'max'); |
|
197
|
22 |
|
} elseif (\preg_match('/^false$/i', $_options)) { // False as string |
|
198
|
6 |
|
$_options = false; |
|
199
|
19 |
|
} elseif (\preg_match('/^true$/i', $_options)) { // True as string |
|
200
|
7 |
|
$_options = true; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
24 |
|
$_constraints[$_name] = $_options; |
|
204
|
|
|
} else { |
|
205
|
7 |
|
$_constraints[$_rule[0]] = true; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
27 |
|
unset($_constraints[$index]); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
27 |
|
return $_constraints; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
57 |
|
private function getConstraintsCast(array $constraints): array |
|
215
|
|
|
{ |
|
216
|
57 |
|
foreach ($constraints as $name => $value) { |
|
217
|
57 |
|
if (\is_int($name)) { |
|
218
|
5 |
|
$constraints[$value] = true; |
|
219
|
5 |
|
unset($constraints[$name]); |
|
220
|
54 |
|
} elseif ($name === Rule::CHECK || $name === Rule::LENGTH) { |
|
221
|
4 |
|
$constraints['min' . $name] = (int)$value['min']; |
|
222
|
4 |
|
$constraints['max' . $name] = (int)$value['max']; |
|
223
|
4 |
|
unset($constraints[$name]); |
|
224
|
50 |
|
} elseif ($name === Rule::FK && \is_string($value)) { |
|
225
|
9 |
|
$constraints[$name] = $this->getFkOptions($value); |
|
226
|
|
|
} else { |
|
227
|
57 |
|
$constraints[$name] = \is_numeric($value) ? (int)$value : $value; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
57 |
|
return $constraints; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
9 |
|
private function getFkOptions(string $constraint): array |
|
235
|
|
|
{ |
|
236
|
9 |
|
$_vars = \explode(',', $constraint); |
|
237
|
9 |
|
$fkName = 'name'; |
|
238
|
9 |
|
$fkId = 'id'; |
|
239
|
|
|
|
|
240
|
9 |
|
switch (\count($_vars)) { |
|
241
|
9 |
|
case 3: |
|
242
|
3 |
|
[$fkTable, $fkName, $fkId] = $_vars; |
|
243
|
|
|
|
|
244
|
3 |
|
break; |
|
245
|
6 |
|
case 2: |
|
246
|
4 |
|
[$fkTable, $fkName] = $_vars; |
|
247
|
|
|
|
|
248
|
4 |
|
break; |
|
249
|
|
|
default: |
|
250
|
2 |
|
[$fkTable] = $_vars; |
|
251
|
|
|
|
|
252
|
2 |
|
break; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
return [ |
|
256
|
9 |
|
'table' => $fkTable, |
|
257
|
9 |
|
'name' => $fkName, |
|
258
|
9 |
|
'id' => $fkId, |
|
259
|
|
|
]; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
11 |
|
public function typeHint(): string |
|
263
|
|
|
{ |
|
264
|
|
|
$typeHintByDataType = [ |
|
265
|
11 |
|
'smallint' => 'int', |
|
266
|
|
|
'integer' => 'int', |
|
267
|
|
|
'float' => 'float', |
|
268
|
|
|
'double' => 'float', |
|
269
|
|
|
'bool' => 'bool', |
|
270
|
|
|
'boolean' => 'bool', |
|
271
|
|
|
'date' => '\DateTime', |
|
272
|
|
|
'date_immutable' => '\DateTimeImmutable', |
|
273
|
|
|
'datetime' => '\DateTime', |
|
274
|
|
|
'datetime_immutable' => '\DateTimeImmutable', |
|
275
|
|
|
'datetimetz' => '\DateTime', |
|
276
|
|
|
'datetimetz_immutable' => '\DateTimeImmutable', |
|
277
|
|
|
'time' => '\DateTime', |
|
278
|
|
|
'time_immutable' => '\DateTimeImmutable', |
|
279
|
|
|
'array' => 'array', |
|
280
|
|
|
'simple_array' => 'array', |
|
281
|
|
|
'json_array' => 'array', |
|
282
|
|
|
]; |
|
283
|
|
|
|
|
284
|
11 |
|
if (isset($typeHintByDataType[$this->dataType()])) { |
|
285
|
8 |
|
return $typeHintByDataType[$this->dataType()]; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
3 |
|
return 'string'; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|