Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add complimentary Str::doesntContain #40396

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
Expand Down