@@ 21-148 (lines=128) @@ | ||
18 | * |
|
19 | * @author Ivannis Suárez Jerez <[email protected]> |
|
20 | */ |
|
21 | class MethodMetadata implements \Serializable |
|
22 | { |
|
23 | /** |
|
24 | * @var string |
|
25 | */ |
|
26 | protected $className; |
|
27 | ||
28 | /** |
|
29 | * @var string |
|
30 | */ |
|
31 | protected $methodName; |
|
32 | ||
33 | /** |
|
34 | * @var \ReflectionMethod |
|
35 | */ |
|
36 | protected $reflection; |
|
37 | ||
38 | /** |
|
39 | * @var ArrayHashMapInterface |
|
40 | */ |
|
41 | protected $metadata; |
|
42 | ||
43 | /** |
|
44 | * MethodMetadata constructor. |
|
45 | * |
|
46 | * @param string $className |
|
47 | * @param string $methodName |
|
48 | */ |
|
49 | public function __construct($className, $methodName) |
|
50 | { |
|
51 | $this->className = $className; |
|
52 | $this->methodName = $methodName; |
|
53 | $this->metadata = new ArrayHashMap(); |
|
54 | ||
55 | $this->reflection = new \ReflectionMethod($className, $methodName); |
|
56 | $this->reflection->setAccessible(true); |
|
57 | } |
|
58 | ||
59 | /** |
|
60 | * @return string |
|
61 | */ |
|
62 | public function className() |
|
63 | { |
|
64 | return $this->className; |
|
65 | } |
|
66 | ||
67 | /** |
|
68 | * @return string |
|
69 | */ |
|
70 | public function methodName() |
|
71 | { |
|
72 | return $this->methodName; |
|
73 | } |
|
74 | ||
75 | /** |
|
76 | * @return \ReflectionMethod |
|
77 | */ |
|
78 | public function reflection() |
|
79 | { |
|
80 | return $this->reflection; |
|
81 | } |
|
82 | ||
83 | /** |
|
84 | * @return array |
|
85 | */ |
|
86 | public function metadata() |
|
87 | { |
|
88 | return $this->metadata->toArray(); |
|
89 | } |
|
90 | ||
91 | /** |
|
92 | * @param string $key |
|
93 | * @param mixed $value |
|
94 | */ |
|
95 | public function addMetadata($key, $value) |
|
96 | { |
|
97 | $this->metadata->set($key, $value); |
|
98 | } |
|
99 | ||
100 | /** |
|
101 | * @param string $key |
|
102 | * |
|
103 | * @return mixed|null |
|
104 | */ |
|
105 | public function getMetadata($key) |
|
106 | { |
|
107 | return $this->metadata->get($key); |
|
108 | } |
|
109 | ||
110 | /** |
|
111 | * @param object $obj |
|
112 | * @param array $args |
|
113 | * |
|
114 | * @return mixed |
|
115 | */ |
|
116 | public function invoke($obj, array $args = array()) |
|
117 | { |
|
118 | return $this->reflection->invokeArgs($obj, $args); |
|
119 | } |
|
120 | ||
121 | /** |
|
122 | * {@inheritdoc} |
|
123 | */ |
|
124 | public function serialize() |
|
125 | { |
|
126 | return serialize(array( |
|
127 | $this->className, |
|
128 | $this->methodName, |
|
129 | $this->metadata->toArray(), |
|
130 | )); |
|
131 | } |
|
132 | ||
133 | /** |
|
134 | * {@inheritdoc} |
|
135 | */ |
|
136 | public function unserialize($str) |
|
137 | { |
|
138 | list( |
|
139 | $this->className, |
|
140 | $this->methodName, |
|
141 | $metadata) = unserialize($str); |
|
142 | ||
143 | $this->reflection = new \ReflectionMethod($this->className, $this->methodName); |
|
144 | $this->reflection->setAccessible(true); |
|
145 | ||
146 | $this->metadata = new ArrayHashMap($metadata); |
|
147 | } |
|
148 | } |
|
149 |
@@ 21-150 (lines=130) @@ | ||
18 | * |
|
19 | * @author Ivannis Suárez Jerez <[email protected]> |
|
20 | */ |
|
21 | class PropertyMetadata implements \Serializable |
|
22 | { |
|
23 | /** |
|
24 | * @var string |
|
25 | */ |
|
26 | protected $className; |
|
27 | ||
28 | /** |
|
29 | * @var string |
|
30 | */ |
|
31 | protected $propertyName; |
|
32 | ||
33 | /** |
|
34 | * @var \ReflectionProperty |
|
35 | */ |
|
36 | protected $reflection; |
|
37 | ||
38 | /** |
|
39 | * @var ArrayHashMapInterface |
|
40 | */ |
|
41 | protected $metadata; |
|
42 | ||
43 | public function __construct($className, $propertyName) |
|
44 | { |
|
45 | $this->className = $className; |
|
46 | $this->propertyName = $propertyName; |
|
47 | $this->metadata = new ArrayHashMap(); |
|
48 | ||
49 | $this->reflection = new \ReflectionProperty($className, $propertyName); |
|
50 | $this->reflection->setAccessible(true); |
|
51 | } |
|
52 | ||
53 | /** |
|
54 | * @return string |
|
55 | */ |
|
56 | public function className() |
|
57 | { |
|
58 | return $this->className; |
|
59 | } |
|
60 | ||
61 | /** |
|
62 | * @return string |
|
63 | */ |
|
64 | public function propertyName() |
|
65 | { |
|
66 | return $this->propertyName; |
|
67 | } |
|
68 | ||
69 | /** |
|
70 | * @return \ReflectionProperty |
|
71 | */ |
|
72 | public function reflection() |
|
73 | { |
|
74 | return $this->reflection; |
|
75 | } |
|
76 | ||
77 | /** |
|
78 | * @return array |
|
79 | */ |
|
80 | public function metadata() |
|
81 | { |
|
82 | return $this->metadata->toArray(); |
|
83 | } |
|
84 | ||
85 | /** |
|
86 | * @param string $key |
|
87 | * @param mixed $value |
|
88 | */ |
|
89 | public function addMetadata($key, $value) |
|
90 | { |
|
91 | $this->metadata->set($key, $value); |
|
92 | } |
|
93 | ||
94 | /** |
|
95 | * @param string $key |
|
96 | * |
|
97 | * @return mixed|null |
|
98 | */ |
|
99 | public function getMetadata($key) |
|
100 | { |
|
101 | return $this->metadata->get($key); |
|
102 | } |
|
103 | ||
104 | /** |
|
105 | * @param object $obj |
|
106 | * |
|
107 | * @return mixed |
|
108 | */ |
|
109 | public function getValue($obj) |
|
110 | { |
|
111 | return $this->reflection->getValue($obj); |
|
112 | } |
|
113 | ||
114 | /** |
|
115 | * @param object $obj |
|
116 | * @param string $value |
|
117 | */ |
|
118 | public function setValue($obj, $value) |
|
119 | { |
|
120 | $this->reflection->setValue($obj, $value); |
|
121 | } |
|
122 | ||
123 | /** |
|
124 | * {@inheritdoc} |
|
125 | */ |
|
126 | public function serialize() |
|
127 | { |
|
128 | return serialize(array( |
|
129 | $this->className, |
|
130 | $this->propertyName, |
|
131 | $this->metadata->toArray(), |
|
132 | )); |
|
133 | } |
|
134 | ||
135 | /** |
|
136 | * {@inheritdoc} |
|
137 | */ |
|
138 | public function unserialize($str) |
|
139 | { |
|
140 | list( |
|
141 | $this->className, |
|
142 | $this->propertyName, |
|
143 | $metadata) = unserialize($str); |
|
144 | ||
145 | $this->reflection = new \ReflectionProperty($this->className, $this->propertyName); |
|
146 | $this->reflection->setAccessible(true); |
|
147 | ||
148 | $this->metadata = new ArrayHashMap($metadata); |
|
149 | } |
|
150 | } |
|
151 |