1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see AppUtils\ConvertHelper} class. |
4
|
|
|
* |
5
|
|
|
* @package Application Utils |
6
|
|
|
* @subpackage ConvertHelper |
7
|
|
|
* @see AppUtils\ConvertHelper |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace AppUtils; |
11
|
|
|
|
12
|
|
|
use AppUtils\ConvertHelper\WordSplitter; |
13
|
|
|
use DateInterval; |
14
|
|
|
use DateTime; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Static conversion helper class: offers a number of utility methods |
18
|
|
|
* to convert variable types, as well as specialized methods for working |
19
|
|
|
* with specific types like dates. |
20
|
|
|
* |
21
|
|
|
* @package Application Utils |
22
|
|
|
* @subpackage ConvertHelper |
23
|
|
|
* @author Sebastian Mordziol <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ConvertHelper |
26
|
|
|
{ |
27
|
|
|
public const ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER = 23303; |
28
|
|
|
public const ERROR_CANNOT_NORMALIZE_NON_SCALAR_VALUE = 23304; |
29
|
|
|
public const ERROR_JSON_ENCODE_FAILED = 23305; |
30
|
|
|
public const ERROR_INVALID_BOOLEAN_STRING = 23306; |
31
|
|
|
|
32
|
|
|
public const INTERVAL_DAYS = 'days'; |
33
|
|
|
public const INTERVAL_HOURS = 'hours'; |
34
|
|
|
public const INTERVAL_MINUTES = 'minutes'; |
35
|
|
|
public const INTERVAL_SECONDS = 'seconds'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Normalizes tabs in the specified string by indenting everything |
39
|
|
|
* back to the minimum tab distance. With the second parameter, |
40
|
|
|
* tabs can optionally be converted to spaces as well (recommended |
41
|
|
|
* for HTML output). |
42
|
|
|
* |
43
|
|
|
* @param string $string |
44
|
|
|
* @param boolean $tabs2spaces |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public static function normalizeTabs(string $string, bool $tabs2spaces = false) : string |
48
|
|
|
{ |
49
|
|
|
return ConvertHelper_String::normalizeTabs($string, $tabs2spaces); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Converts tabs to spaces in the specified string. |
54
|
|
|
* |
55
|
|
|
* @param string $string |
56
|
|
|
* @param int $tabSize The amount of spaces per tab. |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public static function tabs2spaces(string $string, int $tabSize=4) : string |
60
|
|
|
{ |
61
|
|
|
return ConvertHelper_String::tabs2spaces($string, $tabSize); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Converts spaces to tabs in the specified string. |
66
|
|
|
* |
67
|
|
|
* @param string $string |
68
|
|
|
* @param int $tabSize The amount of spaces per tab in the source string. |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public static function spaces2tabs(string $string, int $tabSize=4) : string |
72
|
|
|
{ |
73
|
|
|
return ConvertHelper_String::spaces2tabs($string, $tabSize); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Makes all hidden characters visible in the target string, |
78
|
|
|
* from spaces to control characters. |
79
|
|
|
* |
80
|
|
|
* @param string $string |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public static function hidden2visible(string $string) : string |
84
|
|
|
{ |
85
|
|
|
return ConvertHelper_String::hidden2visible($string); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Converts the specified amount of seconds into |
90
|
|
|
* a human-readable string split in months, weeks, |
91
|
|
|
* days, hours, minutes and seconds. |
92
|
|
|
* |
93
|
|
|
* @param float $seconds |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public static function time2string($seconds) : string |
97
|
|
|
{ |
98
|
|
|
$converter = new ConvertHelper_TimeConverter($seconds); |
99
|
|
|
return $converter->toString(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Converts a timestamp into an easily understandable |
104
|
|
|
* format, e.g. "2 hours", "1 day", "3 months" |
105
|
|
|
* |
106
|
|
|
* If you set the date to parameter, the difference |
107
|
|
|
* will be calculated between the two dates and not |
108
|
|
|
* the current time. |
109
|
|
|
* |
110
|
|
|
* @param integer|DateTime $datefrom |
111
|
|
|
* @param integer|DateTime $dateto |
112
|
|
|
* @return string |
113
|
|
|
* |
114
|
|
|
* @throws ConvertHelper_Exception |
115
|
|
|
* @see ConvertHelper_DurationConverter::ERROR_NO_DATE_FROM_SET |
116
|
|
|
*/ |
117
|
|
|
public static function duration2string($datefrom, $dateto = -1) : string |
118
|
|
|
{ |
119
|
|
|
return ConvertHelper_DurationConverter::toString($datefrom, $dateto); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Adds HTML syntax highlighting to the specified SQL string. |
124
|
|
|
* |
125
|
|
|
* @param string $sql |
126
|
|
|
* @return string |
127
|
|
|
* @deprecated Use the Highlighter class directly instead. |
128
|
|
|
* @see Highlighter::sql() |
129
|
|
|
*/ |
130
|
|
|
public static function highlight_sql(string $sql) : string |
131
|
|
|
{ |
132
|
|
|
return Highlighter::sql($sql); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Adds HTML syntax highlighting to the specified XML code. |
137
|
|
|
* |
138
|
|
|
* @param string $xml The XML to highlight. |
139
|
|
|
* @param bool $formatSource Whether to format the source with indentation to make it readable. |
140
|
|
|
* @return string |
141
|
|
|
* @deprecated Use the Highlighter class directly instead. |
142
|
|
|
* @see Highlighter::xml() |
143
|
|
|
*/ |
144
|
|
|
public static function highlight_xml(string $xml, bool $formatSource=false) : string |
145
|
|
|
{ |
146
|
|
|
return Highlighter::xml($xml, $formatSource); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $phpCode |
151
|
|
|
* @return string |
152
|
|
|
* @deprecated Use the Highlighter class directly instead. |
153
|
|
|
* @see Highlighter::php() |
154
|
|
|
*/ |
155
|
|
|
public static function highlight_php(string $phpCode) : string |
156
|
|
|
{ |
157
|
|
|
return Highlighter::php($phpCode); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Converts a number of bytes to a human-readable form, |
162
|
|
|
* e.g. xx Kb / xx Mb / xx Gb |
163
|
|
|
* |
164
|
|
|
* @param int $bytes The amount of bytes to convert. |
165
|
|
|
* @param int $precision The amount of decimals |
166
|
|
|
* @param int $base The base to calculate with: Base 10 is default (=1000 Bytes in a KB), Base 2 is mainly used for Windows memory (=1024 Bytes in a KB). |
167
|
|
|
* @return string |
168
|
|
|
* |
169
|
|
|
* @see https://en.m.wikipedia.org/wiki/Megabyte#Definitions |
170
|
|
|
*/ |
171
|
|
|
public static function bytes2readable(int $bytes, int $precision = 1, int $base = ConvertHelper_StorageSizeEnum::BASE_10) : string |
172
|
|
|
{ |
173
|
|
|
return self::parseBytes($bytes)->toString($precision, $base); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Parses a number of bytes, and creates a converter instance which |
178
|
|
|
* allows doing common operations with it. |
179
|
|
|
* |
180
|
|
|
* @param int $bytes |
181
|
|
|
* @return ConvertHelper_ByteConverter |
182
|
|
|
*/ |
183
|
|
|
public static function parseBytes(int $bytes) : ConvertHelper_ByteConverter |
184
|
|
|
{ |
185
|
|
|
return new ConvertHelper_ByteConverter($bytes); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Cuts a text to the specified length if it is longer than the |
190
|
|
|
* target length. Appends a text to signify it has been cut at |
191
|
|
|
* the end of the string. |
192
|
|
|
* |
193
|
|
|
* @param string $text |
194
|
|
|
* @param int $targetLength |
195
|
|
|
* @param string $append |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public static function text_cut(string $text, int $targetLength, string $append = '...') : string |
199
|
|
|
{ |
200
|
|
|
return ConvertHelper_String::cutText($text, $targetLength, $append); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Pretty `var_dump`. |
205
|
|
|
* |
206
|
|
|
* @param mixed $var |
207
|
|
|
* @param bool $html |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public static function var_dump($var, bool $html=true) : string |
211
|
|
|
{ |
212
|
|
|
$info = parseVariable($var); |
213
|
|
|
|
214
|
|
|
if($html) { |
215
|
|
|
return $info->toHTML(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $info->toString(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Pretty `print_r`. |
223
|
|
|
* |
224
|
|
|
* @param mixed $var The variable to dump. |
225
|
|
|
* @param bool $return Whether to return the dumped code. |
226
|
|
|
* @param bool $html Whether to style the dump as HTML. |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public static function print_r($var, bool $return=false, bool $html=true) : string |
230
|
|
|
{ |
231
|
|
|
$result = parseVariable($var)->enableType()->toString(); |
232
|
|
|
|
233
|
|
|
if($html) |
234
|
|
|
{ |
235
|
|
|
$result = |
236
|
|
|
'<pre style="background:#fff;color:#333;padding:16px;border:solid 1px #bbb;border-radius:4px">'. |
237
|
|
|
$result. |
238
|
|
|
'</pre>'; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
if(!$return) |
242
|
|
|
{ |
243
|
|
|
echo $result; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $result; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Converts a string, number or boolean value to a boolean value. |
251
|
|
|
* |
252
|
|
|
* @param mixed $string |
253
|
|
|
* @throws ConvertHelper_Exception |
254
|
|
|
* @return bool |
255
|
|
|
* |
256
|
|
|
* @see ConvertHelper::ERROR_INVALID_BOOLEAN_STRING |
257
|
|
|
*/ |
258
|
|
|
public static function string2bool($string) : bool |
259
|
|
|
{ |
260
|
|
|
return ConvertHelper_Bool::fromString($string); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Whether the specified string is a boolean string or boolean value. |
265
|
|
|
* Alias for {@link ConvertHelper::isBoolean()}. |
266
|
|
|
* |
267
|
|
|
* @param mixed $string |
268
|
|
|
* @return bool |
269
|
|
|
* @deprecated |
270
|
|
|
* @see ConvertHelper::isBoolean() |
271
|
|
|
*/ |
272
|
|
|
public static function isBooleanString($string) : bool |
273
|
|
|
{ |
274
|
|
|
return self::isBoolean($string); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Alias for the {@\AppUtils\XMLHelper::string2xml()} method. |
279
|
|
|
* |
280
|
|
|
* @param string $text |
281
|
|
|
* @return string |
282
|
|
|
* @throws XMLHelper_Exception |
283
|
|
|
* @deprecated Use the XMLHelper method instead. |
284
|
|
|
*/ |
285
|
|
|
public static function text_makeXMLCompliant(string $text) : string |
286
|
|
|
{ |
287
|
|
|
return XMLHelper::string2xml($text); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Transforms a date into a generic human-readable date, optionally with time. |
292
|
|
|
* If the year is the same as the current one, it is omitted. |
293
|
|
|
* |
294
|
|
|
* - 6 Jan 2012 |
295
|
|
|
* - 12 Dec 2012 17:45 |
296
|
|
|
* - 5 Aug |
297
|
|
|
* |
298
|
|
|
* @param DateTime $date |
299
|
|
|
* @param bool $includeTime |
300
|
|
|
* @param bool $shortMonth |
301
|
|
|
* @return string |
302
|
|
|
* |
303
|
|
|
* @throws ConvertHelper_Exception |
304
|
|
|
* @see ConvertHelper::ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER |
305
|
|
|
*/ |
306
|
|
|
public static function date2listLabel(DateTime $date, bool $includeTime = false, bool $shortMonth = false) : string |
307
|
|
|
{ |
308
|
|
|
return ConvertHelper_Date::toListLabel($date, $includeTime, $shortMonth); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Returns a human-readable month name given the month number. Can optionally |
313
|
|
|
* return the shorthand version of the month. Translated into the current |
314
|
|
|
* application locale. |
315
|
|
|
* |
316
|
|
|
* @param int|string $monthNr |
317
|
|
|
* @param boolean $short |
318
|
|
|
* @return string |
319
|
|
|
* |
320
|
|
|
* @throws ConvertHelper_Exception |
321
|
|
|
* @see ConvertHelper::ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER |
322
|
|
|
*/ |
323
|
|
|
public static function month2string($monthNr, bool $short = false) : string |
324
|
|
|
{ |
325
|
|
|
return ConvertHelper_Date::month2string($monthNr, $short); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Transliterates a string. |
330
|
|
|
* |
331
|
|
|
* @param string $string |
332
|
|
|
* @param string $spaceChar |
333
|
|
|
* @param bool $lowercase |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
public static function transliterate(string $string, string $spaceChar = '-', bool $lowercase = true) : string |
337
|
|
|
{ |
338
|
|
|
return ConvertHelper_String::transliterate($string, $spaceChar, $lowercase); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Retrieves the HEX character codes for all control |
343
|
|
|
* characters that the {@link stripControlCharacters()} |
344
|
|
|
* method will remove. |
345
|
|
|
* |
346
|
|
|
* @return string[] |
347
|
|
|
*/ |
348
|
|
|
public static function getControlCharactersAsHex() : array |
349
|
|
|
{ |
350
|
|
|
return self::createControlCharacters()->getCharsAsHex(); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Retrieves an array of all control characters that |
355
|
|
|
* the {@link stripControlCharacters()} method will |
356
|
|
|
* remove, as the actual UTF-8 characters. |
357
|
|
|
* |
358
|
|
|
* @return string[] |
359
|
|
|
*/ |
360
|
|
|
public static function getControlCharactersAsUTF8() : array |
361
|
|
|
{ |
362
|
|
|
return self::createControlCharacters()->getCharsAsUTF8(); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Retrieves all control characters as JSON encoded |
367
|
|
|
* characters, e.g. "\u200b". |
368
|
|
|
* |
369
|
|
|
* @return string[] |
370
|
|
|
*/ |
371
|
|
|
public static function getControlCharactersAsJSON() : array |
372
|
|
|
{ |
373
|
|
|
return self::createControlCharacters()->getCharsAsJSON(); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Removes all control characters from the specified string |
378
|
|
|
* that can cause problems in some cases, like creating |
379
|
|
|
* valid XML documents. This includes invisible non-breaking |
380
|
|
|
* spaces. |
381
|
|
|
* |
382
|
|
|
* @param string $string |
383
|
|
|
* @return string |
384
|
|
|
* @throws ConvertHelper_Exception |
385
|
|
|
*/ |
386
|
|
|
public static function stripControlCharacters(string $string) : string |
387
|
|
|
{ |
388
|
|
|
return self::createControlCharacters()->stripControlCharacters($string); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Creates the control characters class, used to |
393
|
|
|
* work with control characters in strings. |
394
|
|
|
* |
395
|
|
|
* @return ConvertHelper_ControlCharacters |
396
|
|
|
*/ |
397
|
|
|
public static function createControlCharacters() : ConvertHelper_ControlCharacters |
398
|
|
|
{ |
399
|
|
|
return new ConvertHelper_ControlCharacters(); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Converts a unicode character to the PHP notation. |
404
|
|
|
* |
405
|
|
|
* Example: |
406
|
|
|
* |
407
|
|
|
* <pre>unicodeChar2php('"\u0000"')</pre> |
408
|
|
|
* |
409
|
|
|
* Returns |
410
|
|
|
* |
411
|
|
|
* <pre>\x0</pre> |
412
|
|
|
* |
413
|
|
|
* @param string $unicodeChar |
414
|
|
|
* @return string |
415
|
|
|
*/ |
416
|
|
|
public static function unicodeChar2php(string $unicodeChar) : string |
417
|
|
|
{ |
418
|
|
|
$unicodeChar = json_decode($unicodeChar); |
419
|
|
|
|
420
|
|
|
$output = ''; |
421
|
|
|
$split = str_split($unicodeChar); |
422
|
|
|
|
423
|
|
|
foreach($split as $octet) |
424
|
|
|
{ |
425
|
|
|
$ordInt = ord($octet); |
426
|
|
|
// Convert from int (base 10) to hex (base 16), for PHP \x syntax |
427
|
|
|
$ordHex = base_convert((string)$ordInt, 10, 16); |
428
|
|
|
$output .= '\x' . $ordHex; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
return $output; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Removes the extension from the specified filename |
436
|
|
|
* and returns the name without the extension. |
437
|
|
|
* |
438
|
|
|
* Examples: |
439
|
|
|
* |
440
|
|
|
* <ul> |
441
|
|
|
* <li>filename.html > filename</li> |
442
|
|
|
* <li>passed.test.jpg > passed.test</li> |
443
|
|
|
* <li>path/to/file/document.txt > document</li> |
444
|
|
|
* </ul> |
445
|
|
|
* |
446
|
|
|
* @param string $filename |
447
|
|
|
* @return string |
448
|
|
|
*/ |
449
|
|
|
public static function filenameRemoveExtension(string $filename) : string |
450
|
|
|
{ |
451
|
|
|
return FileHelper::removeExtension($filename); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @param mixed $a |
456
|
|
|
* @param mixed $b |
457
|
|
|
* @return bool |
458
|
|
|
* |
459
|
|
|
* @throws ConvertHelper_Exception |
460
|
|
|
* @see ConvertHelper::ERROR_CANNOT_NORMALIZE_NON_SCALAR_VALUE |
461
|
|
|
*/ |
462
|
|
|
public static function areVariablesEqual($a, $b) : bool |
463
|
|
|
{ |
464
|
|
|
return ConvertHelper_Comparator::areVariablesEqual($a, $b); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Compares two strings to check whether they are equal. |
469
|
|
|
* null and empty strings are considered equal. |
470
|
|
|
* |
471
|
|
|
* @param string|NULL $a |
472
|
|
|
* @param string|NULL $b |
473
|
|
|
* @return boolean |
474
|
|
|
* |
475
|
|
|
* @throws ConvertHelper_Exception |
476
|
|
|
* @see ConvertHelper::ERROR_CANNOT_NORMALIZE_NON_SCALAR_VALUE |
477
|
|
|
*/ |
478
|
|
|
public static function areStringsEqual(?string $a, ?string $b) : bool |
479
|
|
|
{ |
480
|
|
|
return ConvertHelper_Comparator::areStringsEqual($a, $b); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Checks whether the two specified numbers are equal. |
485
|
|
|
* null and empty strings are considered as 0 values. |
486
|
|
|
* |
487
|
|
|
* @param number|string|NULL $a |
488
|
|
|
* @param number|string|NULL $b |
489
|
|
|
* @return boolean |
490
|
|
|
* |
491
|
|
|
* @throws ConvertHelper_Exception |
492
|
|
|
* @see ConvertHelper::ERROR_CANNOT_NORMALIZE_NON_SCALAR_VALUE |
493
|
|
|
*/ |
494
|
|
|
public static function areNumbersEqual($a, $b) : bool |
495
|
|
|
{ |
496
|
|
|
return ConvertHelper_Comparator::areNumbersEqual($a, $b); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* Converts a boolean value to a string. Defaults to returning |
501
|
|
|
* 'true' or 'false', with the additional parameter it can also |
502
|
|
|
* return the 'yes' and 'no' variants. |
503
|
|
|
* |
504
|
|
|
* @param boolean|string|int $boolean |
505
|
|
|
* @param boolean $yesNo |
506
|
|
|
* @return string |
507
|
|
|
* @throws ConvertHelper_Exception |
508
|
|
|
* @see ConvertHelper::ERROR_INVALID_BOOLEAN_STRING |
509
|
|
|
*/ |
510
|
|
|
public static function bool2string($boolean, bool $yesNo = false) : string |
511
|
|
|
{ |
512
|
|
|
return ConvertHelper_Bool::toString($boolean, $yesNo); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Converts a strict boolean value to string. |
517
|
|
|
* Cannot throw an exception like {@see ConvertHelper::bool2string()} |
518
|
|
|
* does. |
519
|
|
|
* |
520
|
|
|
* @param bool $boolean |
521
|
|
|
* @param bool $yesNo |
522
|
|
|
* @return string |
523
|
|
|
*/ |
524
|
|
|
public static function boolStrict2string(bool $boolean, bool $yesNo = false) : string |
525
|
|
|
{ |
526
|
|
|
return ConvertHelper_Bool::toStringStrict($boolean, $yesNo); |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* Converts an associative array with attribute name > value pairs |
531
|
|
|
* to an attribute string that can be used in an HTML tag. Empty |
532
|
|
|
* attribute values are ignored. |
533
|
|
|
* |
534
|
|
|
* Example: |
535
|
|
|
* |
536
|
|
|
* array2attributeString(array( |
537
|
|
|
* 'id' => 45, |
538
|
|
|
* 'href' => 'http://www.mistralys.com' |
539
|
|
|
* )); |
540
|
|
|
* |
541
|
|
|
* Result: |
542
|
|
|
* |
543
|
|
|
* id="45" href="http://www.mistralys.com" |
544
|
|
|
* |
545
|
|
|
* @param array<string,mixed> $array |
546
|
|
|
* @return string |
547
|
|
|
*/ |
548
|
|
|
public static function array2attributeString(array $array) : string |
549
|
|
|
{ |
550
|
|
|
return ConvertHelper_Array::toAttributeString($array); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Converts a string, so it can safely be used in a javascript |
555
|
|
|
* statement in an HTML tag: uses single quotes around the string |
556
|
|
|
* and encodes all special characters as needed. |
557
|
|
|
* |
558
|
|
|
* @param string $string |
559
|
|
|
* @return string |
560
|
|
|
* @deprecated Use the JSHelper class instead. |
561
|
|
|
* @see JSHelper::phpVariable2AttributeJS() |
562
|
|
|
*/ |
563
|
|
|
public static function string2attributeJS(string $string) : string |
564
|
|
|
{ |
565
|
|
|
return JSHelper::phpVariable2AttributeJS($string); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Checks if the specified string is a boolean value, which |
570
|
|
|
* includes string representations of boolean values, like |
571
|
|
|
* <code>yes</code> or <code>no</code>, and <code>true</code> |
572
|
|
|
* or <code>false</code>. |
573
|
|
|
* |
574
|
|
|
* @param mixed $value |
575
|
|
|
* @return boolean |
576
|
|
|
*/ |
577
|
|
|
public static function isBoolean($value) : bool |
578
|
|
|
{ |
579
|
|
|
return ConvertHelper_Bool::isBoolean($value); |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Converts an associative array to an HTML style attribute value string. |
584
|
|
|
* |
585
|
|
|
* @param array<string,mixed> $subject |
586
|
|
|
* @return string |
587
|
|
|
*/ |
588
|
|
|
public static function array2styleString(array $subject) : string |
589
|
|
|
{ |
590
|
|
|
return ConvertHelper_Array::toStyleString($subject); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Converts a DateTime object to a timestamp, which |
595
|
|
|
* is PHP 5.2 compatible. |
596
|
|
|
* |
597
|
|
|
* @param DateTime $date |
598
|
|
|
* @return integer |
599
|
|
|
*/ |
600
|
|
|
public static function date2timestamp(DateTime $date) : int |
601
|
|
|
{ |
602
|
|
|
return ConvertHelper_Date::toTimestamp($date); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* Converts a timestamp into a DateTime instance. |
607
|
|
|
* @param int $timestamp |
608
|
|
|
* @return DateTime |
609
|
|
|
*/ |
610
|
|
|
public static function timestamp2date(int $timestamp) : DateTime |
611
|
|
|
{ |
612
|
|
|
return ConvertHelper_Date::fromTimestamp($timestamp); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Strips an absolute path to a file within the application |
617
|
|
|
* to make the path relative to the application root path. |
618
|
|
|
* |
619
|
|
|
* @param string $path |
620
|
|
|
* @return string |
621
|
|
|
* |
622
|
|
|
* @see FileHelper::relativizePath() |
623
|
|
|
* @see FileHelper::relativizePathByDepth() |
624
|
|
|
*/ |
625
|
|
|
public static function fileRelativize(string $path) : string |
626
|
|
|
{ |
627
|
|
|
return FileHelper::relativizePathByDepth($path); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* Converts a PHP regex to a javascript RegExp object statement. |
632
|
|
|
* |
633
|
|
|
* NOTE: This is an alias for the JSHelper's `convertRegex` method. |
634
|
|
|
* More details are available on its usage there. |
635
|
|
|
* |
636
|
|
|
* @param string $regex A PHP preg regex |
637
|
|
|
* @param string $statementType The type of statement to return: Defaults to a statement to create a RegExp object. |
638
|
|
|
* @return string Depending on the specified return type. |
639
|
|
|
* |
640
|
|
|
* @see JSHelper::buildRegexStatement() |
641
|
|
|
*/ |
642
|
|
|
public static function regex2js(string $regex, string $statementType=JSHelper::JS_REGEX_OBJECT) : string |
643
|
|
|
{ |
644
|
|
|
return JSHelper::buildRegexStatement($regex, $statementType); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Converts the specified variable to JSON. Works just |
649
|
|
|
* like the native `json_encode` method, except that it |
650
|
|
|
* will trigger an exception on failure, which has the |
651
|
|
|
* json error details included in its developer details. |
652
|
|
|
* |
653
|
|
|
* @param mixed $variable |
654
|
|
|
* @param int $options JSON encode options. |
655
|
|
|
* @param int $depth |
656
|
|
|
* @return string |
657
|
|
|
* |
658
|
|
|
* @throws ConvertHelper_Exception |
659
|
|
|
* @see ConvertHelper::ERROR_JSON_ENCODE_FAILED |
660
|
|
|
*/ |
661
|
|
|
public static function var2json($variable, int $options=0, int $depth=512) : string |
662
|
|
|
{ |
663
|
|
|
$result = json_encode($variable, $options, $depth); |
664
|
|
|
|
665
|
|
|
if($result !== false) { |
666
|
|
|
return $result; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
throw new ConvertHelper_Exception( |
670
|
|
|
'Could not create json array'.json_last_error_msg(), |
671
|
|
|
sprintf( |
672
|
|
|
'The call to json_encode failed for the variable [%s]. JSON error details: #%s, %s', |
673
|
|
|
parseVariable($variable)->toString(), |
674
|
|
|
json_last_error(), |
675
|
|
|
json_last_error_msg() |
676
|
|
|
), |
677
|
|
|
self::ERROR_JSON_ENCODE_FAILED |
678
|
|
|
); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Converts any PHP variable to a human-readable |
683
|
|
|
* string representation, like "object ClassName" |
684
|
|
|
* |
685
|
|
|
* @param mixed $variable |
686
|
|
|
* @return string |
687
|
|
|
*/ |
688
|
|
|
public function var2string($variable) : string |
689
|
|
|
{ |
690
|
|
|
return parseVariable($variable) |
691
|
|
|
->enableType() |
692
|
|
|
->toString(); |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
/** |
696
|
|
|
* Strips all known UTF byte order marks from the specified string. |
697
|
|
|
* |
698
|
|
|
* @param string $string |
699
|
|
|
* @return string |
700
|
|
|
*/ |
701
|
|
|
public static function stripUTFBom(string $string) : string |
702
|
|
|
{ |
703
|
|
|
$boms = FileHelper::createUnicodeHandling()->getUTFBOMs(); |
704
|
|
|
|
705
|
|
|
foreach($boms as $bomChars) |
706
|
|
|
{ |
707
|
|
|
$length = mb_strlen($bomChars); |
708
|
|
|
$text = mb_substr($string, 0, $length); |
709
|
|
|
|
710
|
|
|
if($text===$bomChars) |
711
|
|
|
{ |
712
|
|
|
return mb_substr($string, $length); |
713
|
|
|
} |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
return $string; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* Converts a string to valid utf8, regardless |
721
|
|
|
* of the string's encoding(s). |
722
|
|
|
* |
723
|
|
|
* @param string $string |
724
|
|
|
* @return string |
725
|
|
|
*/ |
726
|
|
|
public static function string2utf8(string $string) : string |
727
|
|
|
{ |
728
|
|
|
return ConvertHelper_String::toUtf8($string); |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
/** |
732
|
|
|
* Checks whether the specified string is an ASCII |
733
|
|
|
* string, without any special or UTF8 characters. |
734
|
|
|
* Note: empty strings and NULL are considered ASCII. |
735
|
|
|
* Any variable types other than strings are not. |
736
|
|
|
* |
737
|
|
|
* @param string|float|int|NULL $string |
738
|
|
|
* @return boolean |
739
|
|
|
*/ |
740
|
|
|
public static function isStringASCII($string) : bool |
741
|
|
|
{ |
742
|
|
|
return ConvertHelper_String::isASCII(strval($string)); |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
/** |
746
|
|
|
* Adds HTML syntax highlighting to an URL. |
747
|
|
|
* |
748
|
|
|
* NOTE: Includes the necessary CSS styles. When |
749
|
|
|
* highlighting several URLs in the same page, |
750
|
|
|
* prefer using the `parseURL` function instead. |
751
|
|
|
* |
752
|
|
|
* @param string $url |
753
|
|
|
* @return string |
754
|
|
|
* @deprecated Use the Highlighter class directly instead. |
755
|
|
|
* @see Highlighter |
756
|
|
|
*/ |
757
|
|
|
public static function highlight_url(string $url) : string |
758
|
|
|
{ |
759
|
|
|
return Highlighter::url($url); |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
/** |
763
|
|
|
* Calculates a percentage match of the source string with the target string. |
764
|
|
|
* |
765
|
|
|
* Options are: |
766
|
|
|
* |
767
|
|
|
* - maxLevenshtein, default: 10 |
768
|
|
|
* Any levenshtein results above this value are ignored. |
769
|
|
|
* |
770
|
|
|
* - precision, default: 1 |
771
|
|
|
* The precision of the percentage float value |
772
|
|
|
* |
773
|
|
|
* @param string $source |
774
|
|
|
* @param string $target |
775
|
|
|
* @param array<string,mixed> $options |
776
|
|
|
* @return float |
777
|
|
|
* |
778
|
|
|
* @see ConvertHelper_TextComparer |
779
|
|
|
* @see ConvertHelper_TextComparer::OPTION_MAX_LEVENSHTEIN_DISTANCE |
780
|
|
|
* @see ConvertHelper_TextComparer::OPTION_PRECISION |
781
|
|
|
*/ |
782
|
|
|
public static function matchString(string $source, string $target, array $options=array()) : float |
783
|
|
|
{ |
784
|
|
|
return (new ConvertHelper_TextComparer()) |
785
|
|
|
->setOptions($options) |
786
|
|
|
->match($source, $target); |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* Converts a date interval to a human-readable string with |
791
|
|
|
* all necessary time components, e.g. "1 year, 2 months and 4 days". |
792
|
|
|
* |
793
|
|
|
* @param DateInterval $interval |
794
|
|
|
* @return string |
795
|
|
|
* @see ConvertHelper_IntervalConverter |
796
|
|
|
* |
797
|
|
|
* @throws ConvertHelper_Exception |
798
|
|
|
* @see ConvertHelper_IntervalConverter::ERROR_MISSING_TRANSLATION |
799
|
|
|
*/ |
800
|
|
|
public static function interval2string(DateInterval $interval) : string |
801
|
|
|
{ |
802
|
|
|
return (new ConvertHelper_IntervalConverter()) |
803
|
|
|
->toString($interval); |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
/** |
807
|
|
|
* Converts an interval to its total amount of days. |
808
|
|
|
* @param DateInterval $interval |
809
|
|
|
* @return int |
810
|
|
|
*/ |
811
|
|
|
public static function interval2days(DateInterval $interval) : int |
812
|
|
|
{ |
813
|
|
|
return ConvertHelper_DateInterval::toDays($interval); |
814
|
|
|
} |
815
|
|
|
|
816
|
|
|
/** |
817
|
|
|
* Converts an interval to its total amount of hours. |
818
|
|
|
* @param DateInterval $interval |
819
|
|
|
* @return int |
820
|
|
|
*/ |
821
|
|
|
public static function interval2hours(DateInterval $interval) : int |
822
|
|
|
{ |
823
|
|
|
return ConvertHelper_DateInterval::toHours($interval); |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
/** |
827
|
|
|
* Converts an interval to its total amount of minutes. |
828
|
|
|
* @param DateInterval $interval |
829
|
|
|
* @return int |
830
|
|
|
*/ |
831
|
|
|
public static function interval2minutes(DateInterval $interval) : int |
832
|
|
|
{ |
833
|
|
|
return ConvertHelper_DateInterval::toMinutes($interval); |
834
|
|
|
} |
835
|
|
|
|
836
|
|
|
/** |
837
|
|
|
* Converts an interval to its total amount of seconds. |
838
|
|
|
* @param DateInterval $interval |
839
|
|
|
* @return int |
840
|
|
|
*/ |
841
|
|
|
public static function interval2seconds(DateInterval $interval) : int |
842
|
|
|
{ |
843
|
|
|
return ConvertHelper_DateInterval::toSeconds($interval); |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Calculates the total amount of days / hours / minutes or seconds |
848
|
|
|
* of a date interval object (depending on the specified units), and |
849
|
|
|
* returns the total amount. |
850
|
|
|
* |
851
|
|
|
* @param DateInterval $interval |
852
|
|
|
* @param string $unit What total value to calculate. |
853
|
|
|
* @return integer |
854
|
|
|
* |
855
|
|
|
* @see ConvertHelper::INTERVAL_SECONDS |
856
|
|
|
* @see ConvertHelper::INTERVAL_MINUTES |
857
|
|
|
* @see ConvertHelper::INTERVAL_HOURS |
858
|
|
|
* @see ConvertHelper::INTERVAL_DAYS |
859
|
|
|
*/ |
860
|
|
|
public static function interval2total(DateInterval $interval, string $unit=self::INTERVAL_SECONDS) : int |
861
|
|
|
{ |
862
|
|
|
return ConvertHelper_DateInterval::toTotal($interval, $unit); |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
/** |
866
|
|
|
* Converts a date to the corresponding day name. |
867
|
|
|
* |
868
|
|
|
* @param DateTime $date |
869
|
|
|
* @param bool $short |
870
|
|
|
* @return string|NULL |
871
|
|
|
*/ |
872
|
|
|
public static function date2dayName(DateTime $date, bool $short=false) : ?string |
873
|
|
|
{ |
874
|
|
|
return ConvertHelper_Date::toDayName($date, $short); |
875
|
|
|
} |
876
|
|
|
|
877
|
|
|
/** |
878
|
|
|
* Retrieves a list of english day names. |
879
|
|
|
* @return string[] |
880
|
|
|
*/ |
881
|
|
|
public static function getDayNamesInvariant() : array |
882
|
|
|
{ |
883
|
|
|
return ConvertHelper_Date::getDayNamesInvariant(); |
884
|
|
|
} |
885
|
|
|
|
886
|
|
|
/** |
887
|
|
|
* Retrieves the day names list for the current locale. |
888
|
|
|
* |
889
|
|
|
* @param bool $short |
890
|
|
|
* @return string[] |
891
|
|
|
*/ |
892
|
|
|
public static function getDayNames(bool $short=false) : array |
893
|
|
|
{ |
894
|
|
|
return ConvertHelper_Date::getDayNames($short); |
895
|
|
|
} |
896
|
|
|
|
897
|
|
|
/** |
898
|
|
|
* Implodes an array with a separator character, and the last item with "add". |
899
|
|
|
* |
900
|
|
|
* @param string[] $list The indexed array with items to implode. |
901
|
|
|
* @param string $sep The separator character to use. |
902
|
|
|
* @param string $conjunction The word to use as conjunction with the last item in the list. NOTE: include spaces as needed. |
903
|
|
|
* @return string |
904
|
|
|
*/ |
905
|
|
|
public static function implodeWithAnd(array $list, string $sep = ', ', string $conjunction = '') : string |
906
|
|
|
{ |
907
|
|
|
return ConvertHelper_Array::implodeWithAnd($list, $sep, $conjunction); |
908
|
|
|
} |
909
|
|
|
|
910
|
|
|
/** |
911
|
|
|
* Splits a string into an array of all characters it is composed of. |
912
|
|
|
* Unicode character safe. |
913
|
|
|
* |
914
|
|
|
* NOTE: Spaces and newlines (both \r and \n) are also considered single |
915
|
|
|
* characters. |
916
|
|
|
* |
917
|
|
|
* @param string $string |
918
|
|
|
* @return string[] |
919
|
|
|
*/ |
920
|
|
|
public static function string2array(string $string) : array |
921
|
|
|
{ |
922
|
|
|
return ConvertHelper_String::toArray($string); |
|
|
|
|
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
public static function string2words(string $string) : WordSplitter |
926
|
|
|
{ |
927
|
|
|
return new WordSplitter($string); |
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
/** |
931
|
|
|
* Checks whether the specified string contains HTML code. |
932
|
|
|
* |
933
|
|
|
* @param string $string |
934
|
|
|
* @return boolean |
935
|
|
|
*/ |
936
|
|
|
public static function isStringHTML(string $string) : bool |
937
|
|
|
{ |
938
|
|
|
return ConvertHelper_String::isHTML($string); |
939
|
|
|
} |
940
|
|
|
|
941
|
|
|
/** |
942
|
|
|
* UTF8-safe wordwrap method: works like the regular wordwrap |
943
|
|
|
* PHP function but compatible with UTF8. Otherwise the lengths |
944
|
|
|
* are not calculated correctly. |
945
|
|
|
* |
946
|
|
|
* @param string $str |
947
|
|
|
* @param int $width |
948
|
|
|
* @param string $break |
949
|
|
|
* @param bool $cut |
950
|
|
|
* @return string |
951
|
|
|
*/ |
952
|
|
|
public static function wordwrap(string $str, int $width = 75, string $break = "\n", bool $cut = false) : string |
953
|
|
|
{ |
954
|
|
|
return ConvertHelper_String::wordwrap($str, $width, $break, $cut); |
955
|
|
|
} |
956
|
|
|
|
957
|
|
|
/** |
958
|
|
|
* Calculates the byte length of a string, taking into |
959
|
|
|
* account any unicode characters. |
960
|
|
|
* |
961
|
|
|
* @param string $string |
962
|
|
|
* @return int |
963
|
|
|
*/ |
964
|
|
|
public static function string2bytes(string $string): int |
965
|
|
|
{ |
966
|
|
|
return ConvertHelper_String::toBytes($string); |
967
|
|
|
} |
968
|
|
|
|
969
|
|
|
/** |
970
|
|
|
* Creates a short, 8-character long hash for the specified string. |
971
|
|
|
* |
972
|
|
|
* WARNING: Not cryptographically safe. |
973
|
|
|
* |
974
|
|
|
* @param string $string |
975
|
|
|
* @return string |
976
|
|
|
*/ |
977
|
|
|
public static function string2shortHash(string $string) : string |
978
|
|
|
{ |
979
|
|
|
return ConvertHelper_String::toShortHash($string); |
980
|
|
|
} |
981
|
|
|
|
982
|
|
|
/** |
983
|
|
|
* Converts a string into an MD5 hash. |
984
|
|
|
* |
985
|
|
|
* @param string $string |
986
|
|
|
* @return string |
987
|
|
|
*/ |
988
|
|
|
public static function string2hash(string $string): string |
989
|
|
|
{ |
990
|
|
|
return ConvertHelper_String::toHash($string); |
991
|
|
|
} |
992
|
|
|
|
993
|
|
|
/** |
994
|
|
|
* Converts the specified callable to string. |
995
|
|
|
* |
996
|
|
|
* NOTE: Will work even if the callable is not |
997
|
|
|
* actually callable, as compared to |
998
|
|
|
* `parseVariable($callback)->toString()`. |
999
|
|
|
* |
1000
|
|
|
* @param callable $callback |
1001
|
|
|
* @return string |
1002
|
|
|
*/ |
1003
|
|
|
public static function callback2string($callback) : string |
1004
|
|
|
{ |
1005
|
|
|
// We are creating the renderer manually, to allow rendering |
1006
|
|
|
// callbacks to string even if they are not actually callable. |
1007
|
|
|
$renderer = new VariableInfo_Renderer_String_Callable(parseVariable($callback)); |
1008
|
|
|
|
1009
|
|
|
return $renderer->render(); |
1010
|
|
|
} |
1011
|
|
|
|
1012
|
|
|
public static function exception2info(\Throwable $e) : ConvertHelper_ThrowableInfo |
1013
|
|
|
{ |
1014
|
|
|
return self::throwable2info($e); |
1015
|
|
|
} |
1016
|
|
|
|
1017
|
|
|
public static function throwable2info(\Throwable $e) : ConvertHelper_ThrowableInfo |
1018
|
|
|
{ |
1019
|
|
|
return ConvertHelper_ThrowableInfo::fromThrowable($e); |
1020
|
|
|
} |
1021
|
|
|
|
1022
|
|
|
/** |
1023
|
|
|
* Parses the specified query string like the native |
1024
|
|
|
* function <code>parse_str</code>, without the key |
1025
|
|
|
* naming limitations. |
1026
|
|
|
* |
1027
|
|
|
* Using parse_str, dots or spaces in key names are |
1028
|
|
|
* replaced by underscores. This method keeps all names |
1029
|
|
|
* intact. |
1030
|
|
|
* |
1031
|
|
|
* It still uses the parse_str implementation as it |
1032
|
|
|
* is tested and tried, but fixes the parameter names |
1033
|
|
|
* after parsing, as needed. |
1034
|
|
|
* |
1035
|
|
|
* @param string $queryString |
1036
|
|
|
* @return array<string,string> |
1037
|
|
|
* @see ConvertHelper_QueryParser |
1038
|
|
|
*/ |
1039
|
|
|
public static function parseQueryString(string $queryString) : array |
1040
|
|
|
{ |
1041
|
|
|
$parser = new ConvertHelper_QueryParser(); |
1042
|
|
|
return $parser->parse($queryString); |
1043
|
|
|
} |
1044
|
|
|
|
1045
|
|
|
/** |
1046
|
|
|
* Searches for needle in the specified string, and returns a list |
1047
|
|
|
* of all occurrences, including the matched string. The matched |
1048
|
|
|
* string is useful when doing a case-insensitive search, as it |
1049
|
|
|
* shows the exact matched case of needle. |
1050
|
|
|
* |
1051
|
|
|
* @param string $needle |
1052
|
|
|
* @param string $haystack |
1053
|
|
|
* @param bool $caseInsensitive |
1054
|
|
|
* @return ConvertHelper_StringMatch[] |
1055
|
|
|
*/ |
1056
|
|
|
public static function findString(string $needle, string $haystack, bool $caseInsensitive=false): array |
1057
|
|
|
{ |
1058
|
|
|
return ConvertHelper_String::findString($needle, $haystack, $caseInsensitive); |
1059
|
|
|
} |
1060
|
|
|
|
1061
|
|
|
/** |
1062
|
|
|
* Like explode, but trims all entries, and removes |
1063
|
|
|
* empty entries from the resulting array. |
1064
|
|
|
* |
1065
|
|
|
* @param string $delimiter |
1066
|
|
|
* @param string $string |
1067
|
|
|
* @return string[] |
1068
|
|
|
*/ |
1069
|
|
|
public static function explodeTrim(string $delimiter, string $string) : array |
1070
|
|
|
{ |
1071
|
|
|
return ConvertHelper_String::explodeTrim($delimiter, $string); |
1072
|
|
|
} |
1073
|
|
|
|
1074
|
|
|
/** |
1075
|
|
|
* Detects the most used end-of-line character in the subject string. |
1076
|
|
|
* |
1077
|
|
|
* @param string $subjectString The string to check. |
1078
|
|
|
* @return NULL|ConvertHelper_EOL The detected EOL instance, or NULL if none has been detected. |
1079
|
|
|
*/ |
1080
|
|
|
public static function detectEOLCharacter(string $subjectString) : ?ConvertHelper_EOL |
1081
|
|
|
{ |
1082
|
|
|
return ConvertHelper_EOL::detect($subjectString); |
1083
|
|
|
} |
1084
|
|
|
|
1085
|
|
|
/** |
1086
|
|
|
* Removes the specified keys from the target array, |
1087
|
|
|
* if they exist. |
1088
|
|
|
* |
1089
|
|
|
* @param array<string|int,mixed> $array |
1090
|
|
|
* @param string[] $keys |
1091
|
|
|
*/ |
1092
|
|
|
public static function arrayRemoveKeys(array &$array, array $keys) : void |
1093
|
|
|
{ |
1094
|
|
|
ConvertHelper_Array::removeKeys($array, $keys); |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
|
|
/** |
1098
|
|
|
* Checks if the specified variable is an integer or a string containing an integer. |
1099
|
|
|
* Accepts both positive and negative integers. |
1100
|
|
|
* |
1101
|
|
|
* @param mixed $value |
1102
|
|
|
* @return bool |
1103
|
|
|
*/ |
1104
|
|
|
public static function isInteger($value) : bool |
1105
|
|
|
{ |
1106
|
|
|
if(is_int($value)) { |
1107
|
|
|
return true; |
1108
|
|
|
} |
1109
|
|
|
|
1110
|
|
|
// booleans get converted to numbers, so they would |
1111
|
|
|
// actually match the regex. |
1112
|
|
|
if(is_bool($value)) { |
1113
|
|
|
return false; |
1114
|
|
|
} |
1115
|
|
|
|
1116
|
|
|
if(is_string($value) && $value !== '') { |
1117
|
|
|
return preg_match('/\A-?\d+\z/', $value) === 1; |
1118
|
|
|
} |
1119
|
|
|
|
1120
|
|
|
return false; |
1121
|
|
|
} |
1122
|
|
|
|
1123
|
|
|
/** |
1124
|
|
|
* Converts an amount of seconds to a DateInterval object. |
1125
|
|
|
* |
1126
|
|
|
* @param int $seconds |
1127
|
|
|
* @return DateInterval |
1128
|
|
|
* @throws ConvertHelper_Exception If the date interval cannot be created. |
1129
|
|
|
* |
1130
|
|
|
* @see ConvertHelper::ERROR_CANNOT_GET_DATE_DIFF |
1131
|
|
|
*/ |
1132
|
|
|
public static function seconds2interval(int $seconds) : DateInterval |
1133
|
|
|
{ |
1134
|
|
|
return ConvertHelper_DateInterval::fromSeconds($seconds)->getInterval(); |
1135
|
|
|
} |
1136
|
|
|
|
1137
|
|
|
/** |
1138
|
|
|
* Converts a size string like "50 MB" to the corresponding byte size. |
1139
|
|
|
* It is case-insensitive, ignores spaces, and supports both traditional |
1140
|
|
|
* "MB" and "MiB" notations. |
1141
|
|
|
* |
1142
|
|
|
* @param string $size |
1143
|
|
|
* @return int |
1144
|
|
|
*/ |
1145
|
|
|
public static function size2bytes(string $size) : int |
1146
|
|
|
{ |
1147
|
|
|
return self::parseSize($size)->toBytes(); |
1148
|
|
|
} |
1149
|
|
|
|
1150
|
|
|
/** |
1151
|
|
|
* Parses a size string like "50 MB" and returns a size notation instance |
1152
|
|
|
* that has utility methods to access information on it, and convert it. |
1153
|
|
|
* |
1154
|
|
|
* @param string $size |
1155
|
|
|
* @return ConvertHelper_SizeNotation |
1156
|
|
|
*/ |
1157
|
|
|
public static function parseSize(string $size) : ConvertHelper_SizeNotation |
1158
|
|
|
{ |
1159
|
|
|
return new ConvertHelper_SizeNotation($size); |
1160
|
|
|
} |
1161
|
|
|
|
1162
|
|
|
/** |
1163
|
|
|
* Creates a URL finder instance, which can be used to find |
1164
|
|
|
* URLs in a string - be it plain text, or HTML. |
1165
|
|
|
* |
1166
|
|
|
* @param string $subject |
1167
|
|
|
* @return ConvertHelper_URLFinder |
1168
|
|
|
*/ |
1169
|
|
|
public static function createURLFinder(string $subject) : ConvertHelper_URLFinder |
1170
|
|
|
{ |
1171
|
|
|
return new ConvertHelper_URLFinder($subject); |
1172
|
|
|
} |
1173
|
|
|
|
1174
|
|
|
/** |
1175
|
|
|
* Removes the target values from the source array. |
1176
|
|
|
* |
1177
|
|
|
* @param array<number|string,mixed> $sourceArray |
1178
|
|
|
* @param array<number|string,mixed> $values |
1179
|
|
|
* @param bool $keepKeys |
1180
|
|
|
* @return array<number|string,mixed> |
1181
|
|
|
*/ |
1182
|
|
|
public static function arrayRemoveValues(array $sourceArray, array $values, bool $keepKeys=false) : array |
1183
|
|
|
{ |
1184
|
|
|
return ConvertHelper_Array::removeValues($sourceArray, $values, $keepKeys); |
1185
|
|
|
} |
1186
|
|
|
} |
1187
|
|
|
|