|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use \Common\ContainerMagic; |
|
4
|
|
|
use \Common\IPropertyContainer; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class V2PropertyContainer |
|
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 V2PropertyContainer extends ContainerMagic implements IPropertyContainer { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Property descriptions |
|
30
|
|
|
* |
|
31
|
|
|
* @var array[] |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $properties = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Array of accessors - getters/setters/etc |
|
37
|
|
|
* |
|
38
|
|
|
* @var callable[][] |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $accessors = array(); |
|
41
|
|
|
|
|
42
|
1 |
|
public function setProperties($properties) { |
|
43
|
1 |
|
$this->properties = $properties; |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function setDirect($name, $value) { |
|
47
|
1 |
|
parent::__set($name, $value); |
|
|
|
|
|
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function assignAccessor($varName, $type, $callable) { |
|
51
|
1 |
|
if (empty($callable)) { |
|
52
|
1 |
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
if (is_callable($callable)) { |
|
56
|
1 |
|
$this->accessors[$varName][$type] = $callable; |
|
57
|
1 |
|
} else { |
|
58
|
1 |
|
throw new Exception('Error assigning callable in ' . get_called_class() . '! Callable typed [' . $type . '] is not a callable or not accessible in the scope'); |
|
59
|
|
|
} |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function __set($name, $value) { |
|
63
|
1 |
|
if(is_callable($value)) { |
|
64
|
1 |
|
$this->accessors[$name][P_CONTAINER_GETTER] = $value; |
|
65
|
1 |
|
} elseif (!empty($this->accessors[$name][P_CONTAINER_SETTER]) && is_callable($this->accessors[$name][P_CONTAINER_SETTER])) { |
|
66
|
1 |
|
call_user_func($this->accessors[$name][P_CONTAINER_SETTER], $this, $value); |
|
67
|
1 |
|
} else { |
|
68
|
1 |
|
parent::__set($name, $value); |
|
69
|
|
|
} |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
1 |
View Code Duplication |
public function __get($name) { |
|
|
|
|
|
|
73
|
|
|
if ( |
|
74
|
1 |
|
!empty($this->accessors[$name][P_CONTAINER_GETTER]) |
|
75
|
1 |
|
&& |
|
76
|
1 |
|
is_callable($this->accessors[$name][P_CONTAINER_GETTER]) |
|
77
|
1 |
|
) { |
|
78
|
1 |
|
return call_user_func($this->accessors[$name][P_CONTAINER_GETTER], $this); |
|
79
|
|
|
} else { |
|
80
|
1 |
|
return parent::__get($name); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public function __isset($name) { |
|
85
|
|
|
// TODO - or here already can isset($this->name) ???? |
|
|
|
|
|
|
86
|
1 |
|
$value = $this->$name; |
|
87
|
1 |
|
return isset($value); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
View Code Duplication |
public function __unset($name) { |
|
|
|
|
|
|
91
|
|
|
if ( |
|
92
|
1 |
|
!empty($this->accessors[$name][P_CONTAINER_UNSETTER]) |
|
93
|
1 |
|
&& |
|
94
|
1 |
|
is_callable($this->accessors[$name][P_CONTAINER_UNSETTER]) |
|
95
|
1 |
|
) { |
|
96
|
1 |
|
return call_user_func($this->accessors[$name][P_CONTAINER_UNSETTER], $this); |
|
97
|
|
|
} else { |
|
98
|
1 |
|
return parent::__unset($name); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
public function clearProperties() { |
|
103
|
1 |
|
foreach ($this->properties as $propertyName => $propertyData) { |
|
104
|
1 |
|
unset($this->$propertyName); |
|
105
|
1 |
|
} |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
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.