|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isolate\UnitOfWork\Object; |
|
4
|
|
|
|
|
5
|
|
|
use Isolate\UnitOfWork\Exception\InvalidArgumentException; |
|
6
|
|
|
use Isolate\UnitOfWork\Exception\NotExistingPropertyException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @api |
|
10
|
|
|
*/ |
|
11
|
|
|
final class PropertyAccessor |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param $object |
|
15
|
|
|
* @param $propertyName |
|
16
|
|
|
* @return mixed |
|
17
|
|
|
* @throws InvalidArgumentException |
|
18
|
|
|
* @throws NotExistingPropertyException |
|
19
|
|
|
* |
|
20
|
|
|
* @api |
|
21
|
|
|
*/ |
|
22
|
|
View Code Duplication |
public function getValue($object, $propertyName) |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
$this->validateObject($object); |
|
25
|
|
|
$reflection = new \ReflectionClass($object); |
|
26
|
|
|
$this->validateProperty($reflection, $object, $propertyName); |
|
27
|
|
|
$property = $reflection->getProperty($propertyName); |
|
28
|
|
|
$property->setAccessible(true); |
|
29
|
|
|
|
|
30
|
|
|
return $property->getValue($object); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param $object |
|
35
|
|
|
* @param $propertyName |
|
36
|
|
|
* @param $value |
|
37
|
|
|
* @throws InvalidArgumentException |
|
38
|
|
|
* @throws NotExistingPropertyException |
|
39
|
|
|
* |
|
40
|
|
|
* @api |
|
41
|
|
|
*/ |
|
42
|
|
View Code Duplication |
public function setValue($object, $propertyName, $value) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$this->validateObject($object); |
|
45
|
|
|
$reflection = new \ReflectionClass($object); |
|
46
|
|
|
$this->validateProperty($reflection, $object, $propertyName); |
|
47
|
|
|
$property = $reflection->getProperty($propertyName); |
|
48
|
|
|
$property->setAccessible(true); |
|
49
|
|
|
|
|
50
|
|
|
$property->setValue($object, $value); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $value |
|
55
|
|
|
* @throws InvalidArgumentException |
|
56
|
|
|
*/ |
|
57
|
|
|
private function validateObject($value) |
|
58
|
|
|
{ |
|
59
|
|
|
if (!is_object($value)) { |
|
60
|
|
|
throw new InvalidArgumentException(sprintf( |
|
61
|
|
|
"PropertyAccessor require object to access property, \"%s\" passed.", |
|
62
|
|
|
gettype($value) |
|
63
|
|
|
)); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param \ReflectionClass $reflection |
|
69
|
|
|
* @param $object |
|
70
|
|
|
* @param $propertyName |
|
71
|
|
|
* @throws NotExistingPropertyException |
|
72
|
|
|
*/ |
|
73
|
|
|
private function validateProperty(\ReflectionClass $reflection, $object, $propertyName) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$reflection->hasProperty($propertyName)) { |
|
76
|
|
|
throw new NotExistingPropertyException(sprintf( |
|
77
|
|
|
"Property \"%s\" does not exists in \"%s\" class.", |
|
78
|
|
|
$propertyName, |
|
79
|
|
|
get_class($object) |
|
80
|
|
|
)); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.