From a5ab6fd388734a11298971ad74a39d74617de86d Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 13 Jan 2022 09:51:52 -0500 Subject: [PATCH 1/3] Add complimentary contain methods to Str --- src/Illuminate/Support/Str.php | 24 ++++++++++++++++++++++++ tests/Support/SupportStrTest.php | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 6c377897dcf..744b841f53a 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -207,6 +207,30 @@ public static function containsAll($haystack, array $needles) return true; } + /** + * Determine if a given string does not contain a given substring. + * + * @param string $haystack + * @param string|string[] $needles + * @return bool + */ + public static function doesntContain($haystack, $needles) + { + return ! static::contains(...func_get_args()); + } + + /** + * Determine if a given string does not contain any array values. + * + * @param string $haystack + * @param string[] $needles + * @return bool + */ + public static function doesntContainAny($haystack, array $needles) + { + return ! static::containsAll(...func_get_args()); + } + /** * Determine if a given string ends with a given substring. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index de2e60349d0..855437ae759 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -215,6 +215,18 @@ public function testStrContains() $this->assertFalse(Str::contains('', '')); } + public function testStrDoesntContain() + { + $this->assertTrue(Str::doesntContain('taylor', 'xxx')); + $this->assertTrue(Str::doesntContain('taylor', ['xxx'])); + $this->assertTrue(Str::doesntContain('taylor', '')); + $this->assertTrue(Str::doesntContain('', '')); + $this->assertFalse(Str::doesntContain('taylor', 'ylo')); + $this->assertFalse(Str::doesntContain('taylor', 'taylor')); + $this->assertFalse(Str::doesntContain('taylor', ['ylo'])); + $this->assertFalse(Str::doesntContain('taylor', ['xxx', 'ylo'])); + } + public function testStrContainsAll() { $this->assertTrue(Str::containsAll('taylor otwell', ['taylor', 'otwell'])); @@ -222,6 +234,13 @@ public function testStrContainsAll() $this->assertFalse(Str::containsAll('taylor otwell', ['taylor', 'xxx'])); } + public function testStrDoesntContainAny() + { + $this->assertFalse(Str::doesntContainAny('taylor otwell', ['taylor', 'otwell'])); + $this->assertFalse(Str::doesntContainAny('taylor otwell', ['taylor'])); + $this->assertTrue(Str::doesntContainAny('taylor otwell', ['taylor', 'xxx'])); + } + public function testParseCallback() { $this->assertEquals(['Class', 'method'], Str::parseCallback('Class@method', 'foo')); From c8dd2b194bf43db87a074ac738948d48408c79cc Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 13 Jan 2022 14:18:51 -0500 Subject: [PATCH 2/3] Remove redundant doesntContainAny --- src/Illuminate/Support/Str.php | 12 ------------ tests/Support/SupportStrTest.php | 8 +------- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 744b841f53a..6f10c3de9bf 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -219,18 +219,6 @@ public static function doesntContain($haystack, $needles) return ! static::contains(...func_get_args()); } - /** - * Determine if a given string does not contain any array values. - * - * @param string $haystack - * @param string[] $needles - * @return bool - */ - public static function doesntContainAny($haystack, array $needles) - { - return ! static::containsAll(...func_get_args()); - } - /** * Determine if a given string ends with a given substring. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 855437ae759..8ca241a3f27 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -219,6 +219,7 @@ public function testStrDoesntContain() { $this->assertTrue(Str::doesntContain('taylor', 'xxx')); $this->assertTrue(Str::doesntContain('taylor', ['xxx'])); + $this->assertTrue(Str::doesntContain('taylor', ['foo', 'bar'])); $this->assertTrue(Str::doesntContain('taylor', '')); $this->assertTrue(Str::doesntContain('', '')); $this->assertFalse(Str::doesntContain('taylor', 'ylo')); @@ -234,13 +235,6 @@ public function testStrContainsAll() $this->assertFalse(Str::containsAll('taylor otwell', ['taylor', 'xxx'])); } - public function testStrDoesntContainAny() - { - $this->assertFalse(Str::doesntContainAny('taylor otwell', ['taylor', 'otwell'])); - $this->assertFalse(Str::doesntContainAny('taylor otwell', ['taylor'])); - $this->assertTrue(Str::doesntContainAny('taylor otwell', ['taylor', 'xxx'])); - } - public function testParseCallback() { $this->assertEquals(['Class', 'method'], Str::parseCallback('Class@method', 'foo')); From 5cc497485fc760fc6a6c4099e8f15b949996dd70 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 13 Jan 2022 16:44:51 -0500 Subject: [PATCH 3/3] Add to Stringable class --- src/Illuminate/Support/Stringable.php | 12 ++++++++++++ tests/Support/SupportStrTest.php | 2 +- tests/Support/SupportStringableTest.php | 13 +++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index de073f48bd0..1d8fcf53b0e 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -173,6 +173,18 @@ public function dirname($levels = 1) return new static(dirname($this->value, $levels)); } + /** + * Determine if a given string does not contain a given substring. + * + * @param string $haystack + * @param string|string[] $needles + * @return bool + */ + public function doesntContain($needles) + { + return Str::doesntContain($this->value, $needles); + } + /** * Determine if a given string ends with a given substring. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 8ca241a3f27..8418e51a171 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -217,11 +217,11 @@ public function testStrContains() public function testStrDoesntContain() { + $this->assertTrue(Str::doesntContain('', '')); $this->assertTrue(Str::doesntContain('taylor', 'xxx')); $this->assertTrue(Str::doesntContain('taylor', ['xxx'])); $this->assertTrue(Str::doesntContain('taylor', ['foo', 'bar'])); $this->assertTrue(Str::doesntContain('taylor', '')); - $this->assertTrue(Str::doesntContain('', '')); $this->assertFalse(Str::doesntContain('taylor', 'ylo')); $this->assertFalse(Str::doesntContain('taylor', 'taylor')); $this->assertFalse(Str::doesntContain('taylor', ['ylo'])); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index f3afdef9b18..89a4d6b4e72 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -551,6 +551,19 @@ public function testContains() $this->assertFalse($this->stringable('taylor')->contains('')); } + public function testStrDoesntContain() + { + $this->assertTrue($this->stringable('')->doesntContain('')); + $this->assertTrue($this->stringable('taylor')->doesntContain('xxx')); + $this->assertTrue($this->stringable('taylor')->doesntContain(['xxx'])); + $this->assertTrue($this->stringable('taylor')->doesntContain(['foo', 'bar'])); + $this->assertTrue($this->stringable('taylor')->doesntContain('')); + $this->assertFalse($this->stringable('taylor')->doesntContain('ylo')); + $this->assertFalse($this->stringable('taylor')->doesntContain('taylor')); + $this->assertFalse($this->stringable('taylor')->doesntContain(['ylo'])); + $this->assertFalse($this->stringable('taylor')->doesntContain(['xxx', 'ylo'])); + } + public function testContainsAll() { $this->assertTrue($this->stringable('taylor otwell')->containsAll(['taylor', 'otwell']));