forked from jsebrech/php-o
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayClass.php
More file actions
146 lines (113 loc) · 2.75 KB
/
Copy pathArrayClass.php
File metadata and controls
146 lines (113 loc) · 2.75 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
<?php
namespace O;
/**
* Supporting class for the a() function
*/
class ArrayClass implements \IteratorAggregate, \ArrayAccess {
private $a;
function __construct(&$a) {
$this->a =& $a;
}
function count() {
return count($this->a);
}
function has($needle, $strict = FALSE) {
return in_array($needle, $this->a, $strict);
}
function search($needle, $strict = FALSE) {
return array_search($needle, $this->a, $strict);
}
function shift() {
return array_shift($this->a);
}
function unshift() {
$args = func_get_args();
for ($i = count($args) - 1; $i >= 0; $i--) {
array_unshift($this->a, $args[$i]);
};
return count($this->a);
}
function key_exists($key) {
return array_key_exists($key, $this->a);
}
function implode($glue = "") {
return implode($this->a, $glue);
}
function keys() {
return array_keys($this->a);
}
function values() {
return array_values($this->a);
}
function pop() {
return array_pop($this->a);
}
function push() {
$args = func_get_args();
for ($i = 0; $i < count($args); $i++) {
array_push($this->a, $args[$i]);
};
return count($this->a);
}
function slice($offset, $length = NULL, $preserve_keys = false) {
return array_slice($this->a, $offset, $length, $preserve_keys);
}
function splice($offset, $length = 0, $replacement = NULL) {
if ($replacement == NULL) $replacement = array();
return array_splice($this->a, $offset, $length, $replacement);
}
function merge() {
return call_user_func_array("array_merge", array_merge(array($this->a), func_get_args()));
}
function map($callback) {
$args = func_get_args();
$params = a($args)->slice(1);
a($params)->unshift($callback, $this->a);
return call_user_func_array("array_map", $params);
}
function reduce($callable, $initial = NULL) {
return array_reduce($this->a, $callable, $initial);
}
function filter($callable = "") {
return array_filter($this->a, $callable);
}
function sum() {
return array_sum($this->a);
}
function end() {
return end($this->a);
}
function raw() {
return $this->a;
}
// IteratorAggregate
function getIterator() {
$o = new \ArrayObject($this->a);
return $o->getIterator();
}
// ArrayAccess
// ArrayAccess
function offsetExists($offset) {
return isset($this->a[$offset]);
}
function offsetGet($offset) {
return $this->a[$offset];
}
function offsetSet($offset, $value) {
$this->a[$offset] = $value;
}
function offsetUnset($offset) {
unset($this->a[$offset]);
}
}
/**
* @param string $p
* @return \O\ArrayClass
*/
function a(&$p) {
if ($p instanceof \O\ArrayClass) {
return $p;
} else {
return new \O\ArrayClass($p);
}
}