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