File tree 4 files changed +38
-7
lines changed
4 files changed +38
-7
lines changed Original file line number Diff line number Diff line change @@ -26,9 +26,13 @@ Create instance of `Collection`.
26
26
27
27
Add an item to the collection. If ` $key ` already exists in the collection it will be overwritten.
28
28
29
- ##### ` get(string $key, mixed $default = null) `
29
+ ##### ` get(string|array $key, mixed $default = null) `
30
30
31
- Return the value of an item from the collection. If ` $key ` doesn't exist in the collection then ` $default ` will be returned.
31
+ Get an item from the collection. Returns ` $default ` if item cannot be found.
32
+
33
+ Passing an array of item keys for the value of ` $key ` will result in multiple
34
+ items being returned. Keys that are missing from the collection will be
35
+ returned with a value of ` $default ` .
32
36
33
37
##### ` has(string $key) : bool `
34
38
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " nonamephp/php7-common" ,
3
3
"description" : " A collection of common PHP 7 libraries" ,
4
- "version" : " 0.3.0 " ,
4
+ "version" : " 0.3.1 " ,
5
5
"license" : " MIT" ,
6
6
"authors" : [
7
7
{
Original file line number Diff line number Diff line change @@ -71,16 +71,28 @@ public function set($key, $value)
71
71
/**
72
72
* Get an item from the collection. Returns $default if item cannot be found.
73
73
*
74
- * @param string $key
74
+ * Passing an array of item keys for the value of $key will result in multiple
75
+ * items being returned. Keys that are missing from the collection will be
76
+ * returned with a value of $default.
77
+ *
78
+ * @param mixed $key
75
79
* @param mixed $default
76
80
* @return mixed Will return $default if cannot find item
77
81
*/
78
82
public function get ($ key , $ default = null )
79
83
{
80
- if (isset ($ this ->items [$ key ])) {
81
- return $ this ->items [$ key ];
84
+ if (is_array ($ key )) {
85
+ // Get multiple items by their key
86
+ $ items = [];
87
+ foreach ($ key as $ k ) {
88
+ $ items [$ k ] = isset ($ this ->items [$ k ]) ? $ this ->items [$ k ] : $ default ;
89
+ }
90
+
91
+ return $ items ;
92
+ } else {
93
+ // Get a single item by its key
94
+ return isset ($ this ->items [$ key ]) ? $ this ->items [$ key ] : $ default ;
82
95
}
83
- return $ default ;
84
96
}
85
97
86
98
/**
Original file line number Diff line number Diff line change @@ -221,4 +221,19 @@ public function testMagicMethods()
221
221
unset($ collection ['key1 ' ]);
222
222
$ this ->assertFalse ($ collection ->has ('key1 ' ));
223
223
}
224
+
225
+ /**
226
+ * @covers Collection::get
227
+ */
228
+ public function testGetMultipleItems ()
229
+ {
230
+ $ collection = new Collection (['key1 ' => 'value1 ' , 'key2 ' => 'value2 ' ]);
231
+ $ values = $ collection ->get (['key1 ' , 'key2 ' , 'key3 ' ]);
232
+
233
+ $ this ->assertTrue (array_key_exists ('key1 ' , $ values ) && $ values ['key1 ' ] == 'value1 ' );
234
+ $ this ->assertTrue (array_key_exists ('key2 ' , $ values ) && $ values ['key2 ' ] == 'value2 ' );
235
+
236
+ // Assert the key3 equals the default (null)
237
+ $ this ->assertTrue (array_key_exists ('key3 ' , $ values ) && $ values ['key3 ' ] == null );
238
+ }
224
239
}
You can’t perform that action at this time.
0 commit comments