1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-04 20:37:29 +02:00

add startsWithAny function

This commit is contained in:
Thomas Müller
2016-10-31 19:54:19 +01:00
parent 0607751e17
commit 0491ed8db8
2 changed files with 54 additions and 0 deletions

View File

@@ -1170,6 +1170,30 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
return (string) $substring === $startOfStr;
}
/**
* Returns true if the string begins with any $substrings, false otherwise. By
* default the comparison is case-sensitive, but can be made insensitive by
* setting $caseSensitive to false.
*
* @param array $substrings Substrings to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @return bool Whether or not $str starts with $substring
*/
public function startsWithAny($substrings, $caseSensitive = true)
{
if (empty($substrings)) {
return false;
}
foreach ($substrings as $substring) {
if ($this->startsWith($substring, $caseSensitive)) {
return true;
}
}
return false;
}
/**
* Returns the substring beginning at $start, and up to, but not including
* the index specified by $end. If $end is omitted, the function extracts

View File

@@ -844,6 +844,36 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
);
}
/**
* @dataProvider startsWithProviderAny()
*/
public function testStartsWithAny($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->startsWithAny($substring, $caseSensitive);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
public function startsWithProviderAny()
{
return array(
array(true, 'foo bars', ['foo bar']),
array(true, 'FOO bars', ['foo bar'], false),
array(true, 'FOO bars', ['foo bar', 'foo BAR'], false),
array(true, 'FÒÔ bàřs', ['foo bar', 'fòô bàř'], false, 'UTF-8'),
array(true, 'fòô bàřs', ['foo bar', 'fòô BÀŘ'], false, 'UTF-8'),
array(false, 'foo bar', ['bar']),
array(false, 'foo bar', ['foo bars']),
array(false, 'FOO bar', ['foo bars']),
array(false, 'FOO bars', ['foo BAR']),
array(false, 'FÒÔ bàřs', ['fòô bàř'], true, 'UTF-8'),
array(false, 'fòô bàřs', ['fòô BÀŘ'], true, 'UTF-8'),
);
}
/**
* @dataProvider endsWithProvider()
*/