diff --git a/src/Str.php b/src/Str.php index 83c28a7..0e6da73 100644 --- a/src/Str.php +++ b/src/Str.php @@ -183,10 +183,32 @@ public static function charAt($subject, $index) * * @param array|string $needles */ - public static function contains(string $haystack, $needles): bool + public static function contains(string $haystack, mixed $needles, bool $ignoreCase = false): bool + { + if ($ignoreCase) { + return static::containsIgnoreCase($haystack, $needles); + } + + foreach ((array) $needles as $needle) { + $needle = (string) $needle; + if ($needle !== '' && str_contains($haystack, $needle)) { + return true; + } + } + + return false; + } + + /** + * Determine if a given string contains a given substring regardless of case sensitivity. + * + * @param array|string $needles + */ + public static function containsIgnoreCase(string $haystack, $needles): bool { foreach ((array) $needles as $needle) { - if ($needle !== '' && str_contains($haystack, (string) $needle)) { + $needle = (string) $needle; + if ($needle !== '' && stripos($haystack, $needle) !== false) { return true; } } @@ -197,14 +219,13 @@ public static function contains(string $haystack, $needles): bool /** * Determine if a given string contains all array values. * - * @param string $haystack * @param string[] $needles * @return bool */ - public static function containsAll($haystack, array $needles) + public static function containsAll(string $haystack, array $needles, bool $ignoreCase = false) { foreach ($needles as $needle) { - if (! static::contains($haystack, $needle)) { + if (! static::contains($haystack, $needle, $ignoreCase)) { return false; } } @@ -221,7 +242,8 @@ public static function containsAll($haystack, array $needles) public static function endsWith(string $haystack, $needles) { foreach ((array) $needles as $needle) { - if ($needle !== '' && str_ends_with($haystack, (string) $needle)) { + $needle = (string) $needle; + if ($needle !== '' && str_ends_with($haystack, $needle)) { return true; } } @@ -672,7 +694,8 @@ public static function snake(string $value, string $delimiter = '_'): string public static function startsWith(string $haystack, $needles): bool { foreach ((array) $needles as $needle) { - if ($needle !== '' && str_starts_with($haystack, (string) $needle)) { + $needle = (string) $needle; + if ($needle !== '' && str_starts_with($haystack, $needle)) { return true; } } diff --git a/tests/StrTest.php b/tests/StrTest.php index b3169bb..3e6c1fc 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -172,6 +172,9 @@ public function testStartsWith() $this->assertFalse(Str::startsWith('hyperf.wiki', ['http://', 'https://'])); $this->assertTrue(Str::startsWith('http://www.hyperf.io', 'http://')); $this->assertTrue(Str::startsWith('https://www.hyperf.io', ['http://', 'https://'])); + $this->assertFalse(Str::startsWith('Hyperf', [''])); + $this->assertFalse(Str::startsWith('Hyperf', [null])); + $this->assertFalse(Str::startsWith('Hyperf', null)); } public function testStripTags() @@ -591,4 +594,27 @@ public static function invalidUrls() ['http:///path'], ]; } + + public function testContains() + { + $this->assertTrue(Str::contains('Hyperf', ['h'], true)); + $this->assertTrue(Str::contains('Hyperf', ['H'])); + $this->assertFalse(Str::contains('Hyperf', [''])); + $this->assertFalse(Str::contains('Hyperf', [null])); + $this->assertFalse(Str::contains('Hyperf', null)); + } + + public function testEndsWith() + { + $this->assertTrue(Str::endsWith('Hyperf', ['f'])); + $this->assertFalse(Str::endsWith('Hyperf', [''])); + $this->assertFalse(Str::endsWith('Hyperf', [null])); + $this->assertFalse(Str::endsWith('Hyperf', null)); + } + + public function testContainsAll() + { + $this->assertTrue(Str::containsAll('Hyperf', ['h'], true)); + $this->assertFalse(Str::containsAll('Hyperf', ['h'])); + } }