Skip to content
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
44 changes: 44 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -2100,4 +2100,48 @@ public static function flushCache()
static::$camelCache = [];
static::$studlyCache = [];
}

/**
* Determines if a string is null or contains no characters.
*
* @param string|null $string
* @return bool
*/
public static function nullOrEmpty($string)
{
return $string === null || $string === '';
}

/**
* Determines if a string is null or contains only whitespace characters.
*
* @param string|null $string
* @return bool
*/
public static function nullOrWhitespace($string)
{
return $string === null || trim($string) === '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking about multi-byte support, maybe, using mb_trim() would be safer here

Suggested change
return $string === null || trim($string) === '';
return $string === null || mb_trim($string) === '';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has it been a conscious decision to not support modifying the second parameter to trim(), $characters?

}

/**
* Determines if a string is not null and contains characters.
*
* @param string|null $string
* @return bool
*/
public static function notNullOrEmpty($string)
{
return ! static::nullOrEmpty($string);
}

/**
* Determines if a string is not null and does not contain only whitespace characters.
*
* @param string|null $string
* @return bool
*/
public static function notNullOrWhitespace($string)
{
return ! static::nullOrWhitespace($string);
}
}
108 changes: 108 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,114 @@ public function count(): int

$this->assertSame('UserGroups', Str::pluralPascal('UserGroup', $countable));
}

#[DataProvider('nullOrEmptyProvider')]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for using data providers—that makes the test output so much better to read and debug 🙏

public function testNullOrEmpty($value, $expected)
{
$this->assertEquals($expected, Str::nullOrEmpty($value));
}

#[DataProvider('notNullOrEmptyProvider')]
public function testNotNullOrEmpty($value, $expected)
{
$this->assertEquals($expected, Str::notNullOrEmpty($value));
}

#[DataProvider('nullOrWhitespaceProvider')]
public function testNullOrWhitespace($value, $expected)
{
$this->assertEquals($expected, Str::nullOrWhitespace($value));
}

#[DataProvider('notNullOrWhitespaceProvider')]
public function testNotNullOrWhitespace($value, $expected)
{
$this->assertEquals($expected, Str::notNullOrWhitespace($value));
}

public static function nullOrEmptyProvider()
{
return [
'null value' => [null, true],
'empty string' => ['', true],
'single space' => [' ', false],
'multiple spaces' => [' ', false],
'tab character' => ["\t", false],
'newline character' => ["\n", false],
'zero string' => ['0', false],
'simple text' => ['hello', false],
'text with spaces' => ['hello world', false],
'single character' => ['a', false],
'number string' => ['123', false],
'special characters' => ['!@#$', false],
];
}

public static function notNullOrEmptyProvider()
{
return [
'null value' => [null, false],
'empty string' => ['', false],
'single space' => [' ', true],
'multiple spaces' => [' ', true],
'tab character' => ["\t", true],
'newline character' => ["\n", true],
'zero string' => ['0', true],
'simple text' => ['hello', true],
'text with spaces' => ['hello world', true],
'single character' => ['a', true],
'number string' => ['123', true],
'special characters' => ['!@#$', true],
];
}

public static function nullOrWhitespaceProvider()
{
return [
'null value' => [null, true],
'empty string' => ['', true],
'single space' => [' ', true],
'multiple spaces' => [' ', true],
'tab character' => ["\t", true],
'newline character' => ["\n", true],
'carriage return' => ["\r", true],
'mixed whitespace' => [" \t\n\r ", true],
'zero string' => ['0', false],
'simple text' => ['hello', false],
'text with spaces' => ['hello world', false],
'text with leading space' => [' hello', false],
'text with trailing space' => ['hello ', false],
'text with surrounding spaces' => [' hello ', false],
'single character' => ['a', false],
'number string' => ['123', false],
'special characters' => ['!@#$', false],
'whitespace with period' => [' . ', false],
];
}

public static function notNullOrWhitespaceProvider()
{
return [
'null value' => [null, false],
'empty string' => ['', false],
'single space' => [' ', false],
'multiple spaces' => [' ', false],
'tab character' => ["\t", false],
'newline character' => ["\n", false],
'carriage return' => ["\r", false],
'mixed whitespace' => [" \t\n\r ", false],
'zero string' => ['0', true],
'simple text' => ['hello', true],
'text with spaces' => ['hello world', true],
'text with leading space' => [' hello', true],
'text with trailing space' => ['hello ', true],
'text with surrounding spaces' => [' hello ', true],
'single character' => ['a', true],
'number string' => ['123', true],
'special characters' => ['!@#$', true],
'whitespace with period' => [' . ', true],
];
}
}

class StringableObjectStub
Expand Down