Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add a mergeToTop() method #37237

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,25 @@ public function mapWithKeys(callable $callback)
* Merge the collection with the given items.
*
* @param mixed $items
* @param bool $appendToTop
* @return static
*/
public function merge($items)
public function merge($items, $appendToTop = false)
{
return new static(array_merge($this->items, $this->getArrayableItems($items)));
return $appendToTop
? new static(array_merge($this->getArrayableItems($items), $this->items))
: new static(array_merge($this->items, $this->getArrayableItems($items)));
}

/**
* Merge the collection, but append the given items at the top.
*
* @param mixed $items
* @return static
*/
public function mergeToTop($items)
{
return $this->merge($items, $appendToTop = true);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,17 @@ public function merge($items)
return $this->passthru('merge', func_get_args());
}

/**
* Merge the collection, but append the given items at the top.
*
* @param mixed $items
* @return static
*/
public function mergeToTop($items)
{
return $this->passthru('mergeToTop', func_get_args());
}

/**
* Recursively merge the collection with the given items.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function modelKeys()
* @param \ArrayAccess|array $items
* @return static
*/
public function merge($items)
public function merge($items, $appendToTop = false)
{
$dictionary = $this->getDictionary();

Expand Down
54 changes: 54 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,15 @@ public function testMergeArray($collection)
$this->assertEquals(['name' => 'Hello', 'id' => 1], $c->merge(['id' => 1])->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testMergeArrayWithoutKeys($collection)
{
$collection = new $collection(['Apple', 'Banana']);
$this->assertEquals(['Apple', 'Banana', 'Pineapple'], $collection->merge(['Pineapple'])->all());
}

/**
* @dataProvider collectionClassProvider
*/
Expand All @@ -1040,6 +1049,51 @@ public function testMergeCollection($collection)
$this->assertEquals(['name' => 'World', 'id' => 1], $c->merge(new $collection(['name' => 'World', 'id' => 1]))->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testMergeCollectionWithoutKeys($collection)
{
$collection = new $collection(['Banana', 'Apple']);
$this->assertEquals(['Banana', 'Apple', 'Pineapple'], $collection->merge(new $collection(['Pineapple']))->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testMergeArrayAppendToTop($collection)
{
$collection = new $collection(['Apple', 'Banana']);
$this->assertEquals(['Pineapple', 'Apple', 'Banana'], $collection->merge(['Pineapple'], $appendToTop = true)->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testMergeCollectionAppendToTop($collection)
{
$collection = new $collection(['Banana', 'Apple']);
$this->assertEquals(['Pineapple', 'Banana', 'Apple'], $collection->merge(new $collection(['Pineapple']), $appendToTop = true)->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testMergeArrayAppendToTopAsMethod($collection)
{
$collection = new $collection(['Apple', 'Banana']);
$this->assertEquals(['Pineapple', 'Apple', 'Banana'], $collection->mergeToTop(['Pineapple'])->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testMergeCollectionAppendToTopAsMethod($collection)
{
$collection = new $collection(['Banana', 'Apple']);
$this->assertEquals(['Pineapple', 'Banana', 'Apple'], $collection->mergeToTop(new $collection(['Pineapple']))->all());
}

/**
* @dataProvider collectionClassProvider
*/
Expand Down