1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-06 21:36:31 +02:00

Add endsWithAny

This commit is contained in:
Daniel St. Jules
2017-02-28 23:41:49 -05:00
parent ded4d41d44
commit df777da6ac
3 changed files with 81 additions and 29 deletions

View File

@@ -26,24 +26,23 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
<td>[append](#appendstring-string)</td> <td>[append](#appendstring-string)</td>
<td>[at](#atint-index)</td> <td>[at](#atint-index)</td>
<td>[between](#betweenstring-start-string-end--int-offset)</td> <td>[between](#betweenstring-start-string-end--int-offset)</td>
<td>[camelize](#camelize)</td>
</tr> </tr>
<tr> <tr>
<td>[camelize](#camelize)</td>
<td>[chars](#chars)</td> <td>[chars](#chars)</td>
<td>[collapseWhitespace](#collapsewhitespace)</td> <td>[collapseWhitespace](#collapsewhitespace)</td>
</tr>
<tr>
<td>[contains](#containsstring-needle--boolean-casesensitive--true-)</td> <td>[contains](#containsstring-needle--boolean-casesensitive--true-)</td>
<td>[containsAll](#containsallarray-needles--boolean-casesensitive--true-)</td> <td>[containsAll](#containsallarray-needles--boolean-casesensitive--true-)</td>
<td>[containsAny](#containsanyarray-needles--boolean-casesensitive--true-)</td>
</tr> </tr>
<tr> <tr>
<td>[containsAny](#containsanyarray-needles--boolean-casesensitive--true-)</td>
<td>[countSubstr](#countsubstrstring-substring--boolean-casesensitive--true-)</td> <td>[countSubstr](#countsubstrstring-substring--boolean-casesensitive--true-)</td>
<td>[dasherize](#dasherize)</td> <td>[dasherize](#dasherize)</td>
<td>[delimit](#delimitint-delimiter)</td> <td>[delimit](#delimitint-delimiter)</td>
</tr> </tr>
<tr> <tr>
<td>[endsWith](#endswithstring-substring--boolean-casesensitive--true-)</td> <td>[endsWith](#endswithstring-substring--boolean-casesensitive--true-)</td>
<td>[endsWithAny](#endsWithAnystring-substrings--boolean-casesensitive--true-)</td>
<td>[ensureLeft](#ensureleftstring-substring)</td> <td>[ensureLeft](#ensureleftstring-substring)</td>
<td>[ensureRight](#ensurerightstring-substring)</td> <td>[ensureRight](#ensurerightstring-substring)</td>
</tr> </tr>
@@ -51,18 +50,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
<td>[first](#firstint-n)</td> <td>[first](#firstint-n)</td>
<td>[getEncoding](#getencoding)</td> <td>[getEncoding](#getencoding)</td>
<td>[hasLowerCase](#haslowercase)</td> <td>[hasLowerCase](#haslowercase)</td>
<td>[hasUpperCase](#hasuppercase)</td>
</tr> </tr>
<tr> <tr>
<td>[hasUpperCase](#hasuppercase)</td>
<td>[htmlDecode](#htmldecode)</td> <td>[htmlDecode](#htmldecode)</td>
<td>[htmlEncode](#htmlencode)</td> <td>[htmlEncode](#htmlencode)</td>
</tr>
<tr>
<td>[humanize](#humanize)</td> <td>[humanize](#humanize)</td>
<td>[indexOf](#indexofstring-needle--offset--0-)</td> <td>[indexOf](#indexofstring-needle--offset--0-)</td>
<td>[indexOfLast](#indexoflaststring-needle--offset--0-)</td>
</tr> </tr>
<tr> <tr>
<td>[indexOfLast](#indexoflaststring-needle--offset--0-)</td>
<td>[insert](#insertint-index-string-substring)</td> <td>[insert](#insertint-index-string-substring)</td>
<td>[isAlpha](#isalpha)</td> <td>[isAlpha](#isalpha)</td>
<td>[isAlphanumeric](#isalphanumeric)</td> <td>[isAlphanumeric](#isalphanumeric)</td>
@@ -71,18 +68,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
<td>[isBase64](#isbase64)</td> <td>[isBase64](#isbase64)</td>
<td>[isBlank](#isblank)</td> <td>[isBlank](#isblank)</td>
<td>[isHexadecimal](#ishexadecimal)</td> <td>[isHexadecimal](#ishexadecimal)</td>
<td>[isJson](#isjson)</td>
</tr> </tr>
<tr> <tr>
<td>[isJson](#isjson)</td>
<td>[isLowerCase](#islowercase)</td> <td>[isLowerCase](#islowercase)</td>
<td>[isSerialized](#isserialized)</td> <td>[isSerialized](#isserialized)</td>
</tr>
<tr>
<td>[isUpperCase](#isuppercase)</td> <td>[isUpperCase](#isuppercase)</td>
<td>[last](#last)</td> <td>[last](#last)</td>
<td>[length](#length)</td>
</tr> </tr>
<tr> <tr>
<td>[length](#length)</td>
<td>[lines](#lines)</td> <td>[lines](#lines)</td>
<td>[longestCommonPrefix](#longestcommonprefixstring-otherstr)</td> <td>[longestCommonPrefix](#longestcommonprefixstring-otherstr)</td>
<td>[longestCommonSuffix](#longestcommonsuffixstring-otherstr)</td> <td>[longestCommonSuffix](#longestcommonsuffixstring-otherstr)</td>
@@ -91,18 +86,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
<td>[longestCommonSubstring](#longestcommonsubstringstring-otherstr)</td> <td>[longestCommonSubstring](#longestcommonsubstringstring-otherstr)</td>
<td>[lowerCaseFirst](#lowercasefirst)</td> <td>[lowerCaseFirst](#lowercasefirst)</td>
<td>[pad](#padint-length--string-padstr-----string-padtype--right-)</td> <td>[pad](#padint-length--string-padstr-----string-padtype--right-)</td>
<td>[padBoth](#padbothint-length--string-padstr----)</td>
</tr> </tr>
<tr> <tr>
<td>[padBoth](#padbothint-length--string-padstr----)</td>
<td>[padLeft](#padleftint-length--string-padstr----)</td> <td>[padLeft](#padleftint-length--string-padstr----)</td>
<td>[padRight](#padrightint-length--string-padstr----)</td> <td>[padRight](#padrightint-length--string-padstr----)</td>
</tr>
<tr>
<td>[prepend](#prependstring-string)</td> <td>[prepend](#prependstring-string)</td>
<td>[regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr)</td> <td>[regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr)</td>
<td>[removeLeft](#removeleftstring-substring)</td>
</tr> </tr>
<tr> <tr>
<td>[removeLeft](#removeleftstring-substring)</td>
<td>[removeRight](#removerightstring-substring)</td> <td>[removeRight](#removerightstring-substring)</td>
<td>[repeat](#repeatmultiplier)</td> <td>[repeat](#repeatmultiplier)</td>
<td>[replace](#replacestring-search-string-replacement)</td> <td>[replace](#replacestring-search-string-replacement)</td>
@@ -111,18 +104,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
<td>[reverse](#reverse)</td> <td>[reverse](#reverse)</td>
<td>[safeTruncate](#safetruncateint-length--string-substring---)</td> <td>[safeTruncate](#safetruncateint-length--string-substring---)</td>
<td>[shuffle](#shuffle)</td> <td>[shuffle](#shuffle)</td>
<td>[slugify](#slugify-string-replacement----)</td>
</tr> </tr>
<tr> <tr>
<td>[slugify](#slugify-string-replacement----)</td>
<td>[startsWith](#startswithstring-substring--boolean-casesensitive--true-)</td> <td>[startsWith](#startswithstring-substring--boolean-casesensitive--true-)</td>
<td>[startsWithAny](#startswithanystring-substrings--boolean-casesensitive--true-)</td> <td>[startsWithAny](#startswithanystring-substrings--boolean-casesensitive--true-)</td>
</tr>
<tr>
<td>[slice](#sliceint-start--int-end-)</td> <td>[slice](#sliceint-start--int-end-)</td>
<td>[split](#splitstring-pattern--int-limit-)</td> <td>[split](#splitstring-pattern--int-limit-)</td>
<td>[stripWhitespace](#stripwhitespace)</td>
</tr> </tr>
<tr> <tr>
<td>[stripWhitespace](#stripwhitespace)</td>
<td>[substr](#substrint-start--int-length-)</td> <td>[substr](#substrint-start--int-length-)</td>
<td>[surround](#surroundstring-substring)</td> <td>[surround](#surroundstring-substring)</td>
<td>[swapCase](#swapcase)</td> <td>[swapCase](#swapcase)</td>
@@ -131,18 +122,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
<td>[tidy](#tidy)</td> <td>[tidy](#tidy)</td>
<td>[titleize](#titleize-array-ignore)</td> <td>[titleize](#titleize-array-ignore)</td>
<td>[toAscii](#toascii)</td> <td>[toAscii](#toascii)</td>
<td>[toBoolean](#toboolean)</td>
</tr> </tr>
<tr> <tr>
<td>[toBoolean](#toboolean)</td>
<td>[toLowerCase](#tolowercase)</td> <td>[toLowerCase](#tolowercase)</td>
<td>[toSpaces](#tospaces-tablength--4-)</td> <td>[toSpaces](#tospaces-tablength--4-)</td>
</tr>
<tr>
<td>[toTabs](#totabs-tablength--4-)</td> <td>[toTabs](#totabs-tablength--4-)</td>
<td>[toTitleCase](#totitlecase)</td> <td>[toTitleCase](#totitlecase)</td>
<td>[toUpperCase](#touppercase)</td>
</tr> </tr>
<tr> <tr>
<td>[toUpperCase](#touppercase)</td>
<td>[trim](#trim-string-chars)</td> <td>[trim](#trim-string-chars)</td>
<td>[trimLeft](#trimleft-string-chars)</td> <td>[trimLeft](#trimleft-string-chars)</td>
<td>[trimRight](#trimright-string-chars)</td> <td>[trimRight](#trimright-string-chars)</td>
@@ -151,8 +140,6 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
<td>[truncate](#truncateint-length--string-substring---)</td> <td>[truncate](#truncateint-length--string-substring---)</td>
<td>[underscored](#underscored)</td> <td>[underscored](#underscored)</td>
<td>[upperCamelize](#uppercamelize)</td> <td>[upperCamelize](#uppercamelize)</td>
</tr>
<tr>
<td>[upperCaseFirst](#uppercasefirst)</td> <td>[upperCaseFirst](#uppercasefirst)</td>
</tr> </tr>
</table> </table>
@@ -449,6 +436,16 @@ setting $caseSensitive to false.
s('fòôbàř')->endsWith('bàř', true); // true s('fòôbàř')->endsWith('bàř', true); // true
``` ```
##### endsWithAny(string[] $substrings [, boolean $caseSensitive = true ])
Returns true if the string ends with any of $substrings, false otherwise.
By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php
s('fòôbàř')->endsWith(['bàř', 'baz'], true); // true
```
##### ensureLeft(string $substring) ##### ensureLeft(string $substring)
Ensures that the string begins with $substring. If it doesn't, it's prepended. Ensures that the string begins with $substring. If it doesn't, it's prepended.

View File

@@ -353,6 +353,31 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
return (string) $substring === $endOfStr; return (string) $substring === $endOfStr;
} }
/**
* Returns true if the string ends with any of $substrings, false otherwise.
* By default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string[] $substrings Substrings to look for
* @param bool $caseSensitive Whether or not to enforce
* case-sensitivity
* @return bool Whether or not $str ends with $substring
*/
public function endsWithAny($substrings, $caseSensitive = true)
{
if (empty($substrings)) {
return false;
}
foreach ($substrings as $substring) {
if ($this->endsWith($substring, $caseSensitive)) {
return true;
}
}
return false;
}
/** /**
* Ensures that the string begins with $substring. If it doesn't, it's * Ensures that the string begins with $substring. If it doesn't, it's
* prepended. * prepended.

View File

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