Skip to content

Commit 9e3c5e6

Browse files
committed
Implemented ArrayAccess, IteratorAggregate and Countable in ArrObj
1 parent 7297dfa commit 9e3c5e6

File tree

2 files changed

+112
-22
lines changed

2 files changed

+112
-22
lines changed

src/ArrObj.php

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
namespace Minwork\Helper;
44

55
use ArrayAccess;
6+
use ArrayIterator;
67
use 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()
@@ -22,40 +25,40 @@
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
}

test/ArrObj/ArrObjTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,59 @@ public function testThrowsExceptionOnInvalidMethod()
8686
/** @noinspection PhpUndefinedMethodInspection */
8787
(new ArrObj())->invalidMethodTest();
8888
}
89+
90+
/**
91+
* @param array $array
92+
*
93+
* @dataProvider arrayProvider
94+
*/
95+
public function testArrayAccess($array)
96+
{
97+
$obj = new ArrObj($array);
98+
$key = Arr::getFirstKey($array);
99+
100+
$this->assertSame($array[$key], $obj[$key]);
101+
$this->assertSame(isset($array[$key]), isset($obj[$key]));
102+
$this->assertSame(isset($array['non_existent_key_643543543']), isset($obj['non_existent_key_643543543']));
103+
104+
$newKey = 'very_long_unique_key';
105+
$newValue = 'dump_value_123';
106+
$this->assertSame($array[$newKey] = $newValue, $obj[$newKey] = $newValue);
107+
$this->assertSame($array[$newKey], $obj[$newKey]);
108+
$this->assertSame(isset($array[$newKey]), isset($obj[$newKey]));
109+
110+
unset($array[$newKey], $obj[$newKey]);
111+
$this->assertSame(isset($array[$newKey]), isset($obj[$newKey]));
112+
}
113+
114+
/**
115+
* @param array $array
116+
*
117+
* @dataProvider arrayProvider
118+
*/
119+
public function testArrayCount($array)
120+
{
121+
$obj = new ArrObj($array);
122+
123+
$this->assertSame(count($array), count($obj));
124+
$firstKey = Arr::getFirstKey($array);
125+
$lastKey = Arr::getLastKey($array);
126+
127+
unset($array[$firstKey], $obj[$firstKey], $array[$lastKey], $obj[$lastKey]);
128+
$this->assertSame(count($array), count($obj));
129+
}
130+
131+
/**
132+
* @param array $array
133+
*
134+
* @dataProvider arrayProvider
135+
*/
136+
public function testArrayIterator($array)
137+
{
138+
$obj = new ArrObj($array);
139+
140+
foreach ($obj as $key => $value) {
141+
$this->assertSame($array[$key], $value);
142+
}
143+
}
89144
}

0 commit comments

Comments
 (0)