diff --git a/src/StaticStringy.php b/src/StaticStringy.php index baaae2b..002bc23 100644 --- a/src/StaticStringy.php +++ b/src/StaticStringy.php @@ -383,9 +383,9 @@ class StaticStringy } /** - * Returns true if the string contains any $needles, false otherwise. By default, - * the comparison is case-sensitive, but can be made insensitive by setting - * $caseSensitive to false. + * Returns true if the string contains any $needles, false otherwise. By + * default, the comparison is case-sensitive, but can be made insensitive + * by setting $caseSensitive to false. * * @param string $haystack String being checked * @param array $needles Substrings to look for @@ -393,8 +393,8 @@ class StaticStringy * @param string $encoding The character encoding * @return bool Whether or not $haystack contains $needle */ - public static function containsAny($haystack, $needles, $caseSensitive = true, - $encoding = null) + public static function containsAny($haystack, $needles, + $caseSensitive = true, $encoding = null) { return Stringy::create($haystack, $encoding) ->containsAny($needles, $caseSensitive); @@ -458,7 +458,7 @@ class StaticStringy * @return string The resulting string after truncating */ public static function safeTruncate($str, $length, $substring = '', - $encoding = null) + $encoding = null) { return (string) Stringy::create($str, $encoding) ->safeTruncate($length, $substring); diff --git a/src/Stringy.php b/src/Stringy.php index 4ba88f2..dbb1601 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -761,9 +761,9 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess } /** - * Returns true if the string contains any $needles, false otherwise. By default - * the comparison is case-sensitive, but can be made insensitive by setting - * $caseSensitive to false. + * Returns true if the string contains any $needles, false otherwise. By + * default the comparison is case-sensitive, but can be made insensitive by + * setting $caseSensitive to false. * * @param array $needles Substrings to look for * @param bool $caseSensitive Whether or not to enforce case-sensitivity @@ -771,17 +771,15 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess */ public function containsAny($needles, $caseSensitive = true) { - if ( ! empty($needles)) { - $encoding = $this->encoding; + if (empty($needles)) { + return false; + } - foreach($needles as $needle) { - if ($caseSensitive) { - $contains = mb_strpos($this->str, $needle, 0, $encoding) !== false; - } else { - $contains = mb_stripos($this->str, $needle, 0, $encoding) !== false; - } + $encoding = $this->encoding; - if ($contains === true) return true; + foreach($needles as $needle) { + if ($this->contains($needle, $caseSensitive)) { + return true; } } diff --git a/tests/CommonTest.php b/tests/CommonTest.php index b4ddf5a..4794514 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -2,7 +2,6 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase { - /** * Asserts that a variable is of a Stringy instance. * @@ -402,32 +401,16 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase public function containsAnyProvider() { - return array( - // no needle + // One needle + $singleNeedle = array_map(function($array) { + $array[2] = array($array[2]); + return $array; + }, $this->containsProvider()); + + $provider = array( + // No needles array(false, 'This string contains foo bar', array()), - // one needle - array(true, 'This string contains foo bar', array('foo bar')), - array(true, '12398!@(*%!@# @!%#*&^%', array(' @!%#*&^%')), - array(true, 'Ο συγγραφέας είπε', array('συγγραφέας'), 'UTF-8'), - array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array('å´¥©'), true, 'UTF-8'), - array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array('å˚ ∆'), true, 'UTF-8'), - array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array('øœ¬'), true, 'UTF-8'), - array(false, 'This string contains foo bar', array('Foo bar')), - array(false, 'This string contains foo bar', array('foobar')), - array(false, 'This string contains foo bar', array('foo bar ')), - array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας '), true, 'UTF-8'), - array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array(' ßå˚'), true, 'UTF-8'), - array(true, 'This string contains foo bar', array('Foo bar'), false), - array(true, '12398!@(*%!@# @!%#*&^%', array(' @!%#*&^%'), false), - array(true, 'Ο συγγραφέας είπε', array('ΣΥΓΓΡΑΦΈΑΣ'), false, 'UTF-8'), - array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array('Å´¥©'), false, 'UTF-8'), - array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array('Å˚ ∆'), false, 'UTF-8'), - array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array('ØŒ¬'), false, 'UTF-8'), - array(false, 'This string contains foo bar', array('foobar'), false), - array(false, 'This string contains foo bar', array('foo bar '), false), - array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας '), false, 'UTF-8'), - array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array(' ßÅ˚'), false, 'UTF-8'), - // many needles + // Multiple needles array(true, 'This string contains foo bar', array('foo', 'bar')), array(true, '12398!@(*%!@# @!%#*&^%', array(' @!%#*', '&^%')), array(true, 'Ο συγγραφέας είπε', array('συγγρ', 'αφέας'), 'UTF-8'), @@ -450,6 +433,8 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας ', ' ραφέ '), false, 'UTF-8'), array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array(' ßÅ˚', ' Å˚ '), false, 'UTF-8'), ); + + return array_merge($singleNeedle, $provider); } diff --git a/tests/StaticStringyTest.php b/tests/StaticStringyTest.php index f6075c2..0cc8f46 100644 --- a/tests/StaticStringyTest.php +++ b/tests/StaticStringyTest.php @@ -289,7 +289,7 @@ class StaticStringyTestCase extends CommonTest * @dataProvider containsAnyProvider() */ public function testcontainsAny($expected, $haystack, $needles, - $caseSensitive = true, $encoding = null) + $caseSensitive = true, $encoding = null) { $result = S::containsAny($haystack, $needles, $caseSensitive, $encoding); $this->assertInternalType('boolean', $result); diff --git a/tests/StringyTest.php b/tests/StringyTest.php index 27e035d..99d00af 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -485,7 +485,7 @@ class StringyTestCase extends CommonTest * @dataProvider containsAnyProvider() */ public function testcontainsAny($expected, $haystack, $needles, - $caseSensitive = true, $encoding = null) + $caseSensitive = true, $encoding = null) { $stringy = S::create($haystack, $encoding); $result = $stringy->containsAny($needles, $caseSensitive);