Skip to content

Commit c0bf675

Browse files
committed
Merge branch 'master' into 3.1-merge
# Conflicts: # src/stringable/tests/StrTest.php
2 parents 4bb39b9 + 1bcea14 commit c0bf675

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

src/Str.php

+30-7
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,32 @@ public static function charAt($subject, $index)
183183
*
184184
* @param array|string $needles
185185
*/
186-
public static function contains(string $haystack, $needles): bool
186+
public static function contains(string $haystack, mixed $needles, bool $ignoreCase = false): bool
187+
{
188+
if ($ignoreCase) {
189+
return static::containsIgnoreCase($haystack, $needles);
190+
}
191+
192+
foreach ((array) $needles as $needle) {
193+
$needle = (string) $needle;
194+
if ($needle !== '' && str_contains($haystack, $needle)) {
195+
return true;
196+
}
197+
}
198+
199+
return false;
200+
}
201+
202+
/**
203+
* Determine if a given string contains a given substring regardless of case sensitivity.
204+
*
205+
* @param array|string $needles
206+
*/
207+
public static function containsIgnoreCase(string $haystack, $needles): bool
187208
{
188209
foreach ((array) $needles as $needle) {
189-
if ($needle !== '' && str_contains($haystack, (string) $needle)) {
210+
$needle = (string) $needle;
211+
if ($needle !== '' && stripos($haystack, $needle) !== false) {
190212
return true;
191213
}
192214
}
@@ -197,14 +219,13 @@ public static function contains(string $haystack, $needles): bool
197219
/**
198220
* Determine if a given string contains all array values.
199221
*
200-
* @param string $haystack
201222
* @param string[] $needles
202223
* @return bool
203224
*/
204-
public static function containsAll($haystack, array $needles)
225+
public static function containsAll(string $haystack, array $needles, bool $ignoreCase = false)
205226
{
206227
foreach ($needles as $needle) {
207-
if (! static::contains($haystack, $needle)) {
228+
if (! static::contains($haystack, $needle, $ignoreCase)) {
208229
return false;
209230
}
210231
}
@@ -221,7 +242,8 @@ public static function containsAll($haystack, array $needles)
221242
public static function endsWith(string $haystack, $needles)
222243
{
223244
foreach ((array) $needles as $needle) {
224-
if ($needle !== '' && str_ends_with($haystack, (string) $needle)) {
245+
$needle = (string) $needle;
246+
if ($needle !== '' && str_ends_with($haystack, $needle)) {
225247
return true;
226248
}
227249
}
@@ -672,7 +694,8 @@ public static function snake(string $value, string $delimiter = '_'): string
672694
public static function startsWith(string $haystack, $needles): bool
673695
{
674696
foreach ((array) $needles as $needle) {
675-
if ($needle !== '' && str_starts_with($haystack, (string) $needle)) {
697+
$needle = (string) $needle;
698+
if ($needle !== '' && str_starts_with($haystack, $needle)) {
676699
return true;
677700
}
678701
}

tests/StrTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ public function testStartsWith()
172172
$this->assertFalse(Str::startsWith('hyperf.wiki', ['http://', 'https://']));
173173
$this->assertTrue(Str::startsWith('http://www.hyperf.io', 'http://'));
174174
$this->assertTrue(Str::startsWith('https://www.hyperf.io', ['http://', 'https://']));
175+
$this->assertFalse(Str::startsWith('Hyperf', ['']));
176+
$this->assertFalse(Str::startsWith('Hyperf', [null]));
177+
$this->assertFalse(Str::startsWith('Hyperf', null));
175178
}
176179

177180
public function testStripTags()
@@ -591,4 +594,27 @@ public static function invalidUrls()
591594
['http:///path'],
592595
];
593596
}
597+
598+
public function testContains()
599+
{
600+
$this->assertTrue(Str::contains('Hyperf', ['h'], true));
601+
$this->assertTrue(Str::contains('Hyperf', ['H']));
602+
$this->assertFalse(Str::contains('Hyperf', ['']));
603+
$this->assertFalse(Str::contains('Hyperf', [null]));
604+
$this->assertFalse(Str::contains('Hyperf', null));
605+
}
606+
607+
public function testEndsWith()
608+
{
609+
$this->assertTrue(Str::endsWith('Hyperf', ['f']));
610+
$this->assertFalse(Str::endsWith('Hyperf', ['']));
611+
$this->assertFalse(Str::endsWith('Hyperf', [null]));
612+
$this->assertFalse(Str::endsWith('Hyperf', null));
613+
}
614+
615+
public function testContainsAll()
616+
{
617+
$this->assertTrue(Str::containsAll('Hyperf', ['h'], true));
618+
$this->assertFalse(Str::containsAll('Hyperf', ['h']));
619+
}
594620
}

0 commit comments

Comments
 (0)