1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 00:04:11 +02:00

Added $caseSensitive to contains()

This commit is contained in:
Daniel St. Jules
2013-09-14 01:32:37 -04:00
parent 49e1c5ad2a
commit ea20aebc91
6 changed files with 47 additions and 26 deletions

View File

@@ -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('συγγραφέας');

View File

@@ -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);
}
/**

View File

@@ -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);
}
/**

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);