mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-08 06:16:32 +02:00
Add containsAll
This commit is contained in:
@@ -382,6 +382,24 @@ class StaticStringy
|
||||
->contains($needle, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains all $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
|
||||
* @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 containsAll($haystack, $needles, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)
|
||||
->containsAll($needles, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds a string with the given substring.
|
||||
*
|
||||
|
@@ -760,6 +760,34 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains all $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
|
||||
* @return bool Whether or not $str contains $needle
|
||||
*/
|
||||
public function containsAll($needles, $caseSensitive = true)
|
||||
{
|
||||
if (empty($needles)) return false;
|
||||
|
||||
$encoding = $this->encoding;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if ($contains == false) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds $str with the given substring.
|
||||
*
|
||||
|
@@ -400,6 +400,59 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function containsAllProvider()
|
||||
{
|
||||
return array(
|
||||
// no needle
|
||||
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
|
||||
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', 'bar')),
|
||||
array(false, 'This string contains foo bar', array('foo bar ', 'bar')),
|
||||
array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας ', ' συγγραφ '), true, 'UTF-8'),
|
||||
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array(' ßå˚', ' ß '), true, 'UTF-8'),
|
||||
array(true, 'This string contains foo bar', array('Foo bar', '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', 'none'), false),
|
||||
array(false, 'This string contains foo bar', array('foo bar ', ' ba'), false),
|
||||
array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας ', ' ραφέ '), false, 'UTF-8'),
|
||||
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array(' ßÅ˚', ' Å˚ '), false, 'UTF-8'),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function surroundProvider()
|
||||
{
|
||||
return array(
|
||||
|
@@ -285,6 +285,17 @@ class StaticStringyTestCase extends CommonTest
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider containsAllProvider()
|
||||
*/
|
||||
public function testContainsAll($expected, $haystack, $needles,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::containsAll($haystack, $needles, $caseSensitive, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider surroundProvider()
|
||||
*/
|
||||
|
@@ -481,6 +481,19 @@ class StringyTestCase extends CommonTest
|
||||
$this->assertEquals($haystack, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider containsAllProvider()
|
||||
*/
|
||||
public function testContainsAll($expected, $haystack, $needles,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($haystack, $encoding);
|
||||
$result = $stringy->containsAll($needles, $caseSensitive);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($haystack, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider surroundProvider()
|
||||
*/
|
||||
|
Reference in New Issue
Block a user