1
|
|
|
<?php namespace Knot\Dict; |
2
|
|
|
|
3
|
|
|
use ArrayAccess; |
4
|
|
|
use Countable; |
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use IteratorAggregate; |
7
|
|
|
use Knot\Exceptions\FunctionExecuteException; |
8
|
|
|
use Knot\Exceptions\WrongArrayPathException; |
9
|
|
|
use Knot\Exceptions\WrongFunctionException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* PHP ArrayEqualHelper methods |
13
|
|
|
* @method $this merge( array $array1 = null, array $array2 = null, array $_ = null ) |
14
|
|
|
* @method $this reverse() |
15
|
|
|
* @method $this values() |
16
|
|
|
* |
17
|
|
|
* @method mixed shift() |
18
|
|
|
* @method mixed unshift( mixed $variable ) |
19
|
|
|
* @method mixed push( mixed $variable ) |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractDictBody implements Arrayaccess, Countable, IteratorAggregate, Arrayable { |
22
|
|
|
|
23
|
|
|
use ArrayAccessTrait, CountableTrait, IteratorAggregateTrait, PathOperationsTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Knot data. |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $data; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AbstractDictBody |
33
|
|
|
*/ |
34
|
|
|
protected $parentArray; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return AbstractDictBody |
39
|
|
|
*/ |
40
|
|
|
abstract public function kill(); |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $data |
45
|
|
|
* @param AbstractDictBody $parent |
46
|
|
|
* @param $path |
47
|
|
|
*/ |
48
|
|
|
public function __construct(array &$data, AbstractDictBody $parent = null, $path = '') |
49
|
|
|
{ |
50
|
|
|
$this->data =& $data; |
51
|
|
|
$this->path = $path; |
52
|
|
|
$this->parentArray = $parent; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $key |
58
|
|
|
* @param $value |
59
|
|
|
*/ |
60
|
|
|
public function __set($key, $value) |
61
|
|
|
{ |
62
|
|
|
$this->data[$key] = $value; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string|int $key |
68
|
|
|
* |
69
|
|
|
* @return mixed|\Knot\Dict\ChildDict |
70
|
|
|
* @throws \Exception |
71
|
|
|
*/ |
72
|
|
|
public function &__get($key) |
73
|
|
|
{ |
74
|
|
|
if ( array_key_exists($key, $this->data) ) |
75
|
|
|
{ |
76
|
|
|
$target =& $this->data[$key]; |
77
|
|
|
} |
78
|
|
|
else |
79
|
|
|
{ |
80
|
|
|
throw new WrongArrayPathException($key); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ( is_array($target) ) |
84
|
|
|
{ |
85
|
|
|
$r = new ChildDict($target, $this->childParent(), $this->path($key)); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return $r; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $target; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Call callable data variable. |
96
|
|
|
* |
97
|
|
|
* @param string $methodPath |
98
|
|
|
* @param array $arguments |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
* @throws \Exception |
102
|
|
|
*/ |
103
|
|
|
public function call($methodPath, array $arguments = [ ]) |
104
|
|
|
{ |
105
|
|
|
$function = $this->get($methodPath, false); |
106
|
|
|
|
107
|
|
|
if ( ! $function || ! is_callable($function) ) |
108
|
|
|
{ |
109
|
|
|
throw new WrongFunctionException("Wrong function or not callable key!"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
try |
113
|
|
|
{ |
114
|
|
|
$arguments = array_merge([ &$this->data ], $arguments); |
115
|
|
|
|
116
|
|
|
return call_user_func_array($function, $arguments); |
117
|
|
|
} |
118
|
|
|
catch (\Exception $e) |
119
|
|
|
{ |
120
|
|
|
throw new FunctionExecuteException($methodPath); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Function list: Helper Libraries! |
127
|
|
|
* |
128
|
|
|
* @param string $method |
129
|
|
|
* @param array $arguments |
130
|
|
|
* |
131
|
|
|
* @return $this|mixed |
132
|
|
|
* @throws \Exception|WrongFunctionException |
133
|
|
|
*/ |
134
|
|
|
public function __call($method, $arguments = [ ]) |
135
|
|
|
{ |
136
|
|
|
try |
137
|
|
|
{ |
138
|
|
|
return $this->getHelperManager()->execute($method, $arguments, $this); |
139
|
|
|
} |
140
|
|
|
catch (\Exception $e) |
141
|
|
|
{ |
142
|
|
|
throw $e; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param mixed $key |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function __isset($key) |
153
|
|
|
{ |
154
|
|
|
return isset( $this->data[$key] ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param mixed $key |
160
|
|
|
*/ |
161
|
|
|
public function __unset($key) |
162
|
|
|
{ |
163
|
|
|
unset( $this->data[$key] ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Easy Access for get function! |
169
|
|
|
* |
170
|
|
|
* @param $path |
171
|
|
|
* |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
|
|
public function __invoke($path) |
175
|
|
|
{ |
176
|
|
|
return $this->get($path); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Only search own data keys. |
182
|
|
|
* |
183
|
|
|
* @param mixed $key |
184
|
|
|
* |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public function keyExists($key) |
188
|
|
|
{ |
189
|
|
|
return isset( $this->data[$key] ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return HelperManager |
195
|
|
|
*/ |
196
|
|
|
public function getHelperManager() |
197
|
|
|
{ |
198
|
|
|
return HelperManager::getInstance(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return int|string |
204
|
|
|
*/ |
205
|
|
|
public function lastKey() |
206
|
|
|
{ |
207
|
|
|
end($this->data); |
208
|
|
|
|
209
|
|
|
return key($this->data); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
public function &toArray() |
217
|
|
|
{ |
218
|
|
|
return $this->data; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return ParentDict |
224
|
|
|
*/ |
225
|
|
|
public function copy() |
226
|
|
|
{ |
227
|
|
|
$_data = $this->data; |
228
|
|
|
|
229
|
|
|
return new ParentDict($_data, null, ''); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return $this |
235
|
|
|
*/ |
236
|
|
|
public function childParent() |
237
|
|
|
{ |
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: