1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Botonomous\utility; |
4
|
|
|
|
5
|
|
|
use Botonomous\BotonomousException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class StringUtility. |
9
|
|
|
*/ |
10
|
|
|
class StringUtility extends AbstractUtility |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $json |
14
|
|
|
* |
15
|
|
|
* @throws \Exception |
16
|
|
|
* |
17
|
|
|
* @return array|mixed |
18
|
|
|
*/ |
19
|
22 |
|
public function jsonToArray(string $json) |
20
|
|
|
{ |
21
|
22 |
|
$array = empty($json) ? [] : json_decode($json, true); |
22
|
22 |
|
if ($array === null || !is_array($array) || json_last_error() !== 0) { |
23
|
1 |
|
throw new BotonomousException('Invalid JSON content'); |
24
|
|
|
} |
25
|
|
|
|
26
|
22 |
|
return $array; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $toRemove |
31
|
|
|
* @param string $subject |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
1 |
|
public function removeStringFromString(string $toRemove, string $subject): string |
36
|
|
|
{ |
37
|
|
|
// pattern: !\s+! is used to replace multiple spaces with single space |
38
|
1 |
|
return trim(preg_replace('!\s+!', ' ', str_replace($toRemove, '', $subject))); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $toFind |
43
|
|
|
* @param string $wordBoundary |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
11 |
|
private function getFindPattern(string $toFind, string $wordBoundary): string |
48
|
|
|
{ |
49
|
11 |
|
return $wordBoundary === true ? "/\b{$toFind}\b/" : "/{$toFind}/"; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $toFind |
54
|
|
|
* @param string $subject |
55
|
|
|
* @param bool $wordBoundary If true $toFind is searched with word boundaries |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
5 |
|
public function findInString(string $toFind, string $subject, bool $wordBoundary = true): bool |
60
|
|
|
{ |
61
|
5 |
|
$pattern = $this->getFindPattern($toFind, $wordBoundary); |
|
|
|
|
62
|
|
|
|
63
|
5 |
|
return preg_match($pattern, $subject) ? true : false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $toFind |
68
|
|
|
* @param string $subject |
69
|
|
|
* @param bool $wordBoundary |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
6 |
|
public function findPositionInString(string $toFind, string $subject, bool $wordBoundary = true) |
74
|
|
|
{ |
75
|
6 |
|
$pattern = $this->getFindPattern($toFind, $wordBoundary); |
|
|
|
|
76
|
6 |
|
$positions = []; |
77
|
6 |
|
if (preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE) && !empty($matches[0])) { |
78
|
4 |
|
foreach ($matches[0] as $match) { |
79
|
4 |
|
$positions[] = $match[1]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
return $positions; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Convert snake case to camel case e.g. admin_user becomes AdminUser. |
88
|
|
|
* |
89
|
|
|
* @param string $string |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
20 |
|
public function snakeCaseToCamelCase(string $string) |
94
|
|
|
{ |
95
|
20 |
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $string))); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check subject to see whether $string1 is followed by $string2. |
100
|
|
|
* |
101
|
|
|
* @param string $subject |
102
|
|
|
* @param string $string1 |
103
|
|
|
* @param string $string2 |
104
|
|
|
* @param array $exceptions |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
1 |
|
public function isString1FollowedByString2( |
109
|
|
|
string $subject, |
110
|
|
|
string $string1, |
111
|
|
|
string $string2, |
112
|
|
|
array $exceptions = [] |
113
|
|
|
): bool { |
114
|
1 |
|
$exceptionsString = ''; |
115
|
1 |
|
if (!empty($exceptions)) { |
116
|
1 |
|
$exceptions = implode('|', $exceptions); |
117
|
1 |
|
$exceptionsString = "(?<!{$exceptions})"; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$pattern = '/'.$string1.'(?:\s+\w+'.$exceptionsString.'){0,2}\s+'.$string2.'\b/'; |
121
|
|
|
|
122
|
1 |
|
return preg_match($pattern, $subject) ? true : false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $haystack |
127
|
|
|
* @param string $needle |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
13 |
|
public function endsWith(string $haystack, string $needle): bool |
132
|
|
|
{ |
133
|
13 |
|
$length = strlen($needle); |
134
|
|
|
|
135
|
13 |
|
if ($length === 0) { |
136
|
1 |
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
13 |
|
return substr($haystack, -$length) === $needle; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Apply replacements in a string |
144
|
|
|
* Replacement key in the string should be like {replacementKey}. |
145
|
|
|
* |
146
|
|
|
* @param $subject mixed |
147
|
|
|
* @param $replacements array |
148
|
|
|
* |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
109 |
|
public function applyReplacements($subject, array $replacements) |
152
|
|
|
{ |
153
|
109 |
|
if (empty($replacements) || !is_string($subject)) { |
154
|
107 |
|
return $subject; |
155
|
|
|
} |
156
|
|
|
|
157
|
5 |
|
foreach ($replacements as $key => $value) { |
158
|
5 |
|
$subject = str_replace('{'.$key.'}', $value, $subject); |
159
|
|
|
} |
160
|
|
|
|
161
|
5 |
|
return $subject; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: