3
* Flight: An extensible micro-framework.
5
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
6
* @license MIT, http://flightphp.com/license
12
* The Collection class allows you to access a set of data
13
* using both array and object notation.
15
class Collection implements \ArrayAccess, \Iterator, \Countable {
26
* @param array $data Initial data
28
public function __construct(array $data = array()) {
35
* @param string $key Key
38
public function __get($key) {
39
return isset($this->data[$key]) ? $this->data[$key] : null;
45
* @param string $key Key
46
* @param mixed $value Value
48
public function __set($key, $value) {
49
$this->data[$key] = $value;
53
* Checks if an item exists.
55
* @param string $key Key
56
* @return bool Item status
58
public function __isset($key) {
59
return isset($this->data[$key]);
65
* @param string $key Key
67
public function __unset($key) {
68
unset($this->data[$key]);
72
* Gets an item at the offset.
74
* @param string $offset Offset
77
public function offsetGet($offset) {
78
return isset($this->data[$offset]) ? $this->data[$offset] : null;
82
* Sets an item at the offset.
84
* @param string $offset Offset
85
* @param mixed $value Value
87
public function offsetSet($offset, $value) {
88
if (is_null($offset)) {
89
$this->data[] = $value;
92
$this->data[$offset] = $value;
97
* Checks if an item exists at the offset.
99
* @param string $offset Offset
100
* @return bool Item status
102
public function offsetExists($offset) {
103
return isset($this->data[$offset]);
107
* Removes an item at the offset.
109
* @param string $offset Offset
111
public function offsetUnset($offset) {
112
unset($this->data[$offset]);
116
* Resets the collection.
118
public function rewind() {
123
* Gets current collection item.
125
* @return mixed Value
127
public function current() {
128
return current($this->data);
132
* Gets current collection key.
134
* @return mixed Value
136
public function key() {
137
return key($this->data);
141
* Gets the next collection value.
143
* @return mixed Value
145
public function next()
147
return next($this->data);
151
* Checks if the current collection key is valid.
153
* @return bool Key status
155
public function valid()
157
$key = key($this->data);
158
return ($key !== NULL && $key !== FALSE);
162
* Gets the size of the collection.
164
* @return int Collection size
166
public function count() {
167
return sizeof($this->data);
171
* Gets the item keys.
173
* @return array Collection keys
175
public function keys() {
176
return array_keys($this->data);
180
* Gets the collection data.
182
* @return array Collection data
184
public function getData() {
189
* Sets the collection data.
191
* @param array $data New collection data
193
public function setData(array $data) {
198
* Removes all items from the collection.
200
public function clear() {
201
$this->data = array();