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