forked from kzap/Eden-PHP-Type
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayType.php
More file actions
516 lines (452 loc) · 12.6 KB
/
ArrayType.php
File metadata and controls
516 lines (452 loc) · 12.6 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php //-->
/*
* This file is part of the Type package of the Eden PHP Library.
* (c) 2013-2014 Openovate Labs
*
* Copyright and license information can be found at LICENSE
* distributed with this package.
*/
namespace Eden\Type;
use Eden\Type\Argument;
/**
* Array object
*
* @vendor Eden
* @package Type
* @author Christian Blanquera cblanquera@openovate.com
*/
class ArrayType extends Base implements \ArrayAccess, \Iterator, \Serializable, \Countable
{
protected $data = array();
protected $original = array();
/**
* Try to see if this method is a PHP defined array function
*
* @param *string
* @param *array
* @return mixed
*/
public function __call($name, $args)
{
Argument::i()
//argument 1 must be a string
->test(1, 'string')
//argument 2 must be an array
->test(2, 'array');
//if the method starts with get
if(strpos($name, 'get') === 0) {
//getUserName('-')
$separator = '_';
if(isset($args[0]) && is_scalar($args[0])) {
$separator = (string) $args[0];
}
$key = preg_replace("/([A-Z0-9])/", $separator."$1", $name);
//get rid of get
$key = strtolower(substr($key, 3+strlen($separator)));
if(isset($this->data[$key])) {
return $this->data[$key];
}
return null;
} else if (strpos($name, 'set') === 0) {
//setUserName('Chris', '-')
$separator = '_';
if(isset($args[1]) && is_scalar($args[1])) {
$separator = (string) $args[1];
}
$key = preg_replace("/([A-Z0-9])/", $separator."$1", $name);
//get rid of set
$key = strtolower(substr($key, 3+strlen($separator)));
$this->__set($key, isset($args[0]) ? $args[0] : null);
return $this;
}
return parent::__call($name, $args);
}
/**
* Preloads the array
*
* @param array
* @return mixed
*/
public function __construct($data = array())
{
//if there is more arguments or data is not an array
if(func_num_args() > 1 || !is_array($data)) {
//just get the args
$data = func_get_args();
}
parent::__construct($data);
}
/**
* Allow object property magic to redirect to the data variable
*
* @param *string
* @param *mixed
* @return void
*/
public function __set($name, $value)
{
//argument 1 must be a string
Argument::i()->test(1, 'string');
$this->data[$name] = $value;
}
/**
* If we output this to string we should see it as json
*
* @return string
*/
public function __toString()
{
return json_encode($this->get());
}
/**
* Copies the value of source key into destination key
*
* @param string
* @param string
* @return Eden\Type\Type\ArrayType
*/
public function copy($source, $destination)
{
Argument::i()
//argument 1 must be a string
->test(1, 'string')
//argument 2 must be a string
->test(2, 'string');
$this->data[$destination] = $this->data[$source];
return $this;
}
/**
* returns size using the Countable interface
*
* @return string
*/
public function count()
{
return count($this->data);
}
/**
* Removes a row in an array and adjusts all the indexes
*
* @param *scalar the key to leave out
* @return Eden\Type\Type\ArrayType
*/
public function cut($key)
{
//argument 1 must be scalar
Argument::i()->test(1, 'scalar');
//if nothing to cut
if(!isset($this->data[$key])) {
//do nothing
return $this;
}
//unset the value
unset($this->data[$key]);
//reindex the list
$this->data = array_values($this->data);
return $this;
}
/**
* Returns the current item
* For Iterator interface
*
* @return void
*/
public function current()
{
return current($this->data);
}
/**
* Loops through returned result sets
*
* @param *callable
* @return Eden\Type\Type\ArrayType
*/
public function each($callback)
{
Argument::i()->test(1, 'callable');
foreach($this->data as $key => $value) {
call_user_func($callback, $key, $value);
}
return $this;
}
/**
* Returns if the data is empty
*
* @return bool
*/
public function isEmpty()
{
return empty($this->data);
}
/**
* Returns th current position
* For Iterator interface
*
* @return void
*/
public function key()
{
return key($this->data);
}
/**
* Increases the position
* For Iterator interface
*
* @return void
*/
public function next()
{
next($this->data);
}
/**
* isset using the ArrayAccess interface
*
* @param *scalar|null|bool
* @return bool
*/
public function offsetExists($offset)
{
//argument 1 must be scalar, null or bool
Argument::i()->test(1, 'scalar', 'null', 'bool');
return isset($this->data[$offset]);
}
/**
* returns data using the ArrayAccess interface
*
* @param *scalar|null|bool
* @return bool
*/
public function offsetGet($offset)
{
//argument 1 must be scalar, null or bool
Argument::i()->test(1, 'scalar', 'null', 'bool');
return isset($this->data[$offset]) ? $this->data[$offset] : null;
}
/**
* Sets data using the ArrayAccess interface
*
* @param *scalar|null|bool
* @param mixed
* @return void
*/
public function offsetSet($offset, $value)
{
//argument 1 must be scalar, null or bool
Argument::i()->test(1, 'scalar', 'null', 'bool');
if (is_null($offset)) {
$this->data[] = $value;
} else {
$this->data[$offset] = $value;
}
return $this;
}
/**
* unsets using the ArrayAccess interface
*
* @param *scalar|null|bool
* @return bool
*/
public function offsetUnset($offset)
{
//argument 1 must be scalar, null or bool
Argument::i()->test(1, 'scalar', 'null', 'bool');
unset($this->data[$offset]);
return $this;
}
/**
* Inserts a row in an array after the given index and adjusts all the indexes
*
* @param *scalar the key we are looking for to past after
* @param *mixed the value to paste
* @param scalar the key to paste along with the value
* @return Eden\Type\Type\ArrayType
*/
public function paste($after, $value, $key = null)
{
//Argument test
Argument::i()
//Argument 1 must be a scalar
->test(1, 'scalar')
//Argument 3 must be a scalar or null
->test(3, 'scalar', 'null');
$list = array();
//for each row
foreach($this->data as $i => $val) {
//add this row back to the list
$list[$i] = $val;
//if this is not the key we are
//suppose to paste after
if($after != $i) {
//do nothing more
continue;
}
//if there was a key involved
if(!is_null($key)) {
//lets add the new value
$list[$key] = $value;
continue;
}
//lets add the new value
$list[] = $arrayValue;
}
//if there was no key involved
if(is_null($key)) {
//reindex the array
$list = array_values($list);
}
//give it back
$this->data = $list;
return $this;
}
/**
* Rewinds the position
* For Iterator interface
*
* @return void
*/
public function rewind()
{
reset($this->data);
}
/**
* returns serialized data using the Serializable interface
*
* @return string
*/
public function serialize()
{
return json_encode($this->data);
}
/**
* Sets data
*
* @param array
* @return Eden\Type\Type\ArrayType
*/
public function set($value)
{
//argument 1 must be an array
//we test for array this way because the parent
//does not specify data type
Argument::i()->test(1, 'array');
$this->data = $value;
return $this;
}
/**
* sets data using the Serializable interface
*
* @param string
* @return Eden\Type\Type\ArrayType
*/
public function unserialize($data)
{
//argument 1 must be a string
Argument::i()->test(1, 'string');
$this->data = json_decode($data, true);
return $this;
}
/**
* Validates whether if the index is set
* For Iterator interface
*
* @return bool
*/
public function valid()
{
return isset($this->data[$this->key()]);
}
/**
* A PHP method excepts arrays in 3 ways, first argument,
* last argument and as a reference
*
* @param *string
* @return string
*/
protected function getMethodType($name)
{
if(isset(self::$methods[$name])) {
return self::$methods[$name];
}
if(isset(self::$methods['array_'.$name])) {
$name = 'array_'.$name;
return self::$methods[$name];
}
$uncamel = strtolower(preg_replace("/([A-Z])/", "_$1", $name));
if(isset(self::$methods[$uncamel])) {
$name = $uncamel;
return self::$methods[$name];
}
if(isset(self::$methods['array_'.$uncamel])) {
$name = 'array_'.$uncamel;
return self::$methods[$name];
}
return false;
}
protected static $methods = array(
'array_change_key_case' => self::PRE,
'array_chunk' => self::PRE,
'array_combine' => self::PRE,
'array_count_datas' => self::PRE,
'array_diff_assoc' => self::PRE,
'array_diff_key' => self::PRE,
'array_diff_uassoc' => self::PRE,
'array_diff_ukey' => self::PRE,
'array_diff' => self::PRE,
'array_fill_keys' => self::PRE,
'array_filter' => self::PRE,
'array_flip' => self::PRE,
'array_intersect_assoc' => self::PRE,
'array_intersect_key' => self::PRE,
'array_intersect_uassoc' => self::PRE,
'array_intersect_ukey' => self::PRE,
'array_intersect' => self::PRE,
'array_keys' => self::PRE,
'array_merge_recursive' => self::PRE,
'array_merge' => self::PRE,
'array_pad' => self::PRE,
'array_reverse' => self::PRE,
'array_shift' => self::PRE,
'array_slice' => self::PRE,
'array_splice' => self::PRE,
'array_sum' => self::PRE,
'array_udiff_assoc' => self::PRE,
'array_udiff_uassoc' => self::PRE,
'array_udiff' => self::PRE,
'array_uintersect_assoc' => self::PRE,
'array_uintersect_uassoc' => self::PRE,
'array_uintersect' => self::PRE,
'array_unique' => self::PRE,
'array_datas' => self::PRE,
'count' => self::PRE,
'current' => self::PRE,
'each' => self::PRE,
'end' => self::PRE,
'extract' => self::PRE,
'key' => self::PRE,
'next' => self::PRE,
'prev' => self::PRE,
'sizeof' => self::PRE,
'array_fill' => self::POST,
'array_map' => self::POST,
'array_search' => self::POST,
'compact' => self::POST,
'implode' => self::POST,
'in_array' => self::POST,
'array_unshift' => self::REFERENCE,
'array_walk_recursive' => self::REFERENCE,
'array_walk' => self::REFERENCE,
'arsort' => self::REFERENCE,
'asort' => self::REFERENCE,
'krsort' => self::REFERENCE,
'ksort' => self::REFERENCE,
'natcasesort' => self::REFERENCE,
'natsort' => self::REFERENCE,
'reset' => self::REFERENCE,
'rsort' => self::REFERENCE,
'shuffle' => self::REFERENCE,
'sort' => self::REFERENCE,
'uasort' => self::REFERENCE,
'uksort' => self::REFERENCE,
'usort' => self::REFERENCE,
'array_push' => self::REFERENCE);
}