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