diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 6c377897dcf..6f10c3de9bf 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -207,6 +207,18 @@ 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 ends with a given substring. * 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 de2e60349d0..8418e51a171 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -215,6 +215,19 @@ public function testStrContains() $this->assertFalse(Str::contains('', '')); } + 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->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'])); 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']));