mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-09 06:46:40 +02:00
Merge pull request #147 from mimmi20/master
New method request: startsWithAny()
This commit is contained in:
@@ -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
|
||||
|
@@ -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', array('foo bar')),
|
||||
array(true, 'FOO bars', array('foo bar'), false),
|
||||
array(true, 'FOO bars', array('foo bar', 'foo BAR'), false),
|
||||
array(true, 'FÒÔ bàřs', array('foo bar', 'fòô bàř'), false, 'UTF-8'),
|
||||
array(true, 'fòô bàřs', array('foo bar', 'fòô BÀŘ'), false, 'UTF-8'),
|
||||
array(false, 'foo bar', array('bar')),
|
||||
array(false, 'foo bar', array('foo bars')),
|
||||
array(false, 'FOO bar', array('foo bars')),
|
||||
array(false, 'FOO bars', array('foo BAR')),
|
||||
array(false, 'FÒÔ bàřs', array('fòô bàř'), true, 'UTF-8'),
|
||||
array(false, 'fòô bàřs', array('fòô BÀŘ'), true, 'UTF-8'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider endsWithProvider()
|
||||
*/
|
||||
|
Reference in New Issue
Block a user