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\Exception\InvalidSchemaAttributeException; |
15
|
|
|
use FlexPHP\Schema\Validations\SchemaAttributeValidation; |
16
|
|
|
|
17
|
|
|
class SchemaAttribute implements SchemaAttributeInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array<string> |
21
|
|
|
*/ |
22
|
|
|
private $properties = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $dataType; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array<mixed> |
36
|
|
|
*/ |
37
|
|
|
private $constraints; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $type; |
43
|
|
|
|
44
|
11 |
|
public function __construct(array $properties = []) |
45
|
|
|
{ |
46
|
11 |
|
if (!empty($properties)) { |
47
|
4 |
|
foreach ($properties as $name => $property) { |
48
|
4 |
|
if (\method_exists($this, 'set' . $name)) { |
49
|
4 |
|
$this->{'set' . $name}($property); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
11 |
|
} |
54
|
|
|
|
55
|
11 |
|
public function validate(): void |
56
|
|
|
{ |
57
|
11 |
|
if (empty($this->properties)) { |
58
|
1 |
|
throw new InvalidSchemaAttributeException('Schema attribute is empty'); |
59
|
|
|
} |
60
|
|
|
|
61
|
10 |
|
(new SchemaAttributeValidation($this->properties))->validate(); |
62
|
6 |
|
} |
63
|
|
|
|
64
|
3 |
|
public function name(): string |
65
|
|
|
{ |
66
|
3 |
|
return $this->name; |
67
|
|
|
} |
68
|
|
|
|
69
|
10 |
|
public function setName(string $name): void |
70
|
|
|
{ |
71
|
10 |
|
$this->name = $name; |
72
|
10 |
|
$this->properties[Keyword::NAME] = $this->name; |
73
|
10 |
|
} |
74
|
|
|
|
75
|
3 |
|
public function dataType(): string |
76
|
|
|
{ |
77
|
3 |
|
return $this->dataType; |
78
|
|
|
} |
79
|
|
|
|
80
|
8 |
|
public function setDataType(string $dataType): void |
81
|
|
|
{ |
82
|
8 |
|
$this->dataType = $dataType; |
83
|
8 |
|
$this->properties[Keyword::DATATYPE] = $this->dataType; |
84
|
8 |
|
} |
85
|
|
|
|
86
|
2 |
|
public function constraints(): array |
87
|
|
|
{ |
88
|
2 |
|
return $this->constraints; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Constraints can be array or string |
93
|
|
|
* |
94
|
|
|
* @param mixed $constraints |
95
|
|
|
*/ |
96
|
5 |
|
public function setConstraints($constraints): void |
97
|
|
|
{ |
98
|
5 |
|
if (\is_string($constraints)) { |
99
|
4 |
|
$constraints = $this->getConstraintsFromString($constraints); |
100
|
|
|
} |
101
|
|
|
|
102
|
5 |
|
$this->constraints = $constraints; |
103
|
5 |
|
$this->properties[Keyword::CONSTRAINTS] = $this->constraints; |
104
|
5 |
|
} |
105
|
|
|
|
106
|
2 |
|
public function type(): string |
107
|
|
|
{ |
108
|
2 |
|
return $this->type; |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
public function setType(string $type): void |
112
|
|
|
{ |
113
|
2 |
|
$this->type = $type; |
114
|
2 |
|
$this->properties[Keyword::TYPE] = $this->type; |
115
|
2 |
|
} |
116
|
|
|
|
117
|
1 |
|
public function isRequired(): bool |
118
|
|
|
{ |
119
|
1 |
|
return (bool)($this->constraints[Rule::REQUIRED] ?? null); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
public function minLength(): ?int |
123
|
|
|
{ |
124
|
1 |
|
return isset($this->constraints[Rule::MINLENGTH]) |
125
|
1 |
|
? (int)$this->constraints[Rule::MINLENGTH] |
126
|
1 |
|
: null; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function maxLength(): ?int |
130
|
|
|
{ |
131
|
1 |
|
return isset($this->constraints[Rule::MAXLENGTH]) |
132
|
1 |
|
? (int)$this->constraints[Rule::MAXLENGTH] |
133
|
1 |
|
: null; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function minCheck(): ?int |
137
|
|
|
{ |
138
|
1 |
|
return isset($this->constraints[Rule::MINCHECK]) |
139
|
1 |
|
? (int)$this->constraints[Rule::MINCHECK] |
140
|
1 |
|
: null; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
public function maxCheck(): ?int |
144
|
|
|
{ |
145
|
1 |
|
return isset($this->constraints[Rule::MAXCHECK]) |
146
|
1 |
|
? (int)$this->constraints[Rule::MAXCHECK] |
147
|
1 |
|
: null; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
public function min(): ?int |
151
|
|
|
{ |
152
|
1 |
|
return isset($this->constraints[Rule::MIN]) |
153
|
1 |
|
? (int)$this->constraints[Rule::MIN] |
154
|
1 |
|
: null; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
public function max(): ?int |
158
|
|
|
{ |
159
|
1 |
|
return isset($this->constraints[Rule::MAX]) |
160
|
1 |
|
? (int)$this->constraints[Rule::MAX] |
161
|
1 |
|
: null; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
public function equalTo(): ?string |
165
|
|
|
{ |
166
|
1 |
|
return $this->constraints[Rule::EQUALTO] ?? null; |
167
|
|
|
} |
168
|
|
|
|
169
|
4 |
|
private function getConstraintsFromString(string $constraints): array |
170
|
|
|
{ |
171
|
4 |
|
$_constraints = \explode('|', $constraints); |
172
|
|
|
|
173
|
4 |
|
if (\count($_constraints) > 0) { |
174
|
|
|
/** @var string $_constraint */ |
175
|
4 |
|
foreach ($_constraints as $index => $_constraint) { |
176
|
4 |
|
$_rule = \explode(':', $_constraint); |
177
|
|
|
|
178
|
4 |
|
if (\count($_rule) == 2) { |
179
|
4 |
|
[$_name, $_options] = $_rule; |
180
|
4 |
|
$_constraints[$_name] = $_options; |
181
|
|
|
} else { |
182
|
4 |
|
$_constraints[$_rule[0]] = true; |
183
|
|
|
} |
184
|
|
|
|
185
|
4 |
|
unset($_constraints[$index]); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
4 |
|
return $_constraints; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|