|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Enzyme\Collection; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use Countable; |
|
7
|
|
|
use Iterator; |
|
8
|
|
|
|
|
9
|
|
|
abstract class BaseCollection implements ArrayAccess, Iterator, Countable |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The list of internal items stored by this collection. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $items; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The current iterator position. |
|
20
|
|
|
* |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $position; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Instantiate a new collection with the optional provided array. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $items |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(array $items = []) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->items = $items; |
|
33
|
|
|
$this->position = 0; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/* |
|
37
|
|
|
| -------------------------------------------------------------------------- |
|
38
|
|
|
| Implementation of `ArrayAccess` interface. |
|
39
|
|
|
| -------------------------------------------------------------------------- |
|
40
|
|
|
*/ |
|
41
|
|
|
|
|
42
|
|
|
public function offsetExists($offset) |
|
43
|
|
|
{ |
|
44
|
|
|
return true === isset($this->items[$offset]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function offsetGet($offset) |
|
48
|
|
|
{ |
|
49
|
|
|
return (true === isset($this->items[$offset])) |
|
50
|
|
|
? $this->items[$offset] |
|
51
|
|
|
: null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function offsetSet($offset, $value) |
|
55
|
|
|
{ |
|
56
|
|
|
if (true === is_null($offset)) { |
|
57
|
|
|
$this->items[] = $value; |
|
58
|
|
|
} else { |
|
59
|
|
|
$this->items[$offset] = $value; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function offsetUnset($offset) |
|
64
|
|
|
{ |
|
65
|
|
|
unset($this->items[$offset]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/* |
|
69
|
|
|
| -------------------------------------------------------------------------- |
|
70
|
|
|
| Implementation of `Iterator` interface. |
|
71
|
|
|
| -------------------------------------------------------------------------- |
|
72
|
|
|
*/ |
|
73
|
|
|
|
|
74
|
|
|
public function current() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->items[$this->position]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function key() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->position; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function next() |
|
85
|
|
|
{ |
|
86
|
|
|
++$this->position; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function rewind() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->position = 0; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function valid() |
|
95
|
|
|
{ |
|
96
|
|
|
return true === isset($this->items[$this->position]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/* |
|
100
|
|
|
| -------------------------------------------------------------------------- |
|
101
|
|
|
| Implementation of `Countable` interface. |
|
102
|
|
|
| -------------------------------------------------------------------------- |
|
103
|
|
|
*/ |
|
104
|
|
|
|
|
105
|
|
|
public function count() |
|
106
|
|
|
{ |
|
107
|
|
|
return count($this->items); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|