mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-09 14:56:31 +02:00
Merge and fix containsAll
This commit is contained in:
@@ -391,7 +391,7 @@ class StaticStringy
|
|||||||
* @param array $needles Substrings to look for
|
* @param array $needles Substrings to look for
|
||||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return bool Whether or not $haystack contains $needle
|
* @return bool Whether or not $haystack contains any $needles
|
||||||
*/
|
*/
|
||||||
public static function containsAny($haystack, $needles,
|
public static function containsAny($haystack, $needles,
|
||||||
$caseSensitive = true, $encoding = null)
|
$caseSensitive = true, $encoding = null)
|
||||||
@@ -400,6 +400,24 @@ class StaticStringy
|
|||||||
->containsAny($needles, $caseSensitive);
|
->containsAny($needles, $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 all $needles
|
||||||
|
*/
|
||||||
|
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.
|
* Surrounds a string with the given substring.
|
||||||
*
|
*
|
||||||
|
@@ -786,6 +786,32 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 (!$this->contains($needle, $caseSensitive)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Surrounds $str with the given substring.
|
* Surrounds $str with the given substring.
|
||||||
*
|
*
|
||||||
|
@@ -375,25 +375,25 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
public function containsProvider()
|
public function containsProvider()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array(true, 'This string contains foo bar', 'foo bar'),
|
array(true, 'Str contains foo bar', 'foo bar'),
|
||||||
array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%'),
|
array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%'),
|
||||||
array(true, 'Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'),
|
array(true, 'Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'),
|
||||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', true, 'UTF-8'),
|
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', true, 'UTF-8'),
|
||||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', 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, 'Str contains foo bar', 'Foo bar'),
|
||||||
array(false, 'This string contains foo bar', 'foobar'),
|
array(false, 'Str contains foo bar', 'foobar'),
|
||||||
array(false, 'This string contains foo bar', 'foo bar '),
|
array(false, 'Str contains foo bar', 'foo bar '),
|
||||||
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', true, 'UTF-8'),
|
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', true, 'UTF-8'),
|
||||||
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', true, 'UTF-8'),
|
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', true, 'UTF-8'),
|
||||||
array(true, 'This string contains foo bar', 'Foo bar', false),
|
array(true, 'Str contains foo bar', 'Foo bar', false),
|
||||||
array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%', 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(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, 'Str contains foo bar', 'foobar', false),
|
||||||
array(false, 'This string contains foo bar', 'foo bar ', false),
|
array(false, 'Str contains foo bar', 'foo bar ', false),
|
||||||
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', false, 'UTF-8'),
|
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', false, 'UTF-8'),
|
||||||
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßÅ˚', false, 'UTF-8')
|
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßÅ˚', false, 'UTF-8')
|
||||||
);
|
);
|
||||||
@@ -409,27 +409,27 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$provider = array(
|
$provider = array(
|
||||||
// No needles
|
// No needles
|
||||||
array(false, 'This string contains foo bar', array()),
|
array(false, 'Str contains foo bar', array()),
|
||||||
// Multiple needles
|
// Multiple needles
|
||||||
array(true, 'This string contains foo bar', array('foo', 'bar')),
|
array(true, 'Str contains foo bar', array('foo', 'bar')),
|
||||||
array(true, '12398!@(*%!@# @!%#*&^%', array(' @!%#*', '&^%')),
|
array(true, '12398!@(*%!@# @!%#*&^%', array(' @!%#*', '&^%')),
|
||||||
array(true, 'Ο συγγραφέας είπε', array('συγγρ', 'αφέας'), 'UTF-8'),
|
array(true, 'Ο συγγραφέας είπε', array('συγγρ', 'αφέας'), 'UTF-8'),
|
||||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array('å´¥', '©'), true, 'UTF-8'),
|
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array('å´¥', '©'), true, 'UTF-8'),
|
||||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array('å˚ ', '∆'), true, '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, 'Str contains foo bar', array('Foo', 'Bar')),
|
||||||
array(false, 'This string contains foo bar', array('foobar', 'bar ')),
|
array(false, 'Str contains foo bar', array('foobar', 'bar ')),
|
||||||
array(false, 'This string contains foo bar', array('foo bar ', ' foo')),
|
array(false, 'Str contains foo bar', array('foo bar ', ' foo')),
|
||||||
array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας ', ' συγγραφ '), true, 'UTF-8'),
|
array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας ', ' συγγραφ '), true, 'UTF-8'),
|
||||||
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, 'Str contains foo bar', array('Foo bar', 'bar'), false),
|
||||||
array(true, '12398!@(*%!@# @!%#*&^%', array(' @!%#*&^%', '*&^%'), 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(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, 'Str contains foo bar', array('foobar', 'none'), false),
|
||||||
array(false, 'This string contains foo bar', array('foo bar ', ' ba '), false),
|
array(false, 'Str contains foo bar', array('foo bar ', ' ba '), false),
|
||||||
array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας ', ' ραφέ '), false, 'UTF-8'),
|
array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας ', ' ραφέ '), false, 'UTF-8'),
|
||||||
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array(' ßÅ˚', ' Å˚ '), false, 'UTF-8'),
|
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array(' ßÅ˚', ' Å˚ '), false, 'UTF-8'),
|
||||||
);
|
);
|
||||||
@@ -437,6 +437,43 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return array_merge($singleNeedle, $provider);
|
return array_merge($singleNeedle, $provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function containsAllProvider()
|
||||||
|
{
|
||||||
|
// One needle
|
||||||
|
$singleNeedle = array_map(function($array) {
|
||||||
|
$array[2] = array($array[2]);
|
||||||
|
return $array;
|
||||||
|
}, $this->containsProvider());
|
||||||
|
|
||||||
|
$provider = array(
|
||||||
|
// One needle
|
||||||
|
array(false, 'Str contains foo bar', array()),
|
||||||
|
// Multiple needles
|
||||||
|
array(true, 'Str 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, 'Str contains foo bar', array('Foo', 'bar')),
|
||||||
|
array(false, 'Str contains foo bar', array('foobar', 'bar')),
|
||||||
|
array(false, 'Str contains foo bar', array('foo bar ', 'bar')),
|
||||||
|
array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας ', ' συγγραφ '), true, 'UTF-8'),
|
||||||
|
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array(' ßå˚', ' ß '), true, 'UTF-8'),
|
||||||
|
array(true, 'Str 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, 'Str contains foo bar', array('foobar', 'none'), false),
|
||||||
|
array(false, 'Str contains foo bar', array('foo bar ', ' ba'), false),
|
||||||
|
array(false, 'Ο συγγραφέας είπε', array(' συγγραφέας ', ' ραφέ '), false, 'UTF-8'),
|
||||||
|
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', array(' ßÅ˚', ' Å˚ '), false, 'UTF-8'),
|
||||||
|
);
|
||||||
|
|
||||||
|
return array_merge($singleNeedle, $provider);
|
||||||
|
}
|
||||||
|
|
||||||
public function surroundProvider()
|
public function surroundProvider()
|
||||||
{
|
{
|
||||||
|
@@ -296,6 +296,17 @@ class StaticStringyTestCase extends CommonTest
|
|||||||
$this->assertEquals($expected, $result);
|
$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()
|
* @dataProvider surroundProvider()
|
||||||
*/
|
*/
|
||||||
|
@@ -494,6 +494,19 @@ class StringyTestCase extends CommonTest
|
|||||||
$this->assertEquals($haystack, $stringy);
|
$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()
|
* @dataProvider surroundProvider()
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user