Skip to content

Commit 62167b7

Browse files
committed
Add containsAll
1 parent 4dcf84e commit 62167b7

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/Str.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ public static function containsAny(string $haystack, array $needles, bool $caseS
104104
return false;
105105
}
106106

107+
/**
108+
* Check if the given string contains all the specified substrings
109+
*
110+
* @param string $haystack
111+
* @param array $needles
112+
* @param bool $caseSensitive
113+
*
114+
* @return bool
115+
*/
116+
public static function containsAll(string $haystack, array $needles, bool $caseSensitive = true): bool
117+
{
118+
foreach ($needles as $needle) {
119+
if (! self::contains($haystack, $needle, $caseSensitive)) {
120+
return false;
121+
}
122+
}
123+
124+
return true;
125+
}
126+
107127
/**
108128
* Split string into an array padded to the size specified by limit
109129
*

tests/StrTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,40 @@ public function testContainsAnyReturnsFalseIfStringContainsNoneOfTheSpecifiedSub
129129
], true));
130130
}
131131

132+
public function testContainsAllReturnsTrueIfStringContainsAllSpecifiedSubstrings()
133+
{
134+
$this->assertTrue(Str::containsAll(
135+
'host=db1.example.com;dbname=icingaweb2;charset=utf8',
136+
['host=', 'dbname=', 'charset='],
137+
));
138+
}
139+
140+
public function testContainsAllReturnsFalseIfStringIsMissingOneOfTheSpecifiedSubstrings()
141+
{
142+
$this->assertFalse(Str::containsAll(
143+
'host=db1.example.com;charset=utf8',
144+
['host=', 'dbname=', 'charset='],
145+
));
146+
}
147+
148+
public function testContainsAllReturnsTrueIfStringContainsAllSpecifiedSubstringsCaseInsensitively()
149+
{
150+
$this->assertTrue(Str::containsAll(
151+
'HOST=db1.example.com;DBNAME=icingaweb2',
152+
['host=', 'dbname='],
153+
false,
154+
));
155+
}
156+
157+
public function testContainsAllReturnsFalseIfStringIsMissingOneOfTheSpecifiedSubstringsAndCaseIsStrict()
158+
{
159+
$this->assertFalse(Str::containsAll(
160+
'HOST=db1.example.com;DBNAME=icingaweb2',
161+
['host=', 'dbname='],
162+
true,
163+
));
164+
}
165+
132166
public function testIsEmptyReturnsTrueForNull()
133167
{
134168
$this->assertTrue(Str::isEmpty(null));

0 commit comments

Comments
 (0)