1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\Reflection; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AbstractReflection |
7
|
|
|
* |
8
|
|
|
* @package BestServedCold\PhalueObjects\Utility\Reflection |
9
|
|
|
* @author Adam Lewis <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2017 Best Served Cold Media Limited |
11
|
|
|
* @license http://http://opensource.org/licenses/GPL-3.0 GPL License |
12
|
|
|
* @link http://bestservedcold.com |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
* @version 1.0.0 |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractReflection |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $class; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var object |
25
|
|
|
*/ |
26
|
|
|
protected $object; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* __get. |
30
|
|
|
* |
31
|
|
|
* Overloading method to get any $key property from $this->class or if not |
32
|
|
|
* static, from $this->object. |
33
|
|
|
* |
34
|
|
|
* @param $key |
35
|
|
|
* @return \ReflectionProperty |
36
|
|
|
*/ |
37
|
2 |
|
public function __get($key) |
38
|
|
|
{ |
39
|
2 |
|
$property = $this->getProperty($key); |
40
|
2 |
|
return $property->getValue($property->isStatic() ?: $this->object); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* __set. |
45
|
|
|
* |
46
|
|
|
* Overloading method to set any $key property to $this->class or if not static, |
47
|
|
|
* to $this->object. |
48
|
|
|
* |
49
|
|
|
* @param $key |
50
|
|
|
* @param $value |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
2 |
|
public function __set($key, $value) |
54
|
|
|
{ |
55
|
2 |
|
$property = $this->getProperty($key); |
56
|
2 |
|
$property->isStatic() |
57
|
2 |
|
? $property->setValue($value) |
58
|
2 |
|
: $property->setValue($this->object, $value); |
59
|
|
|
|
60
|
2 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* __call. |
65
|
|
|
* |
66
|
|
|
* Overloading method to call the $name method from $this->class and if not |
67
|
|
|
* static, using $this->object. |
68
|
|
|
* |
69
|
|
|
* @param $name |
70
|
|
|
* @param $args |
71
|
|
|
* |
72
|
|
|
* @return \ReflectionMethod |
73
|
|
|
*/ |
74
|
2 |
|
public function __call($name, $args) |
75
|
|
|
{ |
76
|
2 |
|
$method = $this->getMethod($name); |
77
|
2 |
|
return $method->invokeArgs($method->isStatic() ? null : $this->object, $args); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get Property |
82
|
|
|
* |
83
|
|
|
* @param $key |
84
|
|
|
* @return \ReflectionProperty |
85
|
|
|
*/ |
86
|
4 |
|
private function getProperty($key) |
87
|
|
|
{ |
88
|
4 |
|
$property = new \ReflectionProperty($this->class, $key); |
89
|
4 |
|
$property->setAccessible(true); |
90
|
4 |
|
return $property; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get Method |
95
|
|
|
* |
96
|
|
|
* @param $key |
97
|
|
|
* @return \ReflectionMethod |
98
|
|
|
*/ |
99
|
2 |
|
private function getMethod($key) |
100
|
|
|
{ |
101
|
2 |
|
$method = new \ReflectionMethod($this->class, $key); |
102
|
2 |
|
$method->setAccessible(true); |
103
|
2 |
|
return $method; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|