1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Utils. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 Starweb AB |
6
|
|
|
* @license BSD 3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Starlit\Utils; |
10
|
|
|
|
11
|
|
|
class Str |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* CamelCases words or a dash name (eg. "hey-you" or "hey you" becomes "heyYou"). |
15
|
|
|
* |
16
|
|
|
* NB! This method is only for property formatted separated none |
17
|
|
|
* multibyte (UTF-8) ASCII strings. It does not handle malformed strings with |
18
|
|
|
* a lot of other weird characters. |
19
|
|
|
* |
20
|
|
|
* @param string $separatorString |
21
|
|
|
* @param string $separator |
22
|
|
|
* @param bool $upperCamelCase If first character should be capitalized (eg. "HeyYou" instead of "heyYou") |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
2 |
|
public static function separatorToCamel( |
26
|
|
|
string $separatorString, |
27
|
|
|
string $separator = '_', |
28
|
|
|
bool $upperCamelCase = false |
29
|
|
|
): string { |
30
|
|
|
// Using PHP's built in functions seem to be a bit 30% faster than a regular expression replace |
31
|
2 |
|
$wordStr = str_replace($separator, ' ', strtolower($separatorString)); |
32
|
2 |
|
$wordsCapitalized = ucwords($wordStr); |
33
|
2 |
|
$camelCased = str_replace(' ', '', $wordsCapitalized); |
34
|
|
|
|
35
|
2 |
|
return $upperCamelCase ? $camelCased : lcfirst($camelCased); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Converts CamelCase text to lowercase separator separated text (eg. "heyYou" becomes "hey-you"). |
40
|
|
|
* |
41
|
|
|
* NB! This method is only for none multibyte (UTF-8) ASCII strings. |
42
|
|
|
* |
43
|
|
|
* @param string $camelCaseString |
44
|
|
|
* @param string $separator |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
1 |
|
public static function camelToSeparator(string $camelCaseString, string $separator = '_'): string |
48
|
|
|
{ |
49
|
1 |
|
$separatedString = preg_replace('/([a-zA-Z])([A-Z])/', '$1' . $separator . '$2', $camelCaseString); |
50
|
1 |
|
$separatedString = preg_replace('/([A-Z0-9])([A-Z])/', '$1' . $separator . '$2', $separatedString); |
51
|
|
|
|
52
|
1 |
|
return strtolower($separatedString); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get a random string generated from provided values. |
57
|
|
|
* |
58
|
|
|
* @param int $charCount |
59
|
|
|
* @param string $characters |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
1 |
|
public static function random(int $charCount, string $characters = 'abcdefghijklmnopqrstuvqxyz0123456789'): string |
63
|
|
|
{ |
64
|
1 |
|
$randomString = ''; |
65
|
1 |
|
for ($i = 0; $i < $charCount; $i++) { |
66
|
1 |
|
$pos = mt_rand(0, strlen($characters) - 1); |
67
|
1 |
|
$randomString .= $characters[$pos]; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $randomString; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get shortened string. |
75
|
|
|
* |
76
|
|
|
* @param string $string |
77
|
|
|
* @param int $maxLength |
78
|
|
|
* @param string $indicator |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
1 |
|
public static function truncate(string $string, int $maxLength, string $indicator = '...'): string |
82
|
|
|
{ |
83
|
1 |
|
if (mb_strlen($string) > $maxLength) { |
84
|
1 |
|
$string = mb_substr($string, 0, $maxLength - mb_strlen($indicator)) . $indicator; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $string; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Check if a string start with another substring. |
92
|
|
|
* |
93
|
|
|
* @param string $string |
94
|
|
|
* @param string $search |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
3 |
|
public static function startsWith(string $string, string $search): bool |
98
|
|
|
{ |
99
|
3 |
|
return strpos($string, $search) === 0; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check if a string ends with another substring. |
104
|
|
|
* |
105
|
|
|
* @param string $string |
106
|
|
|
* @param string $search |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
4 |
|
public static function endsWith(string $string, string $search): bool |
110
|
|
|
{ |
111
|
4 |
|
return (substr($string, -strlen($search)) === $search); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Remove left part of string and return the new string. |
116
|
|
|
* |
117
|
|
|
* @param string $string |
118
|
|
|
* @param string $strip |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
1 |
|
public static function stripLeft(string $string, string $strip): string |
122
|
|
|
{ |
123
|
1 |
|
if (self::startsWith($string, $strip)) { |
124
|
1 |
|
return substr($string, strlen($strip)); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return $string; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Remove right part of string and return the new string. |
132
|
|
|
* |
133
|
|
|
* @param string $string |
134
|
|
|
* @param string $strip |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
2 |
|
public static function stripRight(string $string, string $strip): string |
138
|
|
|
{ |
139
|
2 |
|
if (self::endsWith($string, $strip)) { |
140
|
2 |
|
return substr($string, 0, -strlen($strip)); |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return $string; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Increment a string/separated string. |
148
|
|
|
* |
149
|
|
|
* Example: "a-name" becomes "a-name-2", "a-name-3" and so on. |
150
|
|
|
* |
151
|
|
|
* @param string $string |
152
|
|
|
* @param string $separator |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
1 |
|
public static function incrementSeparated(string $string, string $separator = '-'): string |
156
|
|
|
{ |
157
|
1 |
|
if (preg_match('/' . $separator . '(\d+)$/', $string, $matches)) { |
158
|
1 |
|
$string = self::stripRight($string, $separator . $matches[1]); |
159
|
1 |
|
$duplicateNo = ((int) $matches[1]) + 1; |
160
|
|
|
} else { |
161
|
1 |
|
$duplicateNo = 2; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
$newString = $string . $separator . $duplicateNo; |
165
|
|
|
|
166
|
1 |
|
return $newString; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $string |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
1 |
|
public static function uppercaseFirst(string $string): string |
174
|
|
|
{ |
175
|
1 |
|
$length = mb_strlen($string); |
176
|
1 |
|
$firstChar = mb_substr($string, 0, 1); |
177
|
1 |
|
$otherChars = mb_substr($string, 1, $length - 1); |
178
|
|
|
|
179
|
1 |
|
return mb_strtoupper($firstChar) . $otherChars; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Filter out all non numeric characters and return a clean numeric string. |
184
|
|
|
* |
185
|
|
|
* @param string $string |
186
|
|
|
* @param bool $allowDecimal |
187
|
|
|
* @param bool $allowNegative |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
3 |
|
public static function toNumber(string $string, bool $allowDecimal = false, bool $allowNegative = false): string |
191
|
|
|
{ |
192
|
3 |
|
$string = trim($string); |
193
|
|
|
|
194
|
3 |
|
$decimalPart = ''; |
195
|
3 |
|
if (($firstPos = strpos($string, '.')) !== false) { |
196
|
2 |
|
$integerPart = substr($string, 0, $firstPos); |
197
|
2 |
|
if ($allowDecimal) { |
198
|
1 |
|
$rawDecimalPart = substr($string, $firstPos); |
199
|
1 |
|
$filteredDecimalPart = rtrim(preg_replace('/[^0-9]/', '', $rawDecimalPart), '0'); |
200
|
1 |
|
if (!empty($filteredDecimalPart)) { |
201
|
2 |
|
$decimalPart = '.' . $filteredDecimalPart; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} else { |
205
|
3 |
|
$integerPart = $string; |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
$integerPart = ltrim(preg_replace('/[^0-9]/', '', $integerPart), '0'); |
209
|
3 |
|
$integerPart = $integerPart ?: '0'; |
210
|
|
|
|
211
|
3 |
|
$minusSign = ''; |
212
|
3 |
|
if (strpos($string, '-') === 0) { |
213
|
2 |
|
if (!$allowNegative) { |
214
|
1 |
|
return '0'; |
215
|
1 |
|
} elseif (!($integerPart === '0' && empty($decimalPart))) { |
216
|
1 |
|
$minusSign = '-'; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
3 |
|
$number = $minusSign . $integerPart . $decimalPart; |
221
|
|
|
|
222
|
3 |
|
return $number; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $search |
227
|
|
|
* @param string $replace |
228
|
|
|
* @param string $subject |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
2 |
|
public static function replaceFirst(string $search,string $replace,string $subject): string |
232
|
|
|
{ |
233
|
2 |
|
if (($pos = strpos($subject, $search)) !== false) { |
234
|
2 |
|
return substr_replace($subject, $replace, $pos, strlen($search)); |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
return $subject; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|