1 | <?php |
||
13 | class Collection implements CollectionInterface |
||
14 | { |
||
15 | /** |
||
16 | * The source data |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $data = []; |
||
21 | |||
22 | /** |
||
23 | * Create new collection |
||
24 | * |
||
25 | * @param array $items Pre-populate collection with this key-value array |
||
26 | */ |
||
27 | public function __construct(array $items = []) |
||
31 | |||
32 | /** |
||
33 | * Set collection item |
||
34 | * |
||
35 | * @param string $key The data key |
||
36 | * @param mixed $value The data value |
||
37 | */ |
||
38 | public function set($key, $value) |
||
42 | |||
43 | /** |
||
44 | * Get collection item for key |
||
45 | * |
||
46 | * @param string $key The data key |
||
47 | * @param mixed $default The default value to return if data key does not exist |
||
48 | * |
||
49 | * @return mixed The key's value, or the default value |
||
50 | */ |
||
51 | public function get($key, $default = null) |
||
55 | |||
56 | /** |
||
57 | * Add item to collection, replacing existing items with the same data key |
||
58 | * |
||
59 | * @param array $items Key-value array of data to append to this collection |
||
60 | */ |
||
61 | public function replace(array $items) |
||
67 | |||
68 | /** |
||
69 | * Get all items in collection |
||
70 | * |
||
71 | * @return array The collection's source data |
||
72 | */ |
||
73 | public function all() |
||
77 | |||
78 | /** |
||
79 | * Get collection keys |
||
80 | * |
||
81 | * @return array The collection's source data keys |
||
82 | */ |
||
83 | public function keys() |
||
87 | |||
88 | /** |
||
89 | * Does this collection have a given key? |
||
90 | * |
||
91 | * @param string $key The data key |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function has($key) |
||
99 | |||
100 | /** |
||
101 | * Remove item from collection |
||
102 | * |
||
103 | * @param string $key The data key |
||
104 | */ |
||
105 | public function remove($key) |
||
109 | |||
110 | /** |
||
111 | * Remove all items from collection |
||
112 | */ |
||
113 | public function clear() |
||
117 | |||
118 | /******************************************************************************** |
||
119 | * ArrayAccess interface |
||
120 | *******************************************************************************/ |
||
121 | |||
122 | /** |
||
123 | * Does this collection have a given key? |
||
124 | * |
||
125 | * @param string $key The data key |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function offsetExists($key) |
||
133 | |||
134 | /** |
||
135 | * Get collection item for key |
||
136 | * |
||
137 | * @param string $key The data key |
||
138 | * |
||
139 | * @return mixed The key's value, or the default value |
||
140 | */ |
||
141 | public function offsetGet($key) |
||
145 | |||
146 | /** |
||
147 | * Set collection item |
||
148 | * |
||
149 | * @param string $key The data key |
||
150 | * @param mixed $value The data value |
||
151 | */ |
||
152 | public function offsetSet($key, $value) |
||
156 | |||
157 | /** |
||
158 | * Remove item from collection |
||
159 | * |
||
160 | * @param string $key The data key |
||
161 | */ |
||
162 | public function offsetUnset($key) |
||
166 | |||
167 | /** |
||
168 | * Get number of items in collection |
||
169 | * |
||
170 | * @return int |
||
171 | */ |
||
172 | public function count() |
||
176 | |||
177 | /******************************************************************************** |
||
178 | * IteratorAggregate interface |
||
179 | *******************************************************************************/ |
||
180 | |||
181 | /** |
||
182 | * Get collection iterator |
||
183 | * |
||
184 | * @return \ArrayIterator |
||
185 | */ |
||
186 | public function getIterator() |
||
190 | } |
||
191 |