From b32eee2f85bee1025ac5aa482bd3fa1f2cc01268 Mon Sep 17 00:00:00 2001 From: Vincent Gabriel Date: Mon, 19 Jun 2017 16:32:01 -0700 Subject: [PATCH 1/2] Added array_intersect_key to the Collection object - 19680 --- src/Illuminate/Support/Collection.php | 11 +++++++++++ tests/Support/SupportCollectionTest.php | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 4a879bd36673..7008e104eb2b 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -624,6 +624,17 @@ public function intersect($items) return new static(array_intersect($this->items, $this->getArrayableItems($items))); } + /** + * Intersect the collection with the given items by key + * + * @param mixed $items + * @return static + */ + public function intersectKey($items) + { + return new static(array_intersect_key($this->items, $this->getArrayableItems($items))); + } + /** * Determine if the collection is empty or not. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index a05adfa1f16f..15f5a40e6860 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -564,6 +564,18 @@ public function testIntersectCollection() $this->assertEquals(['first_word' => 'Hello'], $c->intersect(new Collection(['first_world' => 'Hello', 'last_word' => 'World']))->all()); } + public function testIntersectKeyNull() + { + $c = new Collection(['id' => 1, 'first_word' => 'Hello']); + $this->assertEquals([], $c->intersectKey(null)->all()); + } + + public function testIntersectKeyCollection() + { + $c = new Collection(['id' => 1, 'first_word' => 'Hello']); + $this->assertEquals(['first_word' => 'Hello'], $c->intersectKey(new Collection(['first_word' => 'Hello', 'last_word' => 'World']))->all()); + } + public function testUnique() { $c = new Collection(['Hello', 'World', 'World']); From a78855878e520970e0c9fc75f43496c8ca5e6f23 Mon Sep 17 00:00:00 2001 From: Vincent Gabriel Date: Mon, 19 Jun 2017 16:36:07 -0700 Subject: [PATCH 2/2] style ci --- 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 7008e104eb2b..498b1b4bd280 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -625,7 +625,7 @@ public function intersect($items) } /** - * Intersect the collection with the given items by key + * Intersect the collection with the given items by key. * * @param mixed $items * @return static