Skip to content

Commit c2be436

Browse files
committed
feat: add nullish methods to Str::class facade
chore: style cleanup chore: formatting chore: cleanup, use static chore: cleanup
1 parent 7f7ec7b commit c2be436

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,4 +2100,48 @@ public static function flushCache()
21002100
static::$camelCache = [];
21012101
static::$studlyCache = [];
21022102
}
2103+
2104+
/**
2105+
* Determines if a string is null or contains no characters.
2106+
*
2107+
* @param string|null $string
2108+
* @return bool
2109+
*/
2110+
public static function nullOrEmpty($string)
2111+
{
2112+
return $string === null || $string === '';
2113+
}
2114+
2115+
/**
2116+
* Determines if a string is null or contains only whitespace characters.
2117+
*
2118+
* @param string|null $string
2119+
* @return bool
2120+
*/
2121+
public static function nullOrWhitespace($string)
2122+
{
2123+
return static::nullOrEmpty($string) || trim($string) === '';
2124+
}
2125+
2126+
/**
2127+
* Determines if a string is not null and contains characters.
2128+
*
2129+
* @param string|null $string
2130+
* @return bool
2131+
*/
2132+
public static function notNullOrEmpty($string)
2133+
{
2134+
return ! static::nullOrEmpty($string);
2135+
}
2136+
2137+
/**
2138+
* Determines if a string is not null and does not contain only whitespace characters.
2139+
*
2140+
* @param string|null $string
2141+
* @return bool
2142+
*/
2143+
public static function notNullOrWhitespace($string)
2144+
{
2145+
return ! static::nullOrWhitespace($string);
2146+
}
21032147
}

tests/Support/SupportStrTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,114 @@ public function count(): int
19051905

19061906
$this->assertSame('UserGroups', Str::pluralPascal('UserGroup', $countable));
19071907
}
1908+
1909+
#[DataProvider('nullOrEmptyProvider')]
1910+
public function testNullOrEmpty($value, $expected)
1911+
{
1912+
$this->assertEquals($expected, Str::nullOrEmpty($value));
1913+
}
1914+
1915+
#[DataProvider('notNullOrEmptyProvider')]
1916+
public function testNotNullOrEmpty($value, $expected)
1917+
{
1918+
$this->assertEquals($expected, Str::notNullOrEmpty($value));
1919+
}
1920+
1921+
#[DataProvider('nullOrWhitespaceProvider')]
1922+
public function testNullOrWhitespace($value, $expected)
1923+
{
1924+
$this->assertEquals($expected, Str::nullOrWhitespace($value));
1925+
}
1926+
1927+
#[DataProvider('notNullOrWhitespaceProvider')]
1928+
public function testNotNullOrWhitespace($value, $expected)
1929+
{
1930+
$this->assertEquals($expected, Str::notNullOrWhitespace($value));
1931+
}
1932+
1933+
public static function nullOrEmptyProvider()
1934+
{
1935+
return [
1936+
'null value' => [null, true],
1937+
'empty string' => ['', true],
1938+
'single space' => [' ', false],
1939+
'multiple spaces' => [' ', false],
1940+
'tab character' => ["\t", false],
1941+
'newline character' => ["\n", false],
1942+
'zero string' => ['0', false],
1943+
'simple text' => ['hello', false],
1944+
'text with spaces' => ['hello world', false],
1945+
'single character' => ['a', false],
1946+
'number string' => ['123', false],
1947+
'special characters' => ['!@#$', false],
1948+
];
1949+
}
1950+
1951+
public static function notNullOrEmptyProvider()
1952+
{
1953+
return [
1954+
'null value' => [null, false],
1955+
'empty string' => ['', false],
1956+
'single space' => [' ', true],
1957+
'multiple spaces' => [' ', true],
1958+
'tab character' => ["\t", true],
1959+
'newline character' => ["\n", true],
1960+
'zero string' => ['0', true],
1961+
'simple text' => ['hello', true],
1962+
'text with spaces' => ['hello world', true],
1963+
'single character' => ['a', true],
1964+
'number string' => ['123', true],
1965+
'special characters' => ['!@#$', true],
1966+
];
1967+
}
1968+
1969+
public static function nullOrWhitespaceProvider()
1970+
{
1971+
return [
1972+
'null value' => [null, true],
1973+
'empty string' => ['', true],
1974+
'single space' => [' ', true],
1975+
'multiple spaces' => [' ', true],
1976+
'tab character' => ["\t", true],
1977+
'newline character' => ["\n", true],
1978+
'carriage return' => ["\r", true],
1979+
'mixed whitespace' => [" \t\n\r ", true],
1980+
'zero string' => ['0', false],
1981+
'simple text' => ['hello', false],
1982+
'text with spaces' => ['hello world', false],
1983+
'text with leading space' => [' hello', false],
1984+
'text with trailing space' => ['hello ', false],
1985+
'text with surrounding spaces' => [' hello ', false],
1986+
'single character' => ['a', false],
1987+
'number string' => ['123', false],
1988+
'special characters' => ['!@#$', false],
1989+
'whitespace with period' => [' . ', false],
1990+
];
1991+
}
1992+
1993+
public static function notNullOrWhitespaceProvider()
1994+
{
1995+
return [
1996+
'null value' => [null, false],
1997+
'empty string' => ['', false],
1998+
'single space' => [' ', false],
1999+
'multiple spaces' => [' ', false],
2000+
'tab character' => ["\t", false],
2001+
'newline character' => ["\n", false],
2002+
'carriage return' => ["\r", false],
2003+
'mixed whitespace' => [" \t\n\r ", false],
2004+
'zero string' => ['0', true],
2005+
'simple text' => ['hello', true],
2006+
'text with spaces' => ['hello world', true],
2007+
'text with leading space' => [' hello', true],
2008+
'text with trailing space' => ['hello ', true],
2009+
'text with surrounding spaces' => [' hello ', true],
2010+
'single character' => ['a', true],
2011+
'number string' => ['123', true],
2012+
'special characters' => ['!@#$', true],
2013+
'whitespace with period' => [' . ', true],
2014+
];
2015+
}
19082016
}
19092017

19102018
class StringableObjectStub

0 commit comments

Comments
 (0)