1 | <?php |
||
40 | class Collection implements \ArrayAccess, \Iterator, \Countable |
||
41 | { |
||
42 | /** |
||
43 | * The raw data of this Collection |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $data = array( ); |
||
48 | |||
49 | /** |
||
50 | * The current position of the array pointer |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $pointer = 0; |
||
55 | |||
56 | /** |
||
57 | * Returns the current element |
||
58 | * |
||
59 | * @return mixed The current element |
||
60 | */ |
||
61 | 7 | public function current() |
|
62 | { |
||
63 | 7 | return $this->offsetGet($this->pointer); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Returns the current key |
||
68 | * |
||
69 | * @return int The current key |
||
70 | */ |
||
71 | public function key() |
||
75 | |||
76 | /** |
||
77 | * Advances the internal pointer by one |
||
78 | */ |
||
79 | 5 | public function next() |
|
80 | { |
||
81 | 5 | $this->pointer++; |
|
82 | |||
83 | 5 | return $this->valid() ? $this->offsetGet($this->pointer) : null; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Checks if the given offset is set |
||
88 | * |
||
89 | * @param scalar $offset The offset to check |
||
90 | * @return boolean True if the offset is set |
||
91 | */ |
||
92 | 29 | public function offsetExists($offset) |
|
96 | |||
97 | /** |
||
98 | * Returns the data at the given offset |
||
99 | * |
||
100 | * @param scalar $offset The offset to get |
||
101 | * @return mixed The value at the given offset |
||
102 | */ |
||
103 | 35 | public function offsetGet($offset) |
|
107 | |||
108 | /** |
||
109 | * Adds a new key-value pair to the Collection |
||
110 | * |
||
111 | * If the key already exists in the collection, its key-value is |
||
112 | * overwritten. |
||
113 | * |
||
114 | * @param scalar $offset The key to set |
||
115 | * @param mixed $value The value to set |
||
116 | */ |
||
117 | 20 | public function offsetSet($offset, $value) |
|
121 | |||
122 | /** |
||
123 | * Removes the given key-value pair from the Collection |
||
124 | * |
||
125 | * @param scalar $offset The key to unset |
||
126 | */ |
||
127 | public function offsetUnset($offset) |
||
131 | |||
132 | /** |
||
133 | * Resets the internal array pointer |
||
134 | */ |
||
135 | 7 | public function rewind() |
|
136 | { |
||
137 | 7 | $this->pointer = 0; |
|
138 | |||
139 | 7 | return $this->offsetGet(0); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Changes the position of the array pointer |
||
144 | * |
||
145 | * @param scalar $pos The position to seek to |
||
146 | * @return mixed The value at that new point |
||
147 | */ |
||
148 | public function seek($pos) |
||
154 | |||
155 | /** |
||
156 | * Checks if there are any more elements to be read from the |
||
157 | * Collection |
||
158 | * |
||
159 | * @return boolean True if there are one or more elements |
||
160 | */ |
||
161 | 7 | public function valid() |
|
162 | { |
||
163 | 7 | return $this->pointer < $this->size(); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Adds an element to the Collection, without specifying a key |
||
168 | * |
||
169 | * @param scalar $offset The key to set |
||
170 | * @param mixed $item The element to add |
||
171 | */ |
||
172 | 19 | public function add($offset, $item) |
|
176 | |||
177 | /** |
||
178 | * Push an element onto the end of the array |
||
179 | * |
||
180 | * @param mixed $item The element to add |
||
181 | */ |
||
182 | 21 | public function push($item) |
|
186 | |||
187 | /** |
||
188 | * Returns how many elements are in the Collection |
||
189 | * |
||
190 | * @return int The number of elements in the Collection |
||
191 | */ |
||
192 | 27 | public function size() |
|
196 | |||
197 | /** |
||
198 | * Returns how many elements are in the Collection |
||
199 | * |
||
200 | * @return int The number of elements in the Collection |
||
201 | */ |
||
202 | 4 | public function count() |
|
206 | |||
207 | /** |
||
208 | * Checks if the Collection is empty |
||
209 | * |
||
210 | * @return boolean True if the element is empty |
||
211 | */ |
||
212 | 2 | public function empty() |
|
216 | |||
217 | /** |
||
218 | * Clears the Collection of all data |
||
219 | */ |
||
220 | public function clear() |
||
224 | |||
225 | /** |
||
226 | * Magic Method which returns the Collection's data when it is |
||
227 | * passed to var_dump or similar |
||
228 | * |
||
229 | * This prevents the Collection from returning the large amount of |
||
230 | * extra stuff that is contained within Collection, such as the |
||
231 | * internal pointer and inherited interanal Model elements. |
||
232 | * |
||
233 | * @return array The data |
||
234 | */ |
||
235 | public function __debugInfo() |
||
239 | } |
||
240 |