This repository has been archived by the owner on Dec 2, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData.php
290 lines (260 loc) · 8.25 KB
/
Data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php namespace AIIX;
/*
* (C) 2016, AII (Alexey Ilyin).
*/
class_exists('\AIIX\Object', false)
or require __dir__.'/Object.php';
class Data extends Object
implements \Countable, \Iterator, \ArrayAccess
{
protected $data, $ts = '/';
/**
*
* @param array $data
* @param mixed $pool bool | array
*/
public function __construct (array $data = array(), $pool = false) {
$this->data = $data;
$pool and self::build($this->data, is_array($pool) ? $pool : $data);
}
/**
* Get value at $path, $default otherwise.
* @param string $path null | 'key(/key)*'
* @param mixed $default
* @return mixed
*/
public function get ($path = null, $default = null) {
return self::getp($this->data, $path, $default, $this->ts);
}
/**
* Check if the value at $path exists.
* @param string $path
* @return boolean
*/
public function has ($path) {
return self::hasp($this->data, $path);
}
/**
* Get reference to the value at $path.
* &$aiixdata->ref() returns reference to raw data array.
* @param string $path null | 'key(/key)*'
* @param mixed $default
* @return mixed
*/
public function &ref ($path = null) {
return self::refp($this->data, $path);
}
/**
* Set the value at $path.
* @param string $path
* @param mixed $value
* @return \AIIXData
*/
public function set ($path, $value) {
self::setp($this->data, $path, $value);
return $this;
}
/**
* Set the reference at $path.
* @param string $path
* @param mixed &$value
* @return \AIIXData
*/
public function setref ($path, &$value) {
self::setrefp($this->data, $path, $value);
return $this;
}
/**
* Add the value at $path.
* @param string $path
* @param mixed $value
* @param mixed $key scalar | null
* @return \AIIXData
*/
public function add ($path, $value, $key = null) {
self::addp($this->data, $path, $value, $key);
return $this;
}
/**
* Add the reference at $path.
* @param string $path
* @param mixed &$value
* @param mixed $key scalar | null
* @return \AIIXData
*/
public function addref ($path, &$value, $key = null) {
self::addrefp($this->data, $path, $value, $key);
return $this;
}
//---=====[ facade ]=====---//
/**
* If exists $pool[$data([key])+], copy it to $data([key])+ recursively.
* @param array $data
* @param mixed $pool false | array
*/
public static function build (&$data, $pool = false) {
$pool or $pool = $data;
foreach ($data as &$val) {
if (!is_scalar($val)) {
settype($val, 'array')
and self::build($val, $pool);
}
else if (isset($val[0]) && $val[0] === '@' && isset($pool[$val])) {
is_scalar($val = $pool[$val])
or settype($val, 'array');
}
}
}
public static function getp ($data, $path = null, $default = null, $ts='/') {
if (isset($path)) for ($name = strtok($path,$ts);
$name !== false; $name = strtok($ts)) {
if (!strlen($name = trim($name))) continue;
if (is_scalar($data) || !isset($data[$name])) {
return is_callable($default) ?
call_user_func($default, $path) :
$default;
}
$data = $data[$name];
}
return $data;
}
public static function hasp ($data, $path, $ts='/') {
for ($name = strtok($path,$ts);
$name !== false; $name = strtok($ts)) {
if (!strlen($name = trim($name))) continue;
if (is_scalar($data) || !isset($data[$name])) return false;
$data = $data[$name];
}
return true;
}
public static function &refp (&$data, $path = null, $ts='/') {
if (isset($path)) for ($name = strtok($path,$ts);
$name !== false; $name = strtok($ts)) {
if (!strlen($name = trim($name))) continue;
if (is_scalar($data)) $data = array();
$data = &$data[$name];
}
return $data;
}
public static function setp (&$data, $path, $value, $ts='/') {
$data = &self::refp($data, $path, $ts);
$data = $value;
}
public static function setrefp (&$data, $path, &$value, $ts='/') {
for ($name = strtok($path, $ts);
$name !== false; $name = strtok($ts)) {
if (!strlen($name = trim($name))) continue;
if (is_scalar($data)) $data = array();
$tnam = $name;
$tdat = &$data;
$data = &$data[$name];
}
assert('isset($tnam)');
$tdat[$tnam] = &$value;
}
public static function addp (&$data, $path, $value, $key = null, $ts='/') {
$data = &self::refp($data, $path, $ts);
settype($data, 'array');
if (isset($key)) $data[$key] = $value;
else $data[] = $value;
}
public static function addrefp (&$data, $path, &$value, $key = null, $ts='/') {
$data = &self::refp($data, $path, $ts);
settype($data, 'array');
if (isset($key)) $data[$key] =& $value;
else $data[] =& $value;
}
/**
* Get $array[$key] if it's set, $default otherwise.
* @param array|ArrayAccess $array
* @param mixed $key
* @param mixed $default
* @return mixed
*/
public static function take ($array, $key, $default = null) {
return isset($array[$key]) ? $array[$key] : (
is_callable($default) ?
call_user_func($default, $key) :
$default
);
}
/**
* Get $array[$key] if it's set, or failed assertion otherwise.
* @param type $array
* @param type $key
* @return type
*/
public static function required ($array, $key) {
return assert("isset(\$array['$key'])") ? $array[$key] : null;
}
/**
* Get and unset $array[$key] if it's set, return $default otherwise.
* @param type $array
* @param type $key
* @param type $default
* @return type
*/
public static function extract (&$array, $key, $default = null) {
if (!isset($array[$key])) return $default;
$result = $array[$key];
unset($array[$key]);
return $result;
}
//---=====[ data sources ]=====---//
/**
* Parse key=vals string (kvs).
* @param string $kvs
* @return array
*/
public static function kvs ($kvs) {
if (preg_match_all('/^([^=\n\r]+)\s*=\s*(.*)$/m',
$kvs, $m, PREG_PATTERN_ORDER)) {
return array_combine(
array_map('trim',$m[1]),
array_map('trim',$m[2])
);
}
return array();
}
/**
* Parse ini-file.
* @param string $filename
* @param bool $sections
* @return array
*/
public static function ini ($filename, $sections = true) {
return parse_ini_file($filename, $sections);
}
//---=====[ Interface implementation ]=====---//
public function current () {
return current($this->data);
}
public function key () {
return key($this->data);
}
public function next () {
return next($this->data);
}
public function rewind () {
reset($this->data);
}
public function valid () {
return key($this->data) !== null;
}
public function count () {
return count($this->data);
}
public function offsetSet($offset, $value) {
$this->data[$offset] = $value;
}
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetUnset($offset) {
unset($this->data[$offset]);
}
public function offsetGet($offset) {
return isset($this->data[$offset]) ?
$this->data[$offset] : null;
}
}