1: <?php namespace Knot\Dict;
2:
3: use ArrayAccess;
4: use Countable;
5: use IteratorAggregate;
6: use Knot\Exceptions\FunctionExecuteException;
7: use Knot\Exceptions\WrongArrayPathException;
8: use Knot\Exceptions\WrongFunctionException;
9:
10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21: abstract class AbstractDictBody implements Arrayaccess, Countable, IteratorAggregate {
22:
23: use ArrayAccessTrait, CountableTrait, IteratorAggregateTrait, PathOperationsTrait;
24:
25: 26: 27: 28:
29: protected $data;
30:
31: 32: 33:
34: protected $parentArray;
35:
36: 37: 38:
39: protected $path = '';
40:
41:
42: 43: 44:
45: abstract public function kill();
46:
47:
48: 49: 50: 51: 52:
53: public function __construct(array &$data, AbstractDictBody $parent = null, $path = '')
54: {
55: $this->data =& $data;
56: $this->path = $path;
57: $this->parentArray = $parent;
58: }
59:
60:
61: 62: 63: 64:
65: public function __set($key, $value)
66: {
67: $this->data[$key] = $value;
68: }
69:
70:
71: 72: 73: 74: 75: 76:
77: public function &__get($key)
78: {
79: if ( array_key_exists($key, $this->data) )
80: {
81: $target =& $this->data[$key];
82: }
83: else
84: {
85: throw new WrongArrayPathException($key);
86: }
87:
88: if ( is_array($target) )
89: {
90: $r = new ChildDict($target, $this->childParent(), $this->path($key));
91:
92: return $r;
93: }
94:
95: return $target;
96: }
97:
98:
99: 100: 101: 102: 103: 104: 105: 106: 107:
108: public function call($method, array $arguments = [ ])
109: {
110: if ( ! $this->keyExists($method) || ! is_callable($this->data[$method]) )
111: {
112: throw new WrongFunctionException("Wrong function or not callable key!");
113: }
114:
115: $function = $this->data[$method];
116:
117: try
118: {
119: $arguments = array_merge([ &$this->data ], $arguments);
120:
121: return call_user_func_array($function, $arguments);
122: }
123: catch (\Exception $e)
124: {
125: throw new FunctionExecuteException($method);
126: }
127: }
128:
129:
130: 131: 132: 133: 134: 135: 136: 137: 138:
139: public function __call($method, $arguments = [ ])
140: {
141: try
142: {
143: return $this->getHelperManager()->execute($method, $arguments, $this);
144: }
145: catch (\Exception $e)
146: {
147: throw $e;
148: }
149: }
150:
151:
152: 153: 154: 155: 156:
157: public function __isset($key)
158: {
159: return isset( $this->data[$key] );
160: }
161:
162:
163: 164: 165:
166: public function __unset($key)
167: {
168: unset( $this->data[$key] );
169: }
170:
171:
172: 173: 174: 175: 176: 177: 178:
179: public function __invoke($path)
180: {
181: return $this->get($path);
182: }
183:
184:
185: 186: 187: 188: 189: 190: 191:
192: public function keyExists($key)
193: {
194: return isset( $this->data[$key] );
195: }
196:
197:
198: 199: 200:
201: public function getHelperManager()
202: {
203: return HelperManager::getInstance();
204: }
205:
206:
207: 208: 209:
210: public function lastKey()
211: {
212: end($this->data);
213:
214: return key($this->data);
215: }
216:
217:
218: 219: 220:
221: public function &toArray()
222: {
223: return $this->data;
224: }
225:
226:
227: 228: 229:
230: public function copy()
231: {
232: $_data = $this->data;
233:
234: return new ParentDict($_data, null, '');
235: }
236:
237:
238: 239: 240:
241: public function childParent()
242: {
243: return $this;
244: }
245:
246: }
247: