From ea20aebc91b4b3397425534b5f4d11dd07cab945 Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Sat, 14 Sep 2013 01:32:37 -0400 Subject: [PATCH] Added $caseSensitive to contains() --- README.md | 8 +++++--- src/Stringy/StaticStringy.php | 18 +++++++++++------- src/Stringy/Stringy.php | 17 ++++++++++------- tests/Stringy/CommonTest.php | 20 +++++++++++++++----- tests/Stringy/StaticStringyTest.php | 5 +++-- tests/Stringy/StringyTest.php | 5 +++-- 6 files changed, 47 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 3cbf8db..a21a603 100644 --- a/README.md +++ b/README.md @@ -167,11 +167,13 @@ S::collapseWhitespace(' Ο συγγραφέας '); // 'Ο συγγρα #### contains -$stringy->contains(string $needle) +$stringy->contains(string $needle [, boolean $caseSensitive = true ]) -S::contains(string $haystack, string $needle [, string $encoding ]) +S::contains(string $haystack, string $needle [, boolean $caseSensitive = true [, string $encoding ]]) -Returns true if the string contains $needle, false otherwise. +Returns true if the string contains $needle, false otherwise. By default, +the comparison is case-sensitive, but can be made insensitive +by setting $caseSensitive to false. ```php S::create('Ο συγγραφέας είπε', 'UTF-8')->contains('συγγραφέας'); diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index ec78825..968533a 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -144,7 +144,7 @@ class StaticStringy * single space. This includes tabs and newline characters, as well as * multibyte whitespace such as the thin space and ideographic space. * - * @param string $str The string to cleanup whitespace + * @param string $str The string to cleanup whitespace * @param string $encoding The character encoding * @return string The trimmed string with condensed whitespace */ @@ -311,16 +311,20 @@ class StaticStringy } /** - * Returns true if the string contains $needle, false otherwise. + * Returns true if the string contains $needle, 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 string $needle Substring to look for - * @param string $encoding The character encoding + * @param string $haystack String being checked + * @param string $needle Substring to look for + * @param bool $caseSensitive Whether or not to enforce case-sensitivity + * @param string $encoding The character encoding * @return bool Whether or not $haystack contains $needle */ - public static function contains($haystack, $needle, $encoding = null) + public static function contains($haystack, $needle, $caseSensitive = true, + $encoding = null) { - return Stringy::create($haystack, $encoding)->contains($needle); + return Stringy::create($haystack, $encoding)->contains($needle, $caseSensitive); } /** diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 3b4eaf3..5a25dfd 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -568,17 +568,20 @@ class Stringy } /** - * Returns true if the string contains $needle, false otherwise. + * Returns true if the string contains $needle, false otherwise. By default + * the comparison is case-sensitive, but can be made insensitive by setting + * $caseSensitive to false. * - * @param string $needle Substring to look for + * @param string $needle Substring to look for + * @param bool $caseSensitive Whether or not to enforce case-sensitivity * @return bool Whether or not $str contains $needle */ - public function contains($needle) + public function contains($needle, $caseSensitive = true) { - if (mb_strpos($this->str, $needle, 0, $this->encoding) !== false) - return true; - - return false; + if ($caseSensitive) + return (mb_strpos($this->str, $needle, 0, $this->encoding) !== false); + else + return (mb_stripos($this->str, $needle, 0, $this->encoding) !== false); } /** diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index c237489..5a73daf 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -360,14 +360,24 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase array(true, 'This string contains foo bar', 'foo bar'), array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%'), array(true, 'Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'), - array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', 'UTF-8'), - array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', 'UTF-8'), - array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'øœ¬', 'UTF-8'), + array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', true, 'UTF-8'), + array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', true, 'UTF-8'), + array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'øœ¬', true, 'UTF-8'), array(false, 'This string contains foo bar', 'Foo bar'), array(false, 'This string contains foo bar', 'foobar'), array(false, 'This string contains foo bar', 'foo bar '), - array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', 'UTF-8'), - array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', 'UTF-8') + array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', true, 'UTF-8'), + array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', true, 'UTF-8'), + array(true, 'This string contains foo bar', 'Foo bar', false), + array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%', false), + array(true, 'Ο συγγραφέας είπε', 'ΣΥΓΓΡΑΦΈΑΣ', false, 'UTF-8'), + array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'Å´¥©', false, 'UTF-8'), + array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'Å˚ ∆', false, 'UTF-8'), + array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'ØŒ¬', false, 'UTF-8'), + array(false, 'This string contains foo bar', 'foobar', false), + array(false, 'This string contains foo bar', 'foo bar ', false), + array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', false, 'UTF-8'), + array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßÅ˚', false, 'UTF-8') ); return $testData; diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 57863fb..5dd89df 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -235,9 +235,10 @@ class StaticStringyTestCase extends CommonTest /** * @dataProvider stringsForContains */ - public function testContains($expected, $haystack, $needle, $encoding = null) + public function testContains($expected, $haystack, $needle, + $caseSensitive = true, $encoding = null) { - $result = S::contains($haystack, $needle, $encoding); + $result = S::contains($haystack, $needle, $caseSensitive, $encoding); $this->assertInternalType('boolean', $result); $this->assertEquals($expected, $result); } diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index deb2285..b9704d2 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -300,10 +300,11 @@ class StringyTestCase extends CommonTest /** * @dataProvider stringsForContains */ - public function testContains($expected, $haystack, $needle, $encoding = null) + public function testContains($expected, $haystack, $needle, + $caseSensitive = true, $encoding = null) { $stringy = S::create($haystack, $encoding); - $result = $stringy->contains($needle); + $result = $stringy->contains($needle, $caseSensitive); $this->assertInternalType('boolean', $result); $this->assertEquals($expected, $result); $this->assertEquals($haystack, $stringy);