Skip to content

Commit 1172fc8

Browse files
committed
Add Collection::make()
1 parent b2b3c44 commit 1172fc8

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ echo $collection->get('user_email');
7878

7979
Creates an instance of `Collection`. Optionally pass an associative array for `$items` to prefill the collection with items.
8080

81+
##### `static make(...$arrays) : Collection`
82+
83+
Make a collection from one or more arrays.
84+
8185
##### `set(string $key, mixed $value) : void`
8286

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

src/Collection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ public function __unset($key)
5757
///////////////////////////////////
5858
// Collection Methods
5959

60+
/**
61+
* Make a collection from one or more arrays.
62+
*
63+
* @param array ...$arrays
64+
* @return Collection
65+
*/
66+
public static function make(array ...$arrays): Collection
67+
{
68+
$items = [];
69+
foreach ($arrays as $arr) {
70+
$items += $arr;
71+
}
72+
return new self($items);
73+
}
74+
6075
/**
6176
* Add an item to the collection.
6277
*

tests/CollectionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ public function isNotCollectionItemsProvider()
6767
///////////////////////////////////
6868
// Tests
6969

70+
/**
71+
* @covers Collection::make
72+
*/
73+
public function testMake()
74+
{
75+
$items_1 = ['key1' => 'val1'];
76+
$items_2 = ['key2' => 'val2'];
77+
$items_3 = ['key3' => 'val3'];
78+
$collection = Collection::make($items_1, $items_2, $items_3);
79+
$this->assertTrue($collection->has('key1'));
80+
$this->assertTrue($collection->has('key2'));
81+
$this->assertTrue($collection->has('key3'));
82+
}
83+
7084
/**
7185
* @covers Collection::count
7286
*/

0 commit comments

Comments
 (0)