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