diff --git a/src/Stringy.php b/src/Stringy.php index 7acfc00..f0b6f73 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -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 diff --git a/tests/StringyTest.php b/tests/StringyTest.php index d538770..8855252 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -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() */