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 properties through function call scoping. |
10
|
|
|
* |
11
|
|
|
* This is the slowest accessor with the fewest limitation. Consider using others when possible. |
12
|
|
|
* |
13
|
|
|
* |
14
|
|
|
* What can it do? |
15
|
|
|
* |
16
|
|
|
* - access public properties |
17
|
|
|
* -> you should use PublicPropertyAccess for performance |
18
|
|
|
* |
19
|
|
|
* - access protected properties that are visible on the subject class |
20
|
|
|
* -> use ReflectionPropertyAccess for performance |
21
|
|
|
* |
22
|
|
|
* - access private properties declared on the subject class |
23
|
|
|
* -> use ReflectionPropertyAccess for performance |
24
|
|
|
* |
25
|
|
|
* - access private properties that are declared on a base class of the subject class |
26
|
|
|
* |
27
|
|
|
* |
28
|
|
|
* @author Karsten J. Gerber <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class ScopedPropertyAccess implements PropertyAccess |
31
|
|
|
{ |
32
|
|
|
/** @var string */ |
33
|
|
|
private $scopeClass; |
34
|
|
|
/** @var string */ |
35
|
|
|
private $propertyName; |
36
|
|
|
|
37
|
|
|
/** @var bool */ |
38
|
|
|
private static $initialized = false; |
39
|
|
|
/** @var \Closure */ |
40
|
|
|
private static $getAccess; |
41
|
|
|
/** @var \Closure */ |
42
|
|
|
private static $setAccess; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $scopeClass Fqcn of the scope that declares the property |
46
|
|
|
* @param string $propertyName The name of the property |
47
|
|
|
* |
48
|
|
|
* @return ScopedPropertyAccess |
49
|
|
|
*/ |
50
|
41 |
|
public static function create($scopeClass, $propertyName) |
51
|
|
|
{ |
52
|
41 |
|
$ret = new self; |
53
|
41 |
|
$ret->scopeClass = $scopeClass; |
54
|
41 |
|
$ret->propertyName = $propertyName; |
55
|
|
|
|
56
|
|
|
// we need to at least initialize this class ones |
57
|
41 |
|
if (self::$initialized === false) { |
58
|
|
|
$ret->init(); |
59
|
|
|
} |
60
|
|
|
|
61
|
41 |
|
return $ret; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Make use of the static create method |
66
|
|
|
*/ |
67
|
|
|
private function __construct() { } |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Called on unserialze |
71
|
|
|
*/ |
72
|
2 |
|
public function __wakeup() |
73
|
|
|
{ |
74
|
|
|
// we need to at least initialize this class ones |
75
|
2 |
|
if (self::$initialized === false) { |
76
|
1 |
|
$this->init(); |
77
|
|
|
} |
78
|
2 |
|
} |
79
|
|
|
|
80
|
1 |
|
private function init() |
81
|
|
|
{ |
82
|
1 |
|
self::$initialized = true; |
83
|
|
|
self::$getAccess = function ($prop) { return $this->$prop; }; |
84
|
|
|
self::$setAccess = function ($prop, $value) { $this->$prop = $value; }; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the value of the property |
89
|
|
|
* |
90
|
|
|
* @param mixed $subject The object to get the value from |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
11 |
|
public function get($subject) |
95
|
|
|
{ |
96
|
11 |
|
$func = self::$getAccess->bindTo($subject, $this->scopeClass); |
97
|
|
|
|
98
|
11 |
|
return $func($this->propertyName); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set the value of the property |
103
|
|
|
* |
104
|
|
|
* @param mixed $subject The object to set the value to |
105
|
|
|
* @param mixed $value |
106
|
|
|
*/ |
107
|
8 |
|
public function set($subject, $value) |
108
|
|
|
{ |
109
|
8 |
|
$func = self::$setAccess->bindTo($subject, $this->scopeClass); |
110
|
8 |
|
$func($this->propertyName, $value); |
111
|
8 |
|
} |
112
|
|
|
} |
113
|
|
|
|