Skip to content

Commit ee2b8ec

Browse files
committed
Merge branch 'release/0.3.1'
2 parents 0bf78bf + 0e699d7 commit ee2b8ec

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ Create instance of `Collection`.
2626

2727
Add an item to the collection. If `$key` already exists in the collection it will be overwritten.
2828

29-
##### `get(string $key, mixed $default = null)`
29+
##### `get(string|array $key, mixed $default = null)`
3030

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`.
3236

3337
##### `has(string $key) : bool`
3438

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nonamephp/php7-common",
33
"description": "A collection of common PHP 7 libraries",
4-
"version": "0.3.0",
4+
"version": "0.3.1",
55
"license": "MIT",
66
"authors": [
77
{

src/Collection.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,28 @@ public function set($key, $value)
7171
/**
7272
* Get an item from the collection. Returns $default if item cannot be found.
7373
*
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
7579
* @param mixed $default
7680
* @return mixed Will return $default if cannot find item
7781
*/
7882
public function get($key, $default = null)
7983
{
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;
8295
}
83-
return $default;
8496
}
8597

8698
/**

tests/CollectionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,19 @@ public function testMagicMethods()
221221
unset($collection['key1']);
222222
$this->assertFalse($collection->has('key1'));
223223
}
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+
}
224239
}

0 commit comments

Comments
 (0)