Skip to content

Commit

Permalink
Merge branch 'master' into 3.1-merge
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/stringable/tests/StrTest.php
  • Loading branch information
limingxinleo committed Jul 27, 2023
2 parents 4bb39b9 + 1bcea14 commit c0bf675
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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']));
}
}

0 comments on commit c0bf675

Please sign in to comment.