|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Composite Utils package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the |
|
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @package spaark/composite-utils |
|
11
|
|
|
* @author Emily Shepherd <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Service; |
|
16
|
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionProperty; |
|
18
|
|
|
use Spaark\CompositeUtils\Exception\PropertyNotWritableException; |
|
19
|
|
|
use Spaark\CompositeUtils\Exception\PropertyNotReadableException; |
|
20
|
|
|
use Spaark\CompositeUtils\Exception\CannotReadPropertyException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* This class is used to access properties of a composite, enforcing |
|
24
|
|
|
* data type requirements and access permissions |
|
25
|
|
|
*/ |
|
26
|
|
|
class ConditionalPropertyAccessor extends PropertyAccessor |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
6 |
|
protected function setAnyValue(ReflectionProperty $property, $value) |
|
32
|
|
|
{ |
|
33
|
6 |
|
if ($property->writable) |
|
34
|
|
|
{ |
|
35
|
3 |
|
parent::setAnyValue($property, $value); |
|
36
|
|
|
} |
|
37
|
|
|
else |
|
38
|
|
|
{ |
|
39
|
3 |
|
throw new PropertyNotWritableException |
|
40
|
|
|
( |
|
41
|
3 |
|
get_class($this->object), |
|
42
|
3 |
|
$property->name |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
3 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
*/ |
|
50
|
7 |
|
public function getValue(string $property) |
|
51
|
|
|
{ |
|
52
|
7 |
|
if (!$this->reflect->properties->containsKey($property)) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
1 |
|
throw new CannotReadPropertyException |
|
55
|
|
|
( |
|
56
|
1 |
|
get_class($this->object), |
|
57
|
1 |
|
$property |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
6 |
|
elseif (!$this->reflect->properties[$property]->readable) |
|
61
|
|
|
{ |
|
62
|
3 |
|
throw new PropertyNotReadableException |
|
63
|
|
|
( |
|
64
|
3 |
|
get_class($this->object), |
|
65
|
3 |
|
$property |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
return parent::getValue($property); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
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: