From bb659604dfe62a44a4ba88ce708af321022bb7d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Nabia=C5=82ek?= Date: Sat, 6 Aug 2016 10:06:39 +0200 Subject: [PATCH 1/2] Add containsStrict method --- src/Illuminate/Support/Collection.php | 22 ++++++++++++++++++ tests/Support/SupportCollectionTest.php | 30 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 1e341b39c57e..531ae52ad44c 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -171,6 +171,28 @@ public function contains($key, $value = null) return in_array($key, $this->items); } + /** + * Determine if an item exists in the collection using strict comparison. + * + * @param mixed $key + * @param mixed $value + * @return bool + */ + public function containsStrict($key, $value = null) + { + if (func_num_args() == 2) { + return $this->contains(function ($item) use ($key, $value) { + return data_get($item, $key) === $value; + }); + } + + if ($this->useAsCallable($key)) { + return ! is_null($this->first($key)); + } + + return in_array($key, $this->items, true); + } + /** * Get the items in the collection that are not present in the given items. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 7745400ecbc9..530f6b8ab5af 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1063,6 +1063,36 @@ public function testContains() $this->assertFalse($c->contains('foo')); } + public function testContainsStrict() + { + $c = new Collection([1, 3, 5,'02']); + + $this->assertTrue($c->containsStrict(1)); + $this->assertFalse($c->containsStrict(2)); + $this->assertTrue($c->containsStrict('02')); + $this->assertTrue($c->containsStrict(function ($value) { + return $value < 5; + })); + $this->assertFalse($c->containsStrict(function ($value) { + return $value > 5; + })); + + $c = new Collection([['v' => 1], ['v' => 3], ['v' => '04'], ['v' => 5]]); + + $this->assertTrue($c->containsStrict('v', 1)); + $this->assertFalse($c->containsStrict('v', 2)); + $this->assertFalse($c->containsStrict('v', 4)); + $this->assertTrue($c->containsStrict('v', '04')); + + $c = new Collection(['date', 'class', (object) ['foo' => 50], '']); + + $this->assertTrue($c->containsStrict('date')); + $this->assertTrue($c->containsStrict('class')); + $this->assertFalse($c->containsStrict('foo')); + $this->assertFalse($c->containsStrict(null)); + $this->assertTrue($c->containsStrict('')); + } + public function testGettingSumFromCollection() { $c = new Collection([(object) ['foo' => 50], (object) ['foo' => 50]]); From 9b95e19270459d9e204d941938e08d69eeca5988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Nabia=C5=82ek?= Date: Sat, 6 Aug 2016 10:13:03 +0200 Subject: [PATCH 2/2] Fix code style --- tests/Support/SupportCollectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 530f6b8ab5af..d03dd6d6f0e1 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1065,7 +1065,7 @@ public function testContains() public function testContainsStrict() { - $c = new Collection([1, 3, 5,'02']); + $c = new Collection([1, 3, 5, '02']); $this->assertTrue($c->containsStrict(1)); $this->assertFalse($c->containsStrict(2));