From b021a59476b7610895c45b3027bba763305139b7 Mon Sep 17 00:00:00 2001 From: Reza Salarmehr Date: Sun, 23 Jul 2017 13:38:21 +1000 Subject: [PATCH 1/2] Collection replace and replaceRecursively functions --- src/Illuminate/Support/Collection.php | 24 ++++++++++++++++++++++++ tests/Support/SupportCollectionTest.php | 15 +++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 54689cb840c2..511db8f421c0 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -1659,4 +1659,28 @@ public function __get($key) return new HigherOrderCollectionProxy($this, $key); } + + /** + * Replaces elements from passed arrays into the collection + * + * @param mixed $items + * @return static + * @see http://php.net/manual/en/function.array-replace-recursive.php + */ + public function replace($items, $recursive = false) + { + return new static(array_replace($this->items, $this->getArrayableItems($items))); + } + + /** + * Replaces elements from passed arrays into the collection recursively + * + * @param mixed $items + * @return static + * @see http://php.net/manual/en/function.array-replace-recursive.php + */ + public function replaceRecursively($items) + { + return new static(array_replace_recursive($this->items, $this->getArrayableItems($items))); + } } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 5a40b19ecae9..9613f62c9237 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -2153,6 +2153,21 @@ public function testUnlessDefault() $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); } + + public function testReplace() + { + $c = new Collection(['foo' => 'x', 'bar' => 'y']); + $this->assertEquals(['foo' => 'f', 'bar' => 'y', 'baz' => 'z'], $c->replace(new Collection(['foo' => 'f', 'baz' => 'z']))->all()); + } + + public function testReplaceCollectionRecursively() + { + $base = ['citrus' => ["orange"], 'berries' => ["blackberry", "raspberry"],]; + $replacements = ['citrus' => ['pineapple'], 'berries' => ['blueberry']]; + $expect = ['citrus' => ['pineapple'], 'berries' => ['blueberry', 'raspberry']]; + $c = new Collection($base); + $this->assertEquals($expect, $c->replaceRecursively(new Collection($replacements))->all()); + } } class TestSupportCollectionHigherOrderItem From 25506f6896394399545551d2f767a82b383007ca Mon Sep 17 00:00:00 2001 From: Reza Date: Sun, 23 Jul 2017 17:53:11 +1000 Subject: [PATCH 2/2] Update Collection.php --- src/Illuminate/Support/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 511db8f421c0..526086378ba1 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -1667,7 +1667,7 @@ public function __get($key) * @return static * @see http://php.net/manual/en/function.array-replace-recursive.php */ - public function replace($items, $recursive = false) + public function replace($items) { return new static(array_replace($this->items, $this->getArrayableItems($items))); }