1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Serializer\PropertyAccessor; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* ClosureAccessor |
9
|
|
|
* |
10
|
|
|
* Use closure to access object property |
11
|
|
|
* |
12
|
|
|
* @see https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/ |
13
|
|
|
* @see https://github.com/Ocramius/GeneratedHydrator |
14
|
|
|
*/ |
15
|
|
|
class ClosureAccessor implements PropertyAccessorInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The class name |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $class; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The property name |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $property; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The property reader |
33
|
|
|
* |
34
|
|
|
* @var Closure |
35
|
|
|
*/ |
36
|
|
|
private $reader; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The property writer |
40
|
|
|
* |
41
|
|
|
* @var Closure |
42
|
|
|
*/ |
43
|
|
|
private $writer; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @param string $class |
49
|
|
|
* @param string $property |
50
|
|
|
*/ |
51
|
6 |
|
public function __construct(string $class, string $property) |
52
|
|
|
{ |
53
|
6 |
|
$this->class = $class; |
54
|
6 |
|
$this->property = $property; |
55
|
6 |
|
$this->reader = $this->createReader(); |
56
|
6 |
|
$this->writer = $this->createWriter(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
2 |
|
public function write($object, $value) |
63
|
|
|
{ |
64
|
2 |
|
$this->writer->__invoke($object, $value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
4 |
|
public function read($object) |
71
|
|
|
{ |
72
|
4 |
|
return $this->reader->__invoke($object); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create property accessor. |
77
|
|
|
* |
78
|
|
|
* @return Closure |
79
|
|
|
*/ |
80
|
6 |
|
private function createReader(): Closure |
81
|
|
|
{ |
82
|
6 |
|
$property = $this->property; |
83
|
|
|
|
84
|
6 |
|
return Closure::bind( |
85
|
|
|
/** |
86
|
|
|
* @param object $object |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
6 |
|
function ($object) use ($property) { |
90
|
4 |
|
return $object->$property; |
91
|
6 |
|
}, |
92
|
6 |
|
null, |
93
|
6 |
|
$this->class |
94
|
6 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create property accessor. |
99
|
|
|
* |
100
|
|
|
* @return Closure |
101
|
|
|
*/ |
102
|
6 |
|
private function createWriter(): Closure |
103
|
|
|
{ |
104
|
6 |
|
$property = $this->property; |
105
|
|
|
|
106
|
6 |
|
return Closure::bind( |
107
|
|
|
/** |
108
|
|
|
* @param object $object |
109
|
|
|
* @param mixed $value |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
6 |
|
function ($object, $value) use ($property) { |
113
|
2 |
|
$object->$property = $value; |
114
|
6 |
|
}, |
115
|
6 |
|
null, |
116
|
6 |
|
$this->class |
117
|
6 |
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Dont serialize closures |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
2 |
|
public function __sleep() |
126
|
|
|
{ |
127
|
2 |
|
return ['class', 'property']; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Rebuild closure. |
132
|
|
|
*/ |
133
|
2 |
|
public function __wakeup() |
134
|
|
|
{ |
135
|
2 |
|
$this->reader = $this->createReader(); |
136
|
2 |
|
$this->writer = $this->createWriter(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|