1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Util; |
3
|
|
|
|
4
|
|
|
use RuntimeException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* String utility class. |
8
|
|
|
* |
9
|
|
|
* @package phpbu |
10
|
|
|
* @subpackage Util |
11
|
|
|
* @author Sebastian Feldmann <[email protected]> |
12
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
13
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
14
|
|
|
* @link http://phpbu.de/ |
15
|
|
|
* @since Class available since Release 1.0.0 |
16
|
|
|
*/ |
17
|
|
|
class Str |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Date placeholder replacement. |
21
|
|
|
* Replaces %{somevalue} with date({somevalue}). |
22
|
|
|
* |
23
|
|
|
* @param string $string |
24
|
|
|
* @param mixed <integer|null> $time |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
70 |
|
public static function replaceDatePlaceholders($string, $time = null) |
28
|
|
|
{ |
29
|
70 |
|
$time = $time === null ? time() : $time; |
30
|
70 |
|
return preg_replace_callback( |
31
|
70 |
|
'#%([a-zA-Z])#', |
32
|
70 |
|
function($match) use ($time) { |
33
|
42 |
|
return date($match[1], $time); |
34
|
70 |
|
}, |
35
|
70 |
|
$string |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Does a given string contain a date placeholder. |
41
|
|
|
* |
42
|
|
|
* @param string $string |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
43 |
|
public static function isContainingPlaceholder($string) |
46
|
|
|
{ |
47
|
43 |
|
return false !== strpos($string, '%'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Replaces %TARGET_DIR% and %TARGET_FILE% in given string. |
52
|
|
|
* |
53
|
|
|
* @param string $string |
54
|
|
|
* @param string $target |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
4 |
|
public static function replaceTargetPlaceholders($string, $target) |
58
|
|
|
{ |
59
|
4 |
|
$targetDir = dirname($target); |
60
|
4 |
|
$search = ['%TARGET_DIR%', '%TARGET_FILE%']; |
61
|
4 |
|
$replace = [$targetDir, $target]; |
62
|
4 |
|
return str_replace($search, $replace, $string); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create a regex that matches the raw path considering possible date placeholders. |
67
|
|
|
* |
68
|
|
|
* @param string $stringWithDatePlaceholders |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
9 |
|
public static function datePlaceholdersToRegex($stringWithDatePlaceholders) |
72
|
|
|
{ |
73
|
9 |
|
$regex = preg_quote($stringWithDatePlaceholders, '#'); |
74
|
9 |
|
return preg_replace('#%[a-z]#i', '[0-9a-z]+', $regex); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Converts a given value to boolean. |
79
|
|
|
* |
80
|
|
|
* @param string $value |
81
|
|
|
* @param bool $default |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
103 |
|
public static function toBoolean($value, $default) |
85
|
|
|
{ |
86
|
103 |
|
if (is_bool($value)) { |
87
|
1 |
|
return $value; |
88
|
|
|
} |
89
|
103 |
|
if (strtolower($value) == 'false') { |
90
|
17 |
|
return false; |
91
|
103 |
|
} elseif (strtolower($value) == 'true') { |
92
|
28 |
|
return true; |
93
|
|
|
} |
94
|
98 |
|
return $default; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return given size in bytes. |
99
|
|
|
* Allowed units: |
100
|
|
|
* B => byte |
101
|
|
|
* K => kilo byte |
102
|
|
|
* M => mega byte |
103
|
|
|
* G => giga byte |
104
|
|
|
* T => terra byte |
105
|
|
|
* P => peta byte |
106
|
|
|
* |
107
|
|
|
* e.g. |
108
|
|
|
* 1K => 1024 |
109
|
|
|
* 2K => 2048 |
110
|
|
|
* ... |
111
|
|
|
* |
112
|
|
|
* @param string $value |
113
|
|
|
* @throws \RuntimeException |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
10 |
View Code Duplication |
public static function toBytes($value) |
117
|
|
|
{ |
118
|
10 |
|
if (!preg_match('#^[0-9]*[BKMGT]$#i', $value)) { |
119
|
1 |
|
throw new RuntimeException('Invalid size value'); |
120
|
|
|
} |
121
|
9 |
|
$units = ['B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4, 'P' => 5]; |
122
|
9 |
|
$unit = strtoupper(substr($value, -1)); |
123
|
9 |
|
$number = intval(substr($value, 0, -1)); |
124
|
|
|
|
125
|
9 |
|
return $number * pow(1024, $units[$unit]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return time in seconds for a given value. |
130
|
|
|
* Allowed units: |
131
|
|
|
* S => second |
132
|
|
|
* I => minute |
133
|
|
|
* D => day |
134
|
|
|
* W => week |
135
|
|
|
* M => month |
136
|
|
|
* Y => year |
137
|
|
|
* |
138
|
|
|
* e.g. |
139
|
|
|
* 2I => 120 |
140
|
|
|
* 10D => 864000 |
141
|
|
|
* ... |
142
|
|
|
* |
143
|
|
|
* @param string $offset |
144
|
|
|
* @throws \RuntimeException |
145
|
|
|
* @return integer |
146
|
|
|
*/ |
147
|
9 |
View Code Duplication |
public static function toTime($offset) |
148
|
|
|
{ |
149
|
9 |
|
if (!preg_match('#^[1-9]+[0-9]*[SIHDWMY]$#i', $offset)) { |
150
|
3 |
|
throw new RuntimeException(sprintf('Invalid value for offset: %s', $offset)); |
151
|
|
|
} |
152
|
6 |
|
$units = ['S' => 1, 'I' => 60, 'H' => 3600, 'D' => 86400, 'W' => 604800, 'M' => 2678400, 'Y' => 31536000]; |
153
|
6 |
|
$unit = strtoupper(substr($offset, -1)); |
154
|
6 |
|
$number = intval(substr($offset, 0, -1)); |
155
|
|
|
|
156
|
6 |
|
return $number * $units[$unit]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Pads all given strings to given length. |
161
|
|
|
* |
162
|
|
|
* @param array $strings |
163
|
|
|
* @param integer $length |
164
|
|
|
* @param string $pad |
165
|
|
|
* @param integer $mode |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
1 |
|
public static function padAll(array $strings, $length, $pad = ' ', $mode = STR_PAD_LEFT) |
169
|
|
|
{ |
170
|
1 |
|
$result = []; |
171
|
1 |
|
foreach ($strings as $key => $s) { |
172
|
1 |
|
$result[$key] = str_pad($s, $length, $pad, $mode); |
173
|
|
|
} |
174
|
1 |
|
return $result; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Explodes string to array but empty string results in empty array not array with empty string in it. |
179
|
|
|
* |
180
|
|
|
* @param string $separated |
181
|
|
|
* @param string $separator |
182
|
|
|
* @param boolean $trim |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
65 |
|
public static function toList($separated, $separator = ',', $trim = true) |
186
|
|
|
{ |
187
|
65 |
|
$list = empty($separated) ? [] : explode($separator, $separated); |
188
|
65 |
|
if ($trim) { |
189
|
64 |
|
$list = array_map('trim', $list); |
190
|
|
|
} |
191
|
65 |
|
return $list; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Determine if the path has a trailing slash. |
196
|
|
|
* |
197
|
|
|
* @param string $string |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
28 |
|
public static function hasTrailingSlash(string $string) : bool |
201
|
|
|
{ |
202
|
28 |
|
return substr($string, -1) === '/'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Adds trailing slash to a string/path if not already there. |
207
|
|
|
* |
208
|
|
|
* @param string $string |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
21 |
|
public static function withTrailingSlash($string) |
212
|
|
|
{ |
213
|
21 |
|
return $string . (self::hasTrailingSlash($string) ? '' : '/'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Removes the trailing slash from a string/path. |
218
|
|
|
* |
219
|
|
|
* @param string $string |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
6 |
|
public static function withoutTrailingSlash($string) |
223
|
|
|
{ |
224
|
6 |
|
return strlen($string) > 1 && self::hasTrailingSlash($string) ? substr($string, 0, -1) : $string; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Determine if the path has a leading slash. |
229
|
|
|
* |
230
|
|
|
* @param string $string |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
9 |
|
public static function hasLeadingSlash(string $string) : bool |
234
|
|
|
{ |
235
|
9 |
|
return substr($string, 0,1) === '/'; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Adds leading slash to a string/path if not already there. |
240
|
|
|
* |
241
|
|
|
* @param string $string |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
6 |
|
public static function withLeadingSlash($string) |
245
|
|
|
{ |
246
|
6 |
|
return (self::hasLeadingSlash($string) ? '' : '/') . $string; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Removes the leading slash from a string/path. |
251
|
|
|
* |
252
|
|
|
* @param string $string |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
1 |
|
public static function withoutLeadingSlash($string) |
256
|
|
|
{ |
257
|
1 |
|
return self::hasLeadingSlash($string) ? substr($string, 1) : $string; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Appends a plural "s" or "'s". |
262
|
|
|
* |
263
|
|
|
* @param string $subject |
264
|
|
|
* @param int $amount |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
6 |
|
public static function appendPluralS($subject, $amount) |
268
|
|
|
{ |
269
|
6 |
|
return $subject . ($amount == 1 ? '' : (substr($subject, -1) == 's' ? '\'s' : 's')); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|