1 | <?php |
||
33 | class PropertyAccessor extends RawPropertyAccessor |
||
34 | { |
||
35 | /** |
||
36 | * Reflection information about the composite being accessed |
||
37 | * |
||
38 | * @var ReflectionComposite |
||
39 | */ |
||
40 | protected $reflect; |
||
41 | |||
42 | /** |
||
43 | * Creates the PropertyAccessor for the given object, using the |
||
44 | * given reflection information |
||
45 | * |
||
46 | * @param object $object The object to access |
||
47 | * @param ReflectionComposite $reflect Reflection information about |
||
48 | * the composite |
||
49 | */ |
||
50 | 33 | public function __construct($object, ReflectionComposite $reflect) |
|
56 | |||
57 | /** |
||
58 | * Initializes the given object with the given parameters, enforcing |
||
59 | * the constructor requirements and auto building any left overs |
||
60 | */ |
||
61 | 16 | public function constructObject(...$args) |
|
107 | |||
108 | /** |
||
109 | * Builds a property automatically |
||
110 | * |
||
111 | * @param ReflectionProperty $property The property to build |
||
112 | */ |
||
113 | 12 | protected function buildProperty(ReflectionProperty $property) |
|
114 | { |
||
115 | 12 | if (!$property->type instanceof ObjectType) |
|
116 | { |
||
117 | 3 | $this->setAnyValue($property, 0); |
|
118 | } |
||
119 | 11 | elseif ($property->builtInConstructor) |
|
120 | { |
||
121 | 2 | $class = (string)$property->type->classname; |
|
122 | 2 | $this->setRawValue($property->name, new $class()); |
|
123 | } |
||
124 | 12 | } |
|
125 | |||
126 | /** |
||
127 | * Returns the value of the property |
||
128 | * |
||
129 | * @param string $property The name of the property to get |
||
130 | * @return mixed The value of the property |
||
131 | */ |
||
132 | 6 | public function getValue(string $property) |
|
136 | |||
137 | /** |
||
138 | * Sets the value of a property, enforcing datatype requirements |
||
139 | * |
||
140 | * @param string $property The name of the property to set |
||
141 | * @param mixed $value The value to set |
||
142 | */ |
||
143 | 9 | public function setValue(string $property, $value) |
|
159 | |||
160 | /** |
||
161 | * Sets the value of a property, enforcing datatype requirements |
||
162 | * |
||
163 | * @param ReflectionProperty $property The property to set |
||
164 | * @param mixed $value The value to set |
||
165 | */ |
||
166 | 12 | protected function setAnyValue(ReflectionProperty $property, $value) |
|
177 | |||
178 | /** |
||
179 | * Attempts to set a property with a null value |
||
180 | * |
||
181 | * @param ReflectionProperty $property The property to set |
||
182 | */ |
||
183 | 2 | private function setNullValue(ReflectionProperty $property) |
|
194 | |||
195 | /** |
||
196 | * Attempts to set a property with a non null value |
||
197 | * |
||
198 | * @param ReflectionProperty $property The property to set |
||
199 | * @param mixed $value The value to set |
||
200 | */ |
||
201 | 10 | private function setNonNullValue |
|
202 | ( |
||
203 | ReflectionProperty $property, |
||
204 | $value |
||
205 | ) |
||
206 | { |
||
207 | 10 | switch (get_class($property->type)) |
|
208 | { |
||
209 | 10 | case MixedType::class: |
|
210 | $this->setRawValue($property, $value); |
||
211 | break; |
||
212 | 10 | case IntegerType::class: |
|
213 | 1 | $this->setScalarValue($property, $value, 'Integer', |
|
214 | function($value) |
||
215 | { |
||
216 | 1 | return (integer)$value; |
|
217 | 1 | }); |
|
218 | 1 | break; |
|
219 | 10 | case StringType::class: |
|
220 | 3 | $this->setScalarValue($property, $value, 'String', |
|
221 | function($value) |
||
222 | { |
||
223 | 3 | return (string)$value; |
|
224 | 3 | }); |
|
225 | 3 | break; |
|
226 | 9 | case BooleanType::class: |
|
227 | 5 | $this->setScalarValue($property, $value, 'Boolean', |
|
228 | 5 | function($value) |
|
229 | { |
||
230 | 5 | return (boolean)$value; |
|
231 | 5 | }); |
|
232 | 5 | break; |
|
233 | 7 | case CollectionType::class: |
|
234 | $this->setCollectionValue($property, $value); |
||
235 | break; |
||
236 | 7 | case ObjectType::class: |
|
237 | 7 | $this->setObjectValue($property, $value); |
|
238 | 7 | break; |
|
239 | } |
||
240 | 10 | } |
|
241 | |||
242 | /** |
||
243 | * Attempts to set a property which expects a scalar value |
||
244 | * |
||
245 | * @param ReflectionProperty $property The property to set |
||
246 | * @param mixed $value The value to set |
||
247 | * @param string $name The name of the scalar type |
||
248 | * @param callable $cast Method to cast a value to the scalar data |
||
249 | * type |
||
250 | */ |
||
251 | 6 | private function setScalarValue |
|
280 | |||
281 | /** |
||
282 | * Attempts to set a property which expects an object value |
||
283 | * |
||
284 | * @param ReflectionProperty $property The property to set |
||
285 | * @param mixed $value The value to set |
||
286 | */ |
||
287 | 7 | private function setObjectValue |
|
288 | ( |
||
289 | ReflectionProperty $property, |
||
290 | $value |
||
291 | ) |
||
292 | { |
||
293 | 7 | if (is_a($value, $property->type->classname)) |
|
294 | { |
||
295 | 7 | $this->setRawValue($property->name, $value); |
|
296 | } |
||
297 | else |
||
298 | { |
||
299 | $this->throwError |
||
300 | ( |
||
301 | $property, |
||
302 | $property->type->classname, |
||
303 | $value |
||
304 | ); |
||
305 | } |
||
306 | 7 | } |
|
307 | |||
308 | /** |
||
309 | * Attempts to set a property which expects a collection value |
||
310 | * |
||
311 | * @param ReflectionProperty $property The property to set |
||
312 | * @param mixed The value to set |
||
313 | */ |
||
314 | private function setCollectionValue |
||
315 | ( |
||
316 | ReflectionProperty $property, |
||
317 | $value |
||
318 | ) |
||
319 | { |
||
320 | if (is_a($value, \ArrayAccess::class) || is_array($value)) |
||
321 | { |
||
322 | $this->setRawValue($property->name, $value); |
||
323 | } |
||
324 | else |
||
325 | { |
||
326 | $this->throwError($property, 'Collection', $value); |
||
327 | } |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Throws an IlleglPropertyTypeException |
||
332 | * |
||
333 | * @param ReflectionProperty $property The property being set |
||
334 | * @param string $expected The expected datatype |
||
335 | * @param string $value The value being set |
||
336 | */ |
||
337 | 1 | private function throwError |
|
352 | } |
||
353 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: