Skip to content

Commit c931c9f

Browse files
committed
Make arrays iterable
1 parent bab61bf commit c931c9f

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

src/BaseArray.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Sevavietl\Arrays;
44

5-
class BaseArray implements \ArrayAccess
5+
class BaseArray implements \ArrayAccess, \Iterator
66
{
77
protected $container;
88
protected $undefinedOffsetAction;
99

10+
protected $iteration = 0;
11+
1012
public function __construct($array = [])
1113
{
1214
$this->container = $array;
@@ -65,7 +67,7 @@ public function offsetGet($offset) {
6567
}
6668

6769
public function offsetSet($offset, $value) {
68-
if (is_null($offset)) {
70+
if (null === $offset) {
6971
$this->container[] = $value;
7072
} else {
7173
$this->container[$offset] = $value;
@@ -75,4 +77,31 @@ public function offsetSet($offset, $value) {
7577
public function offsetUnset($offset) {
7678
unset($this->container[$offset]);
7779
}
80+
81+
public function current()
82+
{
83+
return $this->offsetGet($this->key());
84+
}
85+
86+
public function next()
87+
{
88+
$this->iteration += 1;
89+
next($this->container);
90+
}
91+
92+
public function key()
93+
{
94+
return \key($this->container);
95+
}
96+
97+
public function valid()
98+
{
99+
return $this->iteration < \count($this->container);
100+
}
101+
102+
public function rewind()
103+
{
104+
$this->iteration = 0;
105+
reset($this->container);
106+
}
78107
}

src/OneOffArray.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,35 @@
44

55
class OneOffArray extends BaseArray
66
{
7+
/** @var int|string|null */
8+
private $key;
9+
710
public function offsetGet($offset)
811
{
912
$value = parent::offsetGet($offset);
1013
unset($this->container[$offset]);
1114

1215
return $value;
1316
}
17+
18+
public function next()
19+
{
20+
$this->iteration += 1;
21+
$this->key = \key($this->container);
22+
}
23+
24+
public function key()
25+
{
26+
return $this->key;
27+
}
28+
29+
public function valid()
30+
{
31+
return 0 !== \count($this->container);
32+
}
33+
34+
public function rewind()
35+
{
36+
$this->key = \key($this->container);
37+
}
1438
}

tests/unit/BaseArrayTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ public function testThrowsExceptionOnUndefinedOffset()
4949
$arr = new BaseArray([1, 2, 3]);
5050
$arr[3];
5151
}
52+
53+
public function testItIsIterable()
54+
{
55+
$arr = new BaseArray($initial = [1, 2, 3]);
56+
57+
foreach ($arr as $key => $value) {
58+
$this->assertEquals($initial[$key], $value);
59+
}
60+
61+
$this->assertEquals($initial, $arr->toArray());
62+
}
5263
}

tests/unit/CompositeKeyArrayTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,18 @@ public function arrayDataProviderForUnsetTesting()
156156
[['foo' => ['bar' => ['baz']]], ['foo', 'bar', 0], ['foo' => ['bar' => []]]],
157157
];
158158
}
159+
160+
public function testItIsIterable()
161+
{
162+
$arr = new CompositeKeyArray($initial = [
163+
'foo' => ['bar' => ['baz']],
164+
'quux' => 'foobar',
165+
]);
166+
167+
foreach ($arr as $key => $value) {
168+
$this->assertEquals($initial[$key], $value);
169+
}
170+
171+
$this->assertEquals($initial, $arr->toArray());
172+
}
159173
}

tests/unit/OneOffArrayTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testOffsetGet($array, $offset, $value)
1818
$value,
1919
$array[$offset]
2020
);
21-
21+
2222
$this->assertFalse(isset($array[$offset]));
2323
}
2424

@@ -32,4 +32,15 @@ public function arrayDataProviderForGetTesting()
3232
[new CompositeKeyArray(['foo' => ['bar' => 'baz']]), ['foo', 'bar'], 'baz'],
3333
];
3434
}
35+
36+
public function testItIsIterable()
37+
{
38+
$arr = new OneOffArray($initial = [1, 2, 3]);
39+
40+
foreach ($arr as $key => $value) {
41+
$this->assertEquals($initial[$key], $value);
42+
}
43+
44+
$this->assertEmpty($arr->toArray());
45+
}
3546
}

0 commit comments

Comments
 (0)