diff --git a/README.md b/README.md index 4e2667c..cdf8c16 100644 --- a/README.md +++ b/README.md @@ -26,24 +26,23 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy' [append](#appendstring-string) [at](#atint-index) [between](#betweenstring-start-string-end--int-offset) + [camelize](#camelize) - [camelize](#camelize) [chars](#chars) [collapseWhitespace](#collapsewhitespace) - - [contains](#containsstring-needle--boolean-casesensitive--true-) [containsAll](#containsallarray-needles--boolean-casesensitive--true-) - [containsAny](#containsanyarray-needles--boolean-casesensitive--true-) + [containsAny](#containsanyarray-needles--boolean-casesensitive--true-) [countSubstr](#countsubstrstring-substring--boolean-casesensitive--true-) [dasherize](#dasherize) [delimit](#delimitint-delimiter) [endsWith](#endswithstring-substring--boolean-casesensitive--true-) + [endsWithAny](#endsWithAnystring-substrings--boolean-casesensitive--true-) [ensureLeft](#ensureleftstring-substring) [ensureRight](#ensurerightstring-substring) @@ -51,18 +50,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy' [first](#firstint-n) [getEncoding](#getencoding) [hasLowerCase](#haslowercase) + [hasUpperCase](#hasuppercase) - [hasUpperCase](#hasuppercase) [htmlDecode](#htmldecode) [htmlEncode](#htmlencode) - - [humanize](#humanize) [indexOf](#indexofstring-needle--offset--0-) - [indexOfLast](#indexoflaststring-needle--offset--0-) + [indexOfLast](#indexoflaststring-needle--offset--0-) [insert](#insertint-index-string-substring) [isAlpha](#isalpha) [isAlphanumeric](#isalphanumeric) @@ -71,18 +68,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy' [isBase64](#isbase64) [isBlank](#isblank) [isHexadecimal](#ishexadecimal) + [isJson](#isjson) - [isJson](#isjson) [isLowerCase](#islowercase) [isSerialized](#isserialized) - - [isUpperCase](#isuppercase) [last](#last) - [length](#length) + [length](#length) [lines](#lines) [longestCommonPrefix](#longestcommonprefixstring-otherstr) [longestCommonSuffix](#longestcommonsuffixstring-otherstr) @@ -91,18 +86,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy' [longestCommonSubstring](#longestcommonsubstringstring-otherstr) [lowerCaseFirst](#lowercasefirst) [pad](#padint-length--string-padstr-----string-padtype--right-) + [padBoth](#padbothint-length--string-padstr----) - [padBoth](#padbothint-length--string-padstr----) [padLeft](#padleftint-length--string-padstr----) [padRight](#padrightint-length--string-padstr----) - - [prepend](#prependstring-string) [regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr) - [removeLeft](#removeleftstring-substring) + [removeLeft](#removeleftstring-substring) [removeRight](#removerightstring-substring) [repeat](#repeatmultiplier) [replace](#replacestring-search-string-replacement) @@ -111,18 +104,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy' [reverse](#reverse) [safeTruncate](#safetruncateint-length--string-substring---) [shuffle](#shuffle) + [slugify](#slugify-string-replacement----) - [slugify](#slugify-string-replacement----) [startsWith](#startswithstring-substring--boolean-casesensitive--true-) [startsWithAny](#startswithanystring-substrings--boolean-casesensitive--true-) - - [slice](#sliceint-start--int-end-) [split](#splitstring-pattern--int-limit-) - [stripWhitespace](#stripwhitespace) + [stripWhitespace](#stripwhitespace) [substr](#substrint-start--int-length-) [surround](#surroundstring-substring) [swapCase](#swapcase) @@ -131,18 +122,16 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy' [tidy](#tidy) [titleize](#titleize-array-ignore) [toAscii](#toascii) + [toBoolean](#toboolean) - [toBoolean](#toboolean) [toLowerCase](#tolowercase) [toSpaces](#tospaces-tablength--4-) - - [toTabs](#totabs-tablength--4-) [toTitleCase](#totitlecase) - [toUpperCase](#touppercase) + [toUpperCase](#touppercase) [trim](#trim-string-chars) [trimLeft](#trimleft-string-chars) [trimRight](#trimright-string-chars) @@ -151,8 +140,6 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy' [truncate](#truncateint-length--string-substring---) [underscored](#underscored) [upperCamelize](#uppercamelize) - - [upperCaseFirst](#uppercasefirst) @@ -449,6 +436,16 @@ setting $caseSensitive to false. 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) Ensures that the string begins with $substring. If it doesn't, it's prepended. diff --git a/src/Stringy.php b/src/Stringy.php index 9869da2..8c5abf4 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -353,6 +353,31 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess 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 * prepended. diff --git a/tests/StringyTest.php b/tests/StringyTest.php index e100f8b..f4ef910 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -847,11 +847,11 @@ class StringyTestCase extends PHPUnit_Framework_TestCase /** * @dataProvider startsWithProviderAny() */ - public function testStartsWithAny($expected, $str, $substring, - $caseSensitive = true, $encoding = null) + public function testStartsWithAny($expected, $str, $substrings, + $caseSensitive = true, $encoding = null) { $stringy = S::create($str, $encoding); - $result = $stringy->startsWithAny($substring, $caseSensitive); + $result = $stringy->startsWithAny($substrings, $caseSensitive); $this->assertInternalType('boolean', $result); $this->assertEquals($expected, $result); $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() */