1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: todd |
5
|
|
|
* Date: 12/11/17 |
6
|
|
|
* Time: 8:57 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Greenskies; |
10
|
|
|
|
11
|
|
|
use Greenskies\Exception\CollectionException; |
12
|
|
|
|
13
|
|
|
class Collection implements \Iterator, \Countable, \ArrayAccess |
14
|
|
|
{ |
15
|
|
|
private $items; |
16
|
|
|
private $position = 0; |
17
|
|
|
private $className = null; |
18
|
|
|
|
19
|
|
|
public function __construct(array $items = null, $className = null) |
20
|
|
|
{ |
21
|
|
|
$this->className = $className; |
22
|
|
|
foreach ($items as &$item) { |
23
|
|
|
$item = $this->createElement($item); |
24
|
|
|
} |
25
|
|
|
$this->items = $items; |
26
|
|
|
$this->position = 0; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function createElement($item) |
30
|
|
|
{ |
31
|
|
|
if ($this->className) { |
32
|
|
|
if (is_object($item)) { |
33
|
|
|
if (!is_a($item, $this->className)) { |
34
|
|
|
throw new CollectionException("Wrong class for collection"); |
35
|
|
|
} |
36
|
|
|
} else { |
37
|
|
|
$item = new $this->className($item); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
return $item; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return the current element |
45
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
46
|
|
|
* @return mixed Can return any type. |
47
|
|
|
* @since 5.0.0 |
48
|
|
|
*/ |
49
|
|
|
public function current() |
50
|
|
|
{ |
51
|
|
|
return $this->items[$this->position]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Move forward to next element |
56
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
57
|
|
|
* @return void Any returned value is ignored. |
58
|
|
|
* @since 5.0.0 |
59
|
|
|
*/ |
60
|
|
|
public function next() |
61
|
|
|
{ |
62
|
|
|
$this->position++; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the key of the current element |
67
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
68
|
|
|
* @return mixed scalar on success, or null on failure. |
69
|
|
|
* @since 5.0.0 |
70
|
|
|
*/ |
71
|
|
|
public function key() |
72
|
|
|
{ |
73
|
|
|
return $this->position; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks if current position is valid |
78
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
79
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
80
|
|
|
* Returns true on success or false on failure. |
81
|
|
|
* @since 5.0.0 |
82
|
|
|
*/ |
83
|
|
|
public function valid() |
84
|
|
|
{ |
85
|
|
|
return isset($this->items[$this->position]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Rewind the Iterator to the first element |
90
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
91
|
|
|
* @return void Any returned value is ignored. |
92
|
|
|
* @since 5.0.0 |
93
|
|
|
*/ |
94
|
|
|
public function rewind() |
95
|
|
|
{ |
96
|
|
|
$this->position = 0; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Whether a offset exists |
101
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
102
|
|
|
* @param mixed $offset <p> |
103
|
|
|
* An offset to check for. |
104
|
|
|
* </p> |
105
|
|
|
* @return boolean true on success or false on failure. |
106
|
|
|
* </p> |
107
|
|
|
* <p> |
108
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
109
|
|
|
* @since 5.0.0 |
110
|
|
|
*/ |
111
|
|
|
public function offsetExists($offset) |
112
|
|
|
{ |
113
|
|
|
return isset($this->items[$offset]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Offset to retrieve |
118
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
119
|
|
|
* @param mixed $offset <p> |
120
|
|
|
* The offset to retrieve. |
121
|
|
|
* </p> |
122
|
|
|
* @return mixed Can return all value types. |
123
|
|
|
* @since 5.0.0 |
124
|
|
|
*/ |
125
|
|
|
public function offsetGet($offset) |
126
|
|
|
{ |
127
|
|
|
return isset($this->items[$offset]) ? $this->items[$offset] : null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Offset to set |
132
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
133
|
|
|
* @param mixed $offset <p> |
134
|
|
|
* The offset to assign the value to. |
135
|
|
|
* </p> |
136
|
|
|
* @param mixed $value <p> |
137
|
|
|
* The value to set. |
138
|
|
|
* </p> |
139
|
|
|
* @return void |
140
|
|
|
* @since 5.0.0 |
141
|
|
|
*/ |
142
|
|
|
public function offsetSet($offset, $value) |
143
|
|
|
{ |
144
|
|
|
$value = $this->createElement($value); |
145
|
|
|
if (is_null($offset)) { |
146
|
|
|
$this->items[] = $value; |
147
|
|
|
} else { |
148
|
|
|
$this->items[$offset] = $value; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Offset to unset |
154
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
155
|
|
|
* @param mixed $offset <p> |
156
|
|
|
* The offset to unset. |
157
|
|
|
* </p> |
158
|
|
|
* @return void |
159
|
|
|
* @since 5.0.0 |
160
|
|
|
*/ |
161
|
|
|
public function offsetUnset($offset) |
162
|
|
|
{ |
163
|
|
|
unset($this->items[$offset]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Count elements of an object |
168
|
|
|
* @link http://php.net/manual/en/countable.count.php |
169
|
|
|
* @return int The custom count as an integer. |
170
|
|
|
* </p> |
171
|
|
|
* <p> |
172
|
|
|
* The return value is cast to an integer. |
173
|
|
|
* @since 5.1.0 |
174
|
|
|
*/ |
175
|
|
|
public function count() |
176
|
|
|
{ |
177
|
|
|
return count($this->items); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public static function createFromFile($file) |
181
|
|
|
{ |
182
|
|
|
$string = file_get_contents($file); |
183
|
|
|
return static::createFromString($string); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public static function createFromString($string, $className = null) |
187
|
|
|
{ |
188
|
|
|
$items = preg_split( '/\r\n|\r|\n/', $string); |
189
|
|
|
$array = []; |
190
|
|
|
foreach ($items as $item) { |
191
|
|
|
if ($item) { |
192
|
|
|
$array[] = $item; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
return new Collection($array, $className); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function push($item): self |
199
|
|
|
{ |
200
|
|
|
$this->items[] = $item; |
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function toArray(): array |
205
|
|
|
{ |
206
|
|
|
$array = []; |
207
|
|
|
foreach ($this as $item) { |
208
|
|
|
$array[] = $item; |
209
|
|
|
} |
210
|
|
|
return $array; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|