Skip to content

Commit 01a9aa6

Browse files
committed
wip
1 parent 993e9b3 commit 01a9aa6

2 files changed

Lines changed: 61 additions & 26 deletions

File tree

src/Support/Collections/Collection.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Permafrost\PhpCodeSearch\Support\Arr;
66

77
/** @codeCoverageIgnore */
8-
class Collection implements Arrayable, \ArrayAccess
8+
class Collection implements Arrayable, \ArrayAccess, \Countable, \Iterator
99
{
10+
use Enumerable;
11+
1012
protected $items = [];
1113

1214
public function __construct($items = [])
@@ -26,7 +28,7 @@ public function count(): int
2628

2729
public function each(callable $callback): self
2830
{
29-
foreach ($this->getIterator() as $key => $item) {
31+
foreach ($this as $key => $item) {
3032
if ($callback($item, $key) === false) {
3133
break;
3234
}
@@ -239,28 +241,4 @@ public function getIterator(): \ArrayIterator
239241
{
240242
return new \ArrayIterator($this->items);
241243
}
242-
243-
public function offsetExists($offset)
244-
{
245-
return isset($this->items[$offset]);
246-
}
247-
248-
public function offsetGet($offset)
249-
{
250-
return $this->items[$offset];
251-
}
252-
253-
public function offsetSet($key, $value)
254-
{
255-
if (is_null($key)) {
256-
$this->items[] = $value;
257-
} else {
258-
$this->items[$key] = $value;
259-
}
260-
}
261-
262-
public function offsetUnset($offset)
263-
{
264-
unset($this->items[$offset]);
265-
}
266244
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Support\Collections;
4+
5+
trait Enumerable
6+
{
7+
protected $position = 0;
8+
9+
public function offsetExists($offset)
10+
{
11+
return isset($this->items[$offset]);
12+
}
13+
14+
public function offsetGet($offset)
15+
{
16+
return $this->items[$offset];
17+
}
18+
19+
public function offsetSet($key, $value)
20+
{
21+
if (is_null($key)) {
22+
$this->items[] = $value;
23+
} else {
24+
$this->items[$key] = $value;
25+
}
26+
}
27+
28+
public function offsetUnset($offset)
29+
{
30+
unset($this->items[$offset]);
31+
}
32+
33+
public function rewind()
34+
{
35+
$this->position = 0;
36+
}
37+
38+
public function current()
39+
{
40+
return $this->items[$this->position];
41+
}
42+
43+
public function key()
44+
{
45+
return $this->position;
46+
}
47+
48+
public function next()
49+
{
50+
++$this->position;
51+
}
52+
53+
public function valid()
54+
{
55+
return isset($this->items[$this->position]);
56+
}
57+
}

0 commit comments

Comments
 (0)