1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Dto; |
4
|
|
|
|
5
|
|
|
use Cerbero\Dto\Exceptions\UnexpectedValueException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The DTO property. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
class DtoProperty |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The property name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The property raw value. |
22
|
|
|
* |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
|
|
protected $rawValue; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The property types. |
29
|
|
|
* |
30
|
|
|
* @var DtoPropertyTypes |
31
|
|
|
*/ |
32
|
|
|
protected $types; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The DTO flags. |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $flags; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The property value processor. |
43
|
|
|
* |
44
|
|
|
* @var DtoPropertyValueProcessor |
45
|
|
|
*/ |
46
|
|
|
protected $valueProcessor; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The processed value. |
50
|
|
|
* |
51
|
|
|
* @var mixed |
52
|
|
|
*/ |
53
|
|
|
protected $processedValue; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Whether the value has been processed. |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $valueIsProcessed = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Instantiate the class. |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* @param mixed $rawValue |
67
|
|
|
* @param DtoPropertyTypes $types |
68
|
|
|
* @param int $flags |
69
|
|
|
*/ |
70
|
282 |
|
protected function __construct(string $name, $rawValue, DtoPropertyTypes $types, int $flags) |
71
|
|
|
{ |
72
|
282 |
|
$this->name = $name; |
73
|
282 |
|
$this->rawValue = $rawValue; |
74
|
282 |
|
$this->types = $types; |
75
|
282 |
|
$this->flags = $flags; |
76
|
282 |
|
$this->valueProcessor = new DtoPropertyValueProcessor($this); |
77
|
282 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Retrieve a DTO property instance after validating it |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* @param mixed $rawValue |
84
|
|
|
* @param DtoPropertyTypes $types |
85
|
|
|
* @param int $flags |
86
|
|
|
* @return self |
87
|
|
|
* @throws UnexpectedValueException |
88
|
|
|
*/ |
89
|
282 |
|
public static function create(string $name, $rawValue, DtoPropertyTypes $types, int $flags): self |
90
|
|
|
{ |
91
|
282 |
|
$instance = new static($name, $rawValue, $types, $flags); |
92
|
|
|
|
93
|
282 |
|
return $instance->validate(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Validate the current property value depending on types and flags |
98
|
|
|
* |
99
|
|
|
* @return self |
100
|
|
|
* @throws UnexpectedValueException |
101
|
|
|
*/ |
102
|
282 |
|
public function validate(): self |
103
|
|
|
{ |
104
|
282 |
|
$canBeDto = $this->rawValue instanceof Dto || is_array($this->rawValue); |
105
|
|
|
|
106
|
|
|
switch (true) { |
107
|
282 |
|
case $this->types->expectedDto && $canBeDto: |
108
|
261 |
|
case $this->rawValue === null && $this->isNullable(): |
109
|
231 |
|
case $this->types->expectCollection && is_iterable($this->rawValue): |
110
|
222 |
|
case $this->types->match($this->value()): |
|
|
|
|
111
|
261 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
21 |
|
throw new UnexpectedValueException($this); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Determine whether this property is nullable |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
57 |
|
public function isNullable(): bool |
123
|
|
|
{ |
124
|
57 |
|
if ($this->flags & NOT_NULLABLE) { |
125
|
6 |
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
51 |
|
return ($this->flags & NULLABLE) || $this->types->includeNull; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retrieve the processed value |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
246 |
|
public function value() |
137
|
|
|
{ |
138
|
246 |
|
if (!$this->valueIsProcessed) { |
139
|
246 |
|
$this->processedValue = $this->valueProcessor->process(); |
140
|
246 |
|
$this->valueIsProcessed = true; |
141
|
|
|
} |
142
|
|
|
|
143
|
246 |
|
return $this->processedValue; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Retrieve the property name |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
39 |
|
public function getName(): string |
152
|
|
|
{ |
153
|
39 |
|
return $this->name; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Retrieve the property raw value |
158
|
|
|
* |
159
|
|
|
* @return mixed |
160
|
|
|
*/ |
161
|
255 |
|
public function getRawValue() |
162
|
|
|
{ |
163
|
255 |
|
return $this->rawValue; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Retrieve the property types |
168
|
|
|
* |
169
|
|
|
* @return DtoPropertyTypes |
170
|
|
|
*/ |
171
|
258 |
|
public function getTypes(): DtoPropertyTypes |
172
|
|
|
{ |
173
|
258 |
|
return $this->types; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Retrieve the DTO flags |
178
|
|
|
* |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
219 |
|
public function getFlags(): int |
182
|
|
|
{ |
183
|
219 |
|
return $this->flags; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set a new value to this property and validate it |
188
|
|
|
* |
189
|
|
|
* @param mixed $rawValue |
190
|
|
|
* @param int $flags |
191
|
|
|
* @return self |
192
|
|
|
*/ |
193
|
24 |
|
public function setValue($rawValue, int $flags): self |
194
|
|
|
{ |
195
|
24 |
|
$this->rawValue = $rawValue; |
196
|
24 |
|
$this->flags = $flags; |
197
|
24 |
|
$this->valueIsProcessed = false; |
198
|
|
|
|
199
|
24 |
|
return $this->validate(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Determine how to clone the DTO property |
204
|
|
|
* |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
27 |
|
public function __clone() |
208
|
|
|
{ |
209
|
27 |
|
$this->types = clone $this->types; |
210
|
27 |
|
$this->valueProcessor = new DtoPropertyValueProcessor($this); |
211
|
27 |
|
} |
212
|
|
|
} |
213
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.