1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by gerk on 13.11.17 05:50 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Component\PropertyAccess; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This implementation accesses public properties directly. |
10
|
|
|
* |
11
|
|
|
* This is the fastest but also the most limited accessor. |
12
|
|
|
* |
13
|
|
|
* |
14
|
|
|
* What can it do? |
15
|
|
|
* |
16
|
|
|
* - access public properties |
17
|
|
|
* |
18
|
|
|
* What can it NOT do? |
19
|
|
|
* |
20
|
|
|
* - access protected properties that are visible on the subject class |
21
|
|
|
* -> use ReflectionPropertyAccess |
22
|
|
|
* |
23
|
|
|
* - access private properties declared on the subject class |
24
|
|
|
* -> use ReflectionPropertyAccess |
25
|
|
|
* |
26
|
|
|
* - access private properties that are declared on a base class of the subject class |
27
|
|
|
* -> use ScopedPropertyAccess then which is the slowest accessor |
28
|
|
|
* |
29
|
|
|
* |
30
|
|
|
* @author Karsten J. Gerber <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class PublicPropertyAccess implements PropertyAccess |
33
|
|
|
{ |
34
|
|
|
/** @var string */ |
35
|
|
|
private $propertyName; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $propertyName The name of the property |
39
|
|
|
* |
40
|
|
|
* @return PublicPropertyAccess |
41
|
|
|
*/ |
42
|
21 |
|
public static function create($propertyName) |
43
|
|
|
{ |
44
|
21 |
|
$ret = new self; |
45
|
21 |
|
$ret->propertyName = $propertyName; |
46
|
|
|
|
47
|
21 |
|
return $ret; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Make use of the static create method |
52
|
|
|
*/ |
53
|
|
|
private function __construct() { } |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the value of the property |
57
|
|
|
* |
58
|
|
|
* @param mixed $subject The object to get the value from |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
1 |
|
public function get($subject) |
63
|
|
|
{ |
64
|
1 |
|
return $subject->{$this->propertyName}; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the value of the property |
69
|
|
|
* |
70
|
|
|
* @param mixed $subject The object to set the value to |
71
|
|
|
* @param mixed $value |
72
|
|
|
*/ |
73
|
1 |
|
public function set($subject, $value) |
74
|
|
|
{ |
75
|
1 |
|
$subject->{$this->propertyName} = $value; |
76
|
1 |
|
} |
77
|
|
|
} |
78
|
|
|
|