1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isolate\LazyObjects\Object; |
4
|
|
|
|
5
|
|
|
use Isolate\LazyObjects\Exception\InvalidArgumentException; |
6
|
|
|
use Isolate\LazyObjects\Exception\NotExistingPropertyException; |
7
|
|
|
use Isolate\LazyObjects\Object\Value\Assembler; |
8
|
|
|
use Isolate\LazyObjects\Object\Value\AssemblerFactory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @api |
12
|
|
|
*/ |
13
|
|
|
class PropertyAccessor |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param $object |
17
|
|
|
* @param $propertyName |
18
|
|
|
* @param $value |
19
|
|
|
* @throws InvalidArgumentException |
20
|
|
|
* @throws NotExistingPropertyException |
21
|
|
|
* |
22
|
|
|
* @api |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
public function set($object, $propertyName, $value) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->validateObject($object); |
27
|
|
|
$reflection = new \ReflectionClass($object); |
28
|
|
|
$this->validateProperty($reflection, $object, $propertyName); |
29
|
|
|
$property = $reflection->getProperty($propertyName); |
30
|
|
|
$property->setAccessible(true); |
31
|
|
|
$property->setValue($object, $value); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $object |
36
|
|
|
* @param $propertyName |
37
|
|
|
* @return mixed |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
* @throws NotExistingPropertyException |
40
|
|
|
* |
41
|
|
|
* @api |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function get($object, $propertyName) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$this->validateObject($object); |
46
|
|
|
$reflection = new \ReflectionClass($object); |
47
|
|
|
$this->validateProperty($reflection, $object, $propertyName); |
48
|
|
|
$property = $reflection->getProperty($propertyName); |
49
|
|
|
$property->setAccessible(true); |
50
|
|
|
|
51
|
|
|
return $property->getValue($object); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $value |
56
|
|
|
* @throws InvalidArgumentException |
57
|
|
|
*/ |
58
|
|
|
private function validateObject($value) |
59
|
|
|
{ |
60
|
|
|
if (!is_object($value)) { |
61
|
|
|
throw new InvalidArgumentException(sprintf( |
62
|
|
|
"PropertyAccessor require object to access property, \"%s\" passed.", |
63
|
|
|
gettype($value) |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \ReflectionClass $reflection |
70
|
|
|
* @param $object |
71
|
|
|
* @param $propertyName |
72
|
|
|
* @throws NotExistingPropertyException |
73
|
|
|
*/ |
74
|
|
|
private function validateProperty(\ReflectionClass $reflection, $object, $propertyName) |
75
|
|
|
{ |
76
|
|
|
if (!$reflection->hasProperty($propertyName)) { |
77
|
|
|
throw new NotExistingPropertyException(sprintf( |
78
|
|
|
"Property \"%s\" does not exists in \"%s\" class.", |
79
|
|
|
$propertyName, |
80
|
|
|
get_class($object) |
81
|
|
|
)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.