33namespace Minwork \Helper ;
44
55use ArrayAccess ;
6+ use ArrayIterator ;
67use BadMethodCallException ;
8+ use Countable ;
9+ use IteratorAggregate ;
710
811/**
912 * Class ArrObj
1013 * @package Minwork\Helper
1114 * @method bool has(mixed $keys)
1215 * @method bool hasKeys(mixed $keys, bool $strict = false)
1316 * @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)
1619 *
1720 * @method bool check(mixed|callable $condition, bool $strict = false)
1821 * @method bool isEmpty()
2225 * @method bool isNested()
2326 * @method bool isArrayOfArrays()
2427 *
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)
2730 *
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)
3033 *
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)
3336 *
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)
3740 *
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)
4144 *
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()
4447 *
4548 * @method int getDepth()
46- * @method self clone()
49+ * @method ArrObj clone()
4750 * @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()
5255 *
5356 * @method string|int|null getFirstKey()
5457 * @method string|int|null getLastKey()
5558 * @method mixed getFirstValue()
5659 * @method mixed getLastValue()
5760 */
58- class ArrObj
61+ class ArrObj implements IteratorAggregate, ArrayAccess, Countable
5962{
6063 const METHODS = [
6164 'has ' ,
@@ -164,4 +167,36 @@ public function setArray($array): self
164167 $ this ->array = $ array ;
165168 return $ this ;
166169 }
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+ }
167202}
0 commit comments