1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
<?php /** * Klein (klein.php) - A fast & flexible router for PHP * * @author Chris O'Hara <cohara87@gmail.com> * @author Trevor Suarez (Rican7) (contributor and v2 refactorer) * @copyright (c) Chris O'Hara * @link https://github.com/klein/klein.php * @license MIT */
namespace Klein\DataCollection;
use ArrayAccess; use ArrayIterator; use Countable; use IteratorAggregate;
/** * DataCollection * * A generic collection class to contain array-like data, specifically * designed to work with HTTP data (request params, session data, etc) * * Inspired by @fabpot's Symfony 2's HttpFoundation * @link https://github.com/symfony/HttpFoundation/blob/master/ParameterBag.php */ class DataCollection implements IteratorAggregate, ArrayAccess, Countable {
/** * Class properties */
/** * Collection of data attributes * * @type array */ protected $attributes = array();
/** * Methods */
/** * Constructor * * @param array $attributes The data attributes of this collection */ public function __construct(array $attributes = array()) { $this->attributes = $attributes; }
/** * Returns all of the key names in the collection * * If an optional mask array is passed, this only * returns the keys that match the mask * * @param array $mask The parameter mask array * @param boolean $fill_with_nulls Whether or not to fill the returned array with * values to match the given mask, even if they don't exist in the collection * @return array */ public function keys($mask = null, $fill_with_nulls = true) { if (null !== $mask) { // Support a more "magical" call if (!is_array($mask)) { $mask = func_get_args(); }
/* * Make sure that the returned array has at least the values * passed into the mask, since the user will expect them to exist */ if ($fill_with_nulls) { $keys = $mask; } else { $keys = array(); }
/* * Remove all of the values from the keys * that aren't in the passed mask */ return array_intersect( array_keys($this->attributes), $mask ) + $keys; }
return array_keys($this->attributes); }
/** * Returns all of the attributes in the collection * * If an optional mask array is passed, this only * returns the keys that match the mask * * @param array $mask The parameter mask array * @param boolean $fill_with_nulls Whether or not to fill the returned array with * values to match the given mask, even if they don't exist in the collection * @return array */ public function all($mask = null, $fill_with_nulls = true) { if (null !== $mask) { // Support a more "magical" call if (!is_array($mask)) { $mask = func_get_args(); }
/* * Make sure that each key in the mask has at least a * null value, since the user will expect the key to exist */ if ($fill_with_nulls) { $attributes = array_fill_keys($mask, null); } else { $attributes = array(); }
/* * Remove all of the keys from the attributes * that aren't in the passed mask */ return array_intersect_key( $this->attributes, array_flip($mask) ) + $attributes; }
return $this->attributes; }
/** * Return an attribute of the collection * * Return a default value if the key doesn't exist * * @param string $key The name of the parameter to return * @param mixed $default_val The default value of the parameter if it contains no value * @return mixed */ public function get($key, $default_val = null) { if (isset($this->attributes[$key])) { return $this->attributes[$key]; }
return $default_val; }
/** * Set an attribute of the collection * * @param string $key The name of the parameter to set * @param mixed $value The value of the parameter to set * @return DataCollection */ public function set($key, $value) { $this->attributes[$key] = $value;
return $this; }
/** * Replace the collection's attributes * * @param array $attributes The attributes to replace the collection's with * @return DataCollection */ public function replace(array $attributes = array()) { $this->attributes = $attributes;
return $this; }
/** * Merge attributes with the collection's attributes * * Optionally allows a second boolean parameter to merge the attributes * into the collection in a "hard" manner, using the "array_replace" * method instead of the usual "array_merge" method * * @param array $attributes The attributes to merge into the collection * @param boolean $hard Whether or not to make the merge "hard" * @return DataCollection */ public function merge(array $attributes = array(), $hard = false) { // Don't waste our time with an "array_merge" call if the array is empty if (!empty($attributes)) { // Hard merge? if ($hard) { $this->attributes = array_replace( $this->attributes, $attributes ); } else { $this->attributes = array_merge( $this->attributes, $attributes ); } }
return $this; }
/** * See if an attribute exists in the collection * * @param string $key The name of the parameter * @return boolean */ public function exists($key) { // Don't use "isset", since it returns false for null values return array_key_exists($key, $this->attributes); }
/** * Remove an attribute from the collection * * @param string $key The name of the parameter * @return void */ public function remove($key) { unset($this->attributes[$key]); }
/** * Clear the collection's contents * * Semantic alias of a no-argument `$this->replace` call * * @return DataCollection */ public function clear() { return $this->replace(); }
/** * Check if the collection is empty * * @return boolean */ public function isEmpty() { return empty($this->attributes); }
/** * A quick convenience method to get an empty clone of the * collection. Great for dependency injection. :) * * @return DataCollection */ public function cloneEmpty() { $clone = clone $this; $clone->clear();
return $clone; }
/* * Magic method implementations */
/** * Magic "__get" method * * Allows the ability to arbitrarily request an attribute from * this instance while treating it as an instance property * * @see get() * @param string $key The name of the parameter to return * @return mixed */ public function __get($key) { return $this->get($key); }
/** * Magic "__set" method * * Allows the ability to arbitrarily set an attribute from * this instance while treating it as an instance property * * @see set() * @param string $key The name of the parameter to set * @param mixed $value The value of the parameter to set * @return void */ public function __set($key, $value) { $this->set($key, $value); }
/** * Magic "__isset" method * * Allows the ability to arbitrarily check the existence of an attribute * from this instance while treating it as an instance property * * @see exists() * @param string $key The name of the parameter * @return boolean */ public function __isset($key) { return $this->exists($key); }
/** * Magic "__unset" method * * Allows the ability to arbitrarily remove an attribute from * this instance while treating it as an instance property * * @see remove() * @param string $key The name of the parameter * @return void */ public function __unset($key) { $this->remove($key); }
/* * Interface required method implementations */
/** * Get the aggregate iterator * * IteratorAggregate interface required method * * @see \IteratorAggregate::getIterator() * @return ArrayIterator */ public function getIterator() { return new ArrayIterator($this->attributes); }
/** * Get an attribute via array syntax * * Allows the access of attributes of this instance while treating it like an array * * @see \ArrayAccess::offsetGet() * @see get() * @param string $key The name of the parameter to return * @return mixed */ public function offsetGet($key) { return $this->get($key); }
/** * Set an attribute via array syntax * * Allows the access of attributes of this instance while treating it like an array * * @see \ArrayAccess::offsetSet() * @see set() * @param string $key The name of the parameter to set * @param mixed $value The value of the parameter to set * @return void */ public function offsetSet($key, $value) { $this->set($key, $value); }
/** * Check existence an attribute via array syntax * * Allows the access of attributes of this instance while treating it like an array * * @see \ArrayAccess::offsetExists() * @see exists() * @param string $key The name of the parameter * @return boolean */ public function offsetExists($key) { return $this->exists($key); }
/** * Remove an attribute via array syntax * * Allows the access of attributes of this instance while treating it like an array * * @see \ArrayAccess::offsetUnset() * @see remove() * @param string $key The name of the parameter * @return void */ public function offsetUnset($key) { $this->remove($key); }
/** * Count the attributes via a simple "count" call * * Allows the use of the "count" function (or any internal counters) * to simply count the number of attributes in the collection. * * @see \Countable::count() * @return int */ public function count() { return count($this->attributes); } }
|