Skip to content

Commit fb96fa9

Browse files
committed
Added functions.php
1 parent ce48a00 commit fb96fa9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
"autoload": {
4040
"psr-4": {
4141
"Okapi\\CodeTransformer\\": "src/"
42-
}
42+
},
43+
"files": [
44+
"src/functions.php"
45+
]
4346
},
4447
"autoload-dev": {
4548
"psr-4": {

src/functions.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
if (!function_exists('str_starts_with_any_but_not')) {
4+
/**
5+
* Determines if the given haystack starts with any of the provided needles,
6+
* but not with any of the provided negative needles.
7+
*
8+
* @param string $haystack The string to search in.
9+
* @param array $needles An array of strings to search for at the
10+
* beginning of the $haystack.
11+
* @param array $negativeNeedles An array of strings to ensure are not at
12+
* the beginning of $haystack.
13+
*
14+
* @return bool Returns {@link true} if $haystack starts with any string in
15+
* $needles, but not with any string in $negativeNeedles.
16+
* Returns {@link false} otherwise.
17+
*/
18+
function str_starts_with_any_but_not(
19+
string $haystack,
20+
array $needles,
21+
array $negativeNeedles = [],
22+
): bool {
23+
// Check for negative needles first
24+
foreach ($negativeNeedles as $negativeNeedle) {
25+
if (str_starts_with($haystack, $negativeNeedle)) {
26+
return false;
27+
}
28+
}
29+
30+
// Check for positive needles
31+
foreach ($needles as $needle) {
32+
if (str_starts_with($haystack, $needle)) {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}
39+
}

0 commit comments

Comments
 (0)