1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait Enumerable |
4
|
|
|
* |
5
|
|
|
* @filesource Enumerable.php |
6
|
|
|
* @created 28.06.2017 |
7
|
|
|
* @package chillerlan\Traits |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Traits; |
14
|
|
|
|
15
|
|
|
use Closure; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/ |
19
|
|
|
*/ |
20
|
|
|
trait Enumerable{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $array = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $offset = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/toArray/ |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
* |
37
|
|
|
* @codeCoverageIgnore |
38
|
|
|
*/ |
39
|
|
|
public function __toArray():array { |
40
|
|
|
return $this->array; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/each/ |
45
|
|
|
* |
46
|
|
|
* @param callable $callback |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function __each($callback){ |
51
|
|
|
$this->__map($callback); |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/collect/ |
58
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/map/ |
59
|
|
|
* |
60
|
|
|
* @param callable $callback |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
* @throws \chillerlan\Traits\TraitException |
64
|
|
|
*/ |
65
|
|
|
public function __map($callback):array { |
66
|
|
|
|
67
|
|
|
if(!is_callable($callback)){ |
68
|
|
|
throw new TraitException('invalid callback'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$return = []; |
72
|
|
|
|
73
|
|
|
foreach($this->array as $index => $element){ |
74
|
|
|
$return[$index] = call_user_func_array($callback, [$element, $index]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @link http://api.prototypejs.org/language/Array/prototype/reverse/ |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function __reverse(){ |
86
|
|
|
$this->array = array_reverse($this->array); |
87
|
|
|
$this->offset = 0; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function __last(){ |
96
|
|
|
return $this->array[count($this->array) - 1]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function __clear(){ |
103
|
|
|
$this->array = []; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @link http://api.prototypejs.org/language/Array/prototype/inspect/ |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function __inspect():string { |
114
|
|
|
return print_r($this->array, true); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/findAll/ |
119
|
|
|
* |
120
|
|
|
* @param \Closure $callback |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
* @throws \chillerlan\Traits\TraitException |
124
|
|
|
*/ |
125
|
|
|
public function __findAll(Closure $callback):array{ |
126
|
|
|
|
127
|
|
|
if(!is_callable($callback)){ |
128
|
|
|
throw new TraitException('invalid callback'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$return = []; |
132
|
|
|
|
133
|
|
|
foreach($this->array as $index => $element){ |
134
|
|
|
|
135
|
|
|
if(call_user_func_array($callback, [$element, $index]) === true){ |
136
|
|
|
$return[] = $element; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/reject/ |
146
|
|
|
* |
147
|
|
|
* @param \Closure $callback |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
* @throws \chillerlan\Traits\TraitException |
151
|
|
|
*/ |
152
|
|
|
public function __reject(Closure $callback):array{ |
153
|
|
|
|
154
|
|
|
if(!is_callable($callback)){ |
155
|
|
|
throw new TraitException('invalid callback'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$return = []; |
159
|
|
|
|
160
|
|
|
foreach($this->array as $index => $element){ |
161
|
|
|
|
162
|
|
|
if(call_user_func_array($callback, [$element, $index]) !== true){ |
163
|
|
|
$return[] = $element; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $return; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param array $y |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function __equal(array $y):bool{ |
177
|
|
|
|
178
|
|
|
if(count($this->array) !== count($y)){ |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$diff = 0; |
183
|
|
|
|
184
|
|
|
foreach($this->array as $kx => $vx){ |
185
|
|
|
$diff |= $vx ^ $y[$kx]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return ((($diff - 1) >> 31) & 1) === 1; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|