-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[12.x] Add nullish check methods to Str facade
#57599
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) === ''; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
|
|
||
| /** | ||
| * 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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1905,6 +1905,114 @@ public function count(): int | |
|
|
||
| $this->assertSame('UserGroups', Str::pluralPascal('UserGroup', $countable)); | ||
| } | ||
|
|
||
| #[DataProvider('nullOrEmptyProvider')] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
There was a problem hiding this comment.
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