3
3
namespace Minwork \Helper ;
4
4
5
5
use ArrayAccess ;
6
+ use ArrayIterator ;
6
7
use BadMethodCallException ;
8
+ use Countable ;
9
+ use IteratorAggregate ;
7
10
8
11
/**
9
12
* Class ArrObj
10
13
* @package Minwork\Helper
11
14
* @method bool has(mixed $keys)
12
15
* @method bool hasKeys(mixed $keys, bool $strict = false)
13
16
* @method mixed get(mixed $keys, $default = null)
14
- * @method self set(mixed $keys, mixed $value)
15
- * @method self remove(mixed $keys)
17
+ * @method ArrObj set(mixed $keys, mixed $value)
18
+ * @method ArrObj remove(mixed $keys)
16
19
*
17
20
* @method bool check(mixed|callable $condition, bool $strict = false)
18
21
* @method bool isEmpty()
22
25
* @method bool isNested()
23
26
* @method bool isArrayOfArrays()
24
27
*
25
- * @method self map(callable $callback, int $mode = Arr::MAP_ARRAY_KEY_VALUE)
26
- * @method self mapObjects(string $method, ...$args)
28
+ * @method ArrObj map(callable $callback, int $mode = Arr::MAP_ARRAY_KEY_VALUE)
29
+ * @method ArrObj mapObjects(string $method, ...$args)
27
30
*
28
- * @method self filterByKeys(mixed $keys, bool $exclude = false)
29
- * @method self filterObjects(string $method, ...$args)
31
+ * @method ArrObj filterByKeys(mixed $keys, bool $exclude = false)
32
+ * @method ArrObj filterObjects(string $method, ...$args)
30
33
*
31
- * @method self group(string|int $key)
32
- * @method self groupObjects(string $method, ...$args)
34
+ * @method ArrObj group(string|int $key)
35
+ * @method ArrObj groupObjects(string $method, ...$args)
33
36
*
34
- * @method self orderByKeys(mixed $keys, bool $appendUnmatched = true)
35
- * @method self sortByKeys(mixed $keys = null, bool $assoc = true)
36
- * @method self sortObjects(string $method, ...$args)
37
+ * @method ArrObj orderByKeys(mixed $keys, bool $appendUnmatched = true)
38
+ * @method ArrObj sortByKeys(mixed $keys = null, bool $assoc = true)
39
+ * @method ArrObj sortObjects(string $method, ...$args)
37
40
*
38
- * @method self sum(array ...$arrays)
39
- * @method self diffObjects(array $array, array ...$arrays)
40
- * @method self intersectObjects(array $array, array ...$arrays)
41
+ * @method ArrObj sum(array ...$arrays)
42
+ * @method ArrObj diffObjects(array $array, array ...$arrays)
43
+ * @method ArrObj intersectObjects(array $array, array ...$arrays)
41
44
*
42
- * @method self flatten(?int $depth = null, bool $assoc = false)
43
- * @method self flattenSingle()
45
+ * @method ArrObj flatten(?int $depth = null, bool $assoc = false)
46
+ * @method ArrObj flattenSingle()
44
47
*
45
48
* @method int getDepth()
46
- * @method self clone()
49
+ * @method ArrObj clone()
47
50
* @method mixed random(int $count = 1)
48
- * @method self shuffle()
49
- * @method self nth(int $A = 1, int $B = 0)
50
- * @method self even()
51
- * @method self odd()
51
+ * @method ArrObj shuffle()
52
+ * @method ArrObj nth(int $A = 1, int $B = 0)
53
+ * @method ArrObj even()
54
+ * @method ArrObj odd()
52
55
*
53
56
* @method string|int|null getFirstKey()
54
57
* @method string|int|null getLastKey()
55
58
* @method mixed getFirstValue()
56
59
* @method mixed getLastValue()
57
60
*/
58
- class ArrObj
61
+ class ArrObj implements IteratorAggregate, ArrayAccess, Countable
59
62
{
60
63
const METHODS = [
61
64
'has ' ,
@@ -164,4 +167,36 @@ public function setArray($array): self
164
167
$ this ->array = $ array ;
165
168
return $ this ;
166
169
}
170
+
171
+ public function offsetExists ($ offset )
172
+ {
173
+ return array_key_exists ($ offset , $ this ->array );
174
+ }
175
+
176
+ public function offsetGet ($ offset )
177
+ {
178
+ return $ this ->array [$ offset ] ?? null ;
179
+ }
180
+
181
+ public function offsetSet ($ offset , $ value )
182
+ {
183
+ return $ this ->array [$ offset ] = $ value ;
184
+ }
185
+
186
+ public function offsetUnset ($ offset )
187
+ {
188
+ if (isset ($ this ->array [$ offset ])) {
189
+ unset($ this ->array [$ offset ]);
190
+ }
191
+ }
192
+
193
+ public function getIterator ()
194
+ {
195
+ return new ArrayIterator ($ this ->array );
196
+ }
197
+
198
+ public function count ()
199
+ {
200
+ return count ($ this ->array );
201
+ }
167
202
}
0 commit comments