1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
abstract class Ajde_Object_Magic extends Ajde_Object |
4
|
|
|
{ |
5
|
|
|
protected $_data = []; |
6
|
|
|
|
7
|
|
|
public final function __call($method, $arguments) |
8
|
|
|
{ |
9
|
|
|
$prefix = strtolower(substr($method, 0, 3)); |
10
|
|
|
$key = substr($method, 3); |
11
|
|
|
$key = strtolower(substr($key, 0, 1)) . substr($key, 1); |
12
|
|
|
switch ($prefix) { |
13
|
|
|
case "get": |
14
|
|
View Code Duplication |
if ($this->has($key)) { |
|
|
|
|
15
|
|
|
return $this->get($key); |
16
|
|
|
} else { |
17
|
|
|
if (!method_exists($this, '__fallback')) { |
18
|
|
|
throw new Ajde_Exception("Property '$key' not set in class " . get_class($this) . " when calling get('$key')", |
19
|
|
|
90007); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
break; |
23
|
|
|
case "set": |
24
|
|
|
return $this->set($key, $arguments[0]); |
25
|
|
|
break; |
|
|
|
|
26
|
|
|
case "has": |
27
|
|
|
return $this->has($key); |
28
|
|
|
break; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
if (method_exists($this, '__fallback')) { |
31
|
|
|
return call_user_func_array([$this, '__fallback'], [$method, $arguments]); |
32
|
|
|
} |
33
|
|
|
throw new Ajde_Exception("Call to undefined method " . get_class($this) . "::$method()", 90006); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @param mixed $name |
|
|
|
|
39
|
|
|
* @param mixed $value |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public function set($key, $value) |
43
|
|
|
{ |
44
|
|
|
$this->_set($key, $value); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function _set($key, $value) |
48
|
|
|
{ |
49
|
|
|
$this->_data[$key] = $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function remove($key) |
53
|
|
|
{ |
54
|
|
|
unset($this->_data[$key]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function get($key) |
58
|
|
|
{ |
59
|
|
View Code Duplication |
if ($this->has($key)) { |
|
|
|
|
60
|
|
|
return $this->_get($key); |
61
|
|
|
} else { |
62
|
|
|
throw new Ajde_Exception("Parameter '$key' not set in class " . get_class($this) . " when calling get('$key')", |
63
|
|
|
90007); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function _get($key) |
68
|
|
|
{ |
69
|
|
|
if (substr_count($key, '.')) { |
70
|
|
|
return Ajde_Core_Array::get($this->_data, $key); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->_data[$key]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function has($key) |
77
|
|
|
{ |
78
|
|
|
$exist = array_key_exists($key, $this->_data); |
79
|
|
|
|
80
|
|
|
if ($exist === false) { |
81
|
|
|
$exist = Ajde_Core_Array::get($this->_data, $key); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return !!$exist; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function isEmpty($key) |
88
|
|
|
{ |
89
|
|
|
$value = $this->get($key); |
90
|
|
|
|
91
|
|
|
return $value instanceof Ajde_Model ? |
92
|
|
|
!$value->hasLoaded() : |
93
|
|
|
empty($value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function hasEmpty($key) |
97
|
|
|
{ |
98
|
|
|
return $this->has($key) && $this->isEmpty($key); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function hasNotEmpty($key) |
102
|
|
|
{ |
103
|
|
|
if ($this->has($key)) { |
104
|
|
|
return !$this->isEmpty($key); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function reset() |
111
|
|
|
{ |
112
|
|
|
$this->_data = []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public final function values() |
116
|
|
|
{ |
117
|
|
|
return $this->_data; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public final function merge($key, array $value) |
121
|
|
|
{ |
122
|
|
|
if (!$this->has($key)) { |
123
|
|
|
$this->set($key, array()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->set($key, Ajde_Core_Array::mergeRecursive($this->get($key), $value)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public final function valuesAsSingleDimensionArray() |
130
|
|
|
{ |
131
|
|
|
$array = []; |
132
|
|
|
foreach ($this->_data as $k => $item) { |
133
|
|
|
if (is_string($item)) { |
134
|
|
|
$array[$k] = $item; |
135
|
|
|
} else { |
136
|
|
|
if (is_array($item)) { |
137
|
|
|
$array[$k] = serialize($item); |
138
|
|
|
} else { |
139
|
|
|
if ($item instanceof Ajde_Object_Magic) { |
140
|
|
|
$array[$k] = serialize($item->valuesAsSingleDimensionArray()); |
141
|
|
|
} else { |
142
|
|
|
if (is_object($item)) { |
143
|
|
|
$array[$k] = serialize($item); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $array; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Translates a camel case string into a string with underscores (e.g. firstName -> first_name) |
155
|
|
|
* |
156
|
|
|
* @see http://www.paulferrett.com/2009/php-camel-case-functions/ |
157
|
|
|
* @param string $str String in camel case format |
158
|
|
|
* @return string $str Translated into underscore format |
159
|
|
|
*/ |
160
|
|
|
public static function fromCamelCase($str) |
161
|
|
|
{ |
162
|
|
|
$str[0] = strtolower($str[0]); |
163
|
|
|
$func = create_function('$c', 'return "_" . strtolower($c[1]);'); |
164
|
|
|
|
165
|
|
|
return preg_replace_callback('/([A-Z])/', $func, $str); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Translates a string with underscores into camel case (e.g. first_name -> firstName) |
170
|
|
|
* |
171
|
|
|
* @see http://www.paulferrett.com/2009/php-camel-case-functions/ |
172
|
|
|
* @param string $str String in underscore format |
173
|
|
|
* @param bool $capitalise_first_char If true, capitalise the first char in $str |
174
|
|
|
* @return string $str translated into camel caps |
175
|
|
|
*/ |
176
|
|
|
public static function toCamelCase($str, $capitalise_first_char = false) |
177
|
|
|
{ |
178
|
|
|
if ($capitalise_first_char) { |
179
|
|
|
$str[0] = strtoupper($str[0]); |
180
|
|
|
} |
181
|
|
|
$func = create_function('$c', 'return strtoupper($c[1]);'); |
182
|
|
|
|
183
|
|
|
return preg_replace_callback('/_([a-z])/', $func, $str); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public static function classnameToUppercase($classname) |
187
|
|
|
{ |
188
|
|
|
return str_replace(' ', '_', ucwords(str_replace('_', ' ', $classname))); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.