|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use \Common\ContainerMagic; |
|
4
|
|
|
use \Common\IContainerAccessors; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class ContainerAccessors |
|
8
|
|
|
* |
|
9
|
|
|
* Support accessors for properties: getter, setter, unsetter |
|
10
|
|
|
* |
|
11
|
|
|
* Below $that - is a shortcut for container object which will be passed to accessor |
|
12
|
|
|
* |
|
13
|
|
|
* Getter is a callable like |
|
14
|
|
|
* function ($this) {} |
|
15
|
|
|
* |
|
16
|
|
|
* Setter is a callable like |
|
17
|
|
|
* function ($that, $value) {} |
|
18
|
|
|
* |
|
19
|
|
|
* Unsetter is a callable like |
|
20
|
|
|
* function ($that) {} |
|
21
|
|
|
* |
|
22
|
|
|
* Use setDirect() and getDirect() methods to access same variable for setter/getter function! |
|
23
|
|
|
* If setter works with other object properties it needs an unsetter to handle clearProperties() method |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
class ContainerAccessors extends ContainerMagic implements IContainerAccessors { |
|
27
|
|
|
/** |
|
28
|
|
|
* Array of accessors - getters/setters/etc |
|
29
|
|
|
* |
|
30
|
|
|
* @var callable[][] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $accessors = array(); |
|
33
|
|
|
|
|
34
|
1 |
|
public function setDirect($name, $value) { |
|
35
|
1 |
|
parent::__set($name, $value); |
|
|
|
|
|
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getDirect($name) { |
|
39
|
|
|
parent::__get($name); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function assignAccessor($varName, $type, $callable) { |
|
43
|
1 |
|
if (empty($callable)) { |
|
44
|
1 |
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
if (is_callable($callable)) { |
|
48
|
1 |
|
$this->accessors[$varName][$type] = $callable; |
|
49
|
1 |
|
} else { |
|
50
|
1 |
|
throw new Exception('Error assigning callable in ' . get_called_class() . '! Callable typed [' . $type . '] is not a callable or not accessible in the scope'); |
|
51
|
|
|
} |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Performs $processor operation on property with specified name |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $processor |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @param null|mixed $value |
|
60
|
|
|
* |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function performMagic($processor, $name, $value = null) { |
|
64
|
|
|
if ( |
|
65
|
|
|
!empty($this->accessors[$name][$processor]) |
|
66
|
|
|
&& |
|
67
|
|
|
is_callable($this->accessors[$name][$processor]) |
|
68
|
|
|
) { |
|
69
|
|
|
return call_user_func($this->accessors[$name][$processor], $this, $value); |
|
70
|
|
|
} else { |
|
71
|
|
|
return parent::$processor($name, $value); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function __set($name, $value) { |
|
76
|
1 |
|
if (is_callable($value)) { |
|
77
|
1 |
|
$this->accessors[$name][P_CONTAINER_GET] = $value; |
|
78
|
1 |
|
} else { |
|
79
|
1 |
|
$this->performMagic(P_CONTAINER_SET, $name, $value); |
|
80
|
|
|
} |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function __get($name) { |
|
84
|
1 |
|
return $this->performMagic(P_CONTAINER_GET, $name, null); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function __unset($name) { |
|
88
|
|
|
$this->performMagic(P_CONTAINER_UNSET, $name, null); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function __isset($name) { |
|
92
|
|
|
// TODO - or here already can isset($this->name) ???? |
|
|
|
|
|
|
93
|
1 |
|
$value = $this->$name; |
|
94
|
|
|
|
|
95
|
1 |
|
return isset($value); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.