| Total Complexity | 199 |
| Total Lines | 1580 |
| Duplicated Lines | 0 % |
| Changes | 19 | ||
| Bugs | 1 | 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 |
||
| 21 | class ConvertHelper |
||
| 22 | { |
||
| 23 | const ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER = 23303; |
||
| 24 | |||
| 25 | const ERROR_CANNOT_NORMALIZE_NON_SCALAR_VALUE = 23304; |
||
| 26 | |||
| 27 | const ERROR_JSON_ENCODE_FAILED = 23305; |
||
| 28 | |||
| 29 | const ERROR_CANNOT_GET_DATE_DIFF = 23306; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Normalizes tabs in the specified string by indenting everything |
||
| 33 | * back to the minimum tab distance. With the second parameter, |
||
| 34 | * tabs can optionally be converted to spaces as well (recommended |
||
| 35 | * for HTML output). |
||
| 36 | * |
||
| 37 | * @param string $string |
||
| 38 | * @param boolean $tabs2spaces |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | public static function normalizeTabs(string $string, bool $tabs2spaces = false) |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Converts tabs to spaces in the specified string. |
||
| 78 | * @param string $string |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public static function tabs2spaces($string) |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Converts the specified amount of seconds into |
||
| 88 | * a human readable string split in months, weeks, |
||
| 89 | * days, hours, minutes and seconds. |
||
| 90 | * |
||
| 91 | * @param float $seconds |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public static function time2string($seconds) |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Converts a timestamp into an easily understandable |
||
| 167 | * format, e.g. "2 hours", "1 day", "3 months" |
||
| 168 | * |
||
| 169 | * If you set the date to parameter, the difference |
||
| 170 | * will be calculated between the two dates and not |
||
| 171 | * the current time. |
||
| 172 | * |
||
| 173 | * @param integer|\DateTime $datefrom |
||
| 174 | * @param integer|\DateTime $dateto |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public static function duration2string($datefrom, $dateto = -1) : string |
||
| 178 | { |
||
| 179 | $converter = new ConvertHelper_DurationConverter(); |
||
| 180 | |||
| 181 | if($datefrom instanceof \DateTime) |
||
| 182 | { |
||
| 183 | $converter->setDateFrom($datefrom); |
||
| 184 | } |
||
| 185 | else |
||
| 186 | { |
||
| 187 | $converter->setDateFrom(self::timestamp2date($datefrom)); |
||
| 188 | } |
||
| 189 | |||
| 190 | if($dateto instanceof \DateTime) |
||
| 191 | { |
||
| 192 | $converter->setDateTo($dateto); |
||
| 193 | } |
||
| 194 | else if($dateto > 0) |
||
| 195 | { |
||
| 196 | $converter->setDateTo(self::timestamp2date($dateto)); |
||
| 197 | } |
||
| 198 | |||
| 199 | return $converter->convert(); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Adds syntax highlighting to the specified SQL string in HTML format |
||
| 204 | * @param string $sql |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public static function highlight_sql($sql) |
||
| 208 | { |
||
| 209 | $geshi = new \GeSHi($sql, 'sql'); |
||
| 210 | |||
| 211 | return $geshi->parse_code(); |
||
| 212 | } |
||
| 213 | |||
| 214 | public static function highlight_xml($xml, $formatSource=false) |
||
| 215 | { |
||
| 216 | if($formatSource) |
||
| 217 | { |
||
| 218 | $dom = new \DOMDocument(); |
||
| 219 | $dom->loadXML($xml); |
||
| 220 | $dom->preserveWhiteSpace = false; |
||
| 221 | $dom->formatOutput = true; |
||
| 222 | |||
| 223 | $xml = $dom->saveXML(); |
||
| 224 | } |
||
| 225 | |||
| 226 | $geshi = new \GeSHi($xml, 'xml'); |
||
| 227 | |||
| 228 | return $geshi->parse_code(); |
||
| 229 | } |
||
| 230 | |||
| 231 | public static function highlight_php($php) |
||
| 232 | { |
||
| 233 | $geshi = new \GeSHi($php, 'php'); |
||
| 234 | |||
| 235 | return $geshi->parse_code(); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Converts a number of bytes to a human readable form, |
||
| 240 | * e.g. xx Kb / xx Mb / xx Gb |
||
| 241 | * |
||
| 242 | * @param $bytes |
||
| 243 | * @param $precision |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public static function bytes2readable($bytes, $precision = 1) |
||
| 247 | { |
||
| 248 | $kilobyte = 1024; |
||
| 249 | $megabyte = $kilobyte * 1024; |
||
| 250 | $gigabyte = $megabyte * 1024; |
||
| 251 | $terabyte = $gigabyte * 1024; |
||
| 252 | |||
| 253 | if (($bytes >= 0) && ($bytes < $kilobyte)) { |
||
| 254 | return $bytes . ' ' . t('B'); |
||
| 255 | |||
| 256 | } elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) { |
||
| 257 | return round($bytes / $kilobyte, $precision) . ' ' . t('Kb'); |
||
| 258 | |||
| 259 | } elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) { |
||
| 260 | return round($bytes / $megabyte, $precision) . ' ' . t('Mb'); |
||
| 261 | |||
| 262 | } elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) { |
||
| 263 | return round($bytes / $gigabyte, $precision) . ' ' . t('Gb'); |
||
| 264 | |||
| 265 | } elseif ($bytes >= $terabyte) { |
||
| 266 | return round($bytes / $gigabyte, $precision) . ' ' . t('Tb'); |
||
| 267 | } |
||
| 268 | |||
| 269 | return $bytes . ' ' . t('B'); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Cuts a text to the specified length if it is longer than the |
||
| 274 | * target length. Appends a text to signify it has been cut at |
||
| 275 | * the end of the string. |
||
| 276 | * |
||
| 277 | * @param string $text |
||
| 278 | * @param int $targetLength |
||
| 279 | * @param string $append |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public static function text_cut(string $text, int $targetLength, string $append = '...') : string |
||
| 283 | { |
||
| 284 | $length = mb_strlen($text); |
||
| 285 | if ($length <= $targetLength) { |
||
| 286 | return $text; |
||
| 287 | } |
||
| 288 | |||
| 289 | $text = trim(mb_substr($text, 0, $targetLength)) . $append; |
||
| 290 | |||
| 291 | return $text; |
||
| 292 | } |
||
| 293 | |||
| 294 | public static function var_dump($var, $html=true) |
||
| 295 | { |
||
| 296 | $info = parseVariable($var); |
||
| 297 | |||
| 298 | if($html) { |
||
| 299 | return $info->toHTML(); |
||
| 300 | } |
||
| 301 | |||
| 302 | return $info->toString(); |
||
| 303 | } |
||
| 304 | |||
| 305 | public static function print_r($var, $return=false, $html=true) |
||
| 306 | { |
||
| 307 | $result = self::var_dump($var, $html); |
||
| 308 | |||
| 309 | if($html) { |
||
| 310 | $result = |
||
| 311 | '<pre style="background:#fff;color:#333;padding:16px;border:solid 1px #bbb;border-radius:4px">'. |
||
| 312 | $result. |
||
| 313 | '</pre>'; |
||
| 314 | } |
||
| 315 | |||
| 316 | if($return) { |
||
| 317 | return $result; |
||
| 318 | } |
||
| 319 | |||
| 320 | echo $result; |
||
| 321 | } |
||
| 322 | |||
| 323 | protected static $booleanStrings = array( |
||
| 324 | 1 => true, |
||
| 325 | 0 => false, |
||
| 326 | '1' => true, |
||
| 327 | '0' => false, |
||
| 328 | 'true' => true, |
||
| 329 | 'false' => false, |
||
| 330 | 'yes' => true, |
||
| 331 | 'no' => false |
||
| 332 | ); |
||
| 333 | |||
| 334 | public static function string2bool($string) |
||
| 335 | { |
||
| 336 | if($string === '' || $string === null) { |
||
| 337 | return false; |
||
| 338 | } |
||
| 339 | |||
| 340 | if (is_bool($string)) { |
||
| 341 | return $string; |
||
| 342 | } |
||
| 343 | |||
| 344 | if (!array_key_exists($string, self::$booleanStrings)) { |
||
| 345 | throw new \InvalidArgumentException('Invalid string boolean representation'); |
||
| 346 | } |
||
| 347 | |||
| 348 | return self::$booleanStrings[$string]; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Whether the specified string is a boolean string or boolean value. |
||
| 353 | * Alias for {@link ConvertHelper::isBoolean()}. |
||
| 354 | * |
||
| 355 | * @param mixed $string |
||
| 356 | * @return bool |
||
| 357 | * @deprecated |
||
| 358 | * @see ConvertHelper::isBoolean() |
||
| 359 | */ |
||
| 360 | public static function isBooleanString($string) : bool |
||
| 361 | { |
||
| 362 | return self::isBoolean($string); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Alias for the {@\AppUtils\XMLHelper::string2xml()} method. |
||
| 367 | * |
||
| 368 | * @param string $text |
||
| 369 | * @return string |
||
| 370 | * @deprecated |
||
| 371 | */ |
||
| 372 | public static function text_makeXMLCompliant($text) |
||
| 373 | { |
||
| 374 | return XMLHelper::string2xml($text); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Transforms a date into a generic human readable date, optionally with time. |
||
| 379 | * If the year is the same as the current one, it is omitted. |
||
| 380 | * |
||
| 381 | * - 6 Jan 2012 |
||
| 382 | * - 12 Dec 2012 17:45 |
||
| 383 | * - 5 Aug |
||
| 384 | * |
||
| 385 | * @param \DateTime $date |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public static function date2listLabel(\DateTime $date, $includeTime = false, $shortMonth = false) |
||
| 389 | { |
||
| 390 | $today = new \DateTime(); |
||
| 391 | if($date->format('d.m.Y') == $today->format('d.m.Y')) { |
||
| 392 | $label = t('Today'); |
||
| 393 | } else { |
||
| 394 | $label = $date->format('d') . '. ' . self::month2string((int)$date->format('m'), $shortMonth) . ' '; |
||
| 395 | if ($date->format('Y') != date('Y')) { |
||
| 396 | $label .= $date->format('Y'); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | if ($includeTime) { |
||
| 401 | $label .= $date->format(' H:i'); |
||
| 402 | } |
||
| 403 | |||
| 404 | return trim($label); |
||
| 405 | } |
||
| 406 | |||
| 407 | protected static $months; |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Returns a human readable month name given the month number. Can optionally |
||
| 411 | * return the shorthand version of the month. Translated into the current |
||
| 412 | * application locale. |
||
| 413 | * |
||
| 414 | * @param int|string $monthNr |
||
| 415 | * @param boolean $short |
||
| 416 | * @throws ConvertHelper_Exception |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public static function month2string($monthNr, $short = false) |
||
| 420 | { |
||
| 421 | if (!isset(self::$months)) { |
||
| 422 | self::$months = array( |
||
| 423 | 1 => array(t('January'), t('Jan')), |
||
| 424 | 2 => array(t('February'), t('Feb')), |
||
| 425 | 3 => array(t('March'), t('Mar')), |
||
| 426 | 4 => array(t('April'), t('Apr')), |
||
| 427 | 5 => array(t('May'), t('May')), |
||
| 428 | 6 => array(t('June'), t('Jun')), |
||
| 429 | 7 => array(t('July'), t('Jul')), |
||
| 430 | 8 => array(t('August'), t('Aug')), |
||
| 431 | 9 => array(t('September'), t('Sep')), |
||
| 432 | 10 => array(t('October'), t('Oct')), |
||
| 433 | 11 => array(t('November'), t('Nov')), |
||
| 434 | 12 => array(t('December'), t('Dec')) |
||
| 435 | ); |
||
| 436 | } |
||
| 437 | |||
| 438 | $monthNr = intval($monthNr); |
||
| 439 | if (!isset(self::$months[$monthNr])) { |
||
| 440 | throw new ConvertHelper_Exception( |
||
| 441 | 'Invalid month number', |
||
| 442 | sprintf('%1$s is not a valid month number.', $monthNr), |
||
| 443 | self::ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER |
||
| 444 | ); |
||
| 445 | } |
||
| 446 | |||
| 447 | if ($short) { |
||
| 448 | return self::$months[$monthNr][1]; |
||
| 449 | } |
||
| 450 | |||
| 451 | return self::$months[$monthNr][0]; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Transliterates a string. |
||
| 456 | * |
||
| 457 | * @param string $string |
||
| 458 | * @param string $spaceChar |
||
| 459 | * @param string $lowercase |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public static function transliterate($string, $spaceChar = '-', $lowercase = true) |
||
| 463 | { |
||
| 464 | $translit = new Transliteration(); |
||
| 465 | $translit->setSpaceReplacement($spaceChar); |
||
| 466 | if ($lowercase) { |
||
| 467 | $translit->setLowercase(); |
||
| 468 | } |
||
| 469 | |||
| 470 | return $translit->convert($string); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Retrieves the HEX character codes for all control |
||
| 475 | * characters that the {@link stripControlCharacters()} |
||
| 476 | * method will remove. |
||
| 477 | * |
||
| 478 | * @return string[] |
||
| 479 | */ |
||
| 480 | public static function getControlCharactersAsHex() |
||
| 481 | { |
||
| 482 | $hexAlphabet = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'); |
||
| 483 | |||
| 484 | $stack = array(); |
||
| 485 | foreach(self::$controlChars as $char) |
||
| 486 | { |
||
| 487 | $tokens = explode('-', $char); |
||
| 488 | $start = $tokens[0]; |
||
| 489 | $end = $tokens[1]; |
||
| 490 | $prefix = substr($start, 0, 3); |
||
| 491 | $range = array(); |
||
| 492 | foreach($hexAlphabet as $number) { |
||
| 493 | $range[] = $prefix.$number; |
||
| 494 | } |
||
| 495 | |||
| 496 | $use = false; |
||
| 497 | foreach($range as $number) { |
||
| 498 | if($number == $start) { |
||
| 499 | $use = true; |
||
| 500 | } |
||
| 501 | |||
| 502 | if($use) { |
||
| 503 | $stack[] = $number; |
||
| 504 | } |
||
| 505 | |||
| 506 | if($number == $end) { |
||
| 507 | break; |
||
| 508 | } |
||
| 509 | } |
||
| 510 | } |
||
| 511 | |||
| 512 | return $stack; |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Retrieves an array of all control characters that |
||
| 517 | * the {@link stripControlCharacters()} method will |
||
| 518 | * remove, as the actual UTF-8 characters. |
||
| 519 | * |
||
| 520 | * @return string[] |
||
| 521 | */ |
||
| 522 | public static function getControlCharactersAsUTF8() |
||
| 523 | { |
||
| 524 | $chars = self::getControlCharactersAsHex(); |
||
| 525 | |||
| 526 | $result = array(); |
||
| 527 | foreach($chars as $char) { |
||
| 528 | $result[] = hex2bin($char); |
||
| 529 | } |
||
| 530 | |||
| 531 | return $result; |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Retrieves all control characters as JSON encoded |
||
| 536 | * characters, e.g. "\u200b". |
||
| 537 | * |
||
| 538 | * @return string[] |
||
| 539 | */ |
||
| 540 | public static function getControlCharactersAsJSON() |
||
| 541 | { |
||
| 542 | $chars = self::getControlCharactersAsHex(); |
||
| 543 | |||
| 544 | $result = array(); |
||
| 545 | foreach($chars as $char) { |
||
| 546 | $result[] = '\u'.strtolower($char); |
||
| 547 | } |
||
| 548 | |||
| 549 | return $result; |
||
| 550 | } |
||
| 551 | |||
| 552 | protected static $controlChars = array( |
||
| 553 | '0000-0008', // control chars |
||
| 554 | '000E-000F', // control chars |
||
| 555 | '0010-001F', // control chars |
||
| 556 | '2000-200F', // non-breaking space and co |
||
| 557 | ); |
||
| 558 | |||
| 559 | protected static $controlCharsRegex; |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Removes all control characters from the specified string |
||
| 563 | * that can cause problems in some cases, like creating |
||
| 564 | * valid XML documents. This includes invisible non-breaking |
||
| 565 | * spaces. |
||
| 566 | * |
||
| 567 | * @param string $string |
||
| 568 | * @return string |
||
| 569 | * @see https://stackoverflow.com/a/8171868/2298192 |
||
| 570 | * @see https://unicode-table.com/en |
||
| 571 | */ |
||
| 572 | public static function stripControlCharacters(string $string) : string |
||
| 573 | { |
||
| 574 | if(empty($string)) { |
||
| 575 | return $string; |
||
| 576 | } |
||
| 577 | |||
| 578 | // create the regex from the unicode characters list |
||
| 579 | if(!isset(self::$controlCharsRegex)) |
||
| 580 | { |
||
| 581 | $chars = self::getControlCharactersAsHex(); |
||
| 582 | |||
| 583 | // we use the notation \x{0000} to specify the unicode character key |
||
| 584 | // in the regular expression. |
||
| 585 | $stack = array(); |
||
| 586 | foreach($chars as $char) { |
||
| 587 | $stack[] = '\x{'.$char.'}'; |
||
| 588 | } |
||
| 589 | |||
| 590 | self::$controlCharsRegex = '/['.implode('', $stack).']/u'; |
||
| 591 | } |
||
| 592 | |||
| 593 | return preg_replace(self::$controlCharsRegex, '', $string); |
||
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Converts a unicode character to the PHPO notation. |
||
| 598 | * |
||
| 599 | * Example: |
||
| 600 | * |
||
| 601 | * <pre>unicodeChar2php('"\u0000"')</pre> |
||
| 602 | * |
||
| 603 | * Returns |
||
| 604 | * |
||
| 605 | * <pre>\x0</pre> |
||
| 606 | * |
||
| 607 | * @param string $unicodeChar |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | public static function unicodeChar2php($unicodeChar) |
||
| 611 | { |
||
| 612 | $unicodeChar = json_decode($unicodeChar); |
||
| 613 | |||
| 614 | /** @author Krinkle 2018 */ |
||
| 615 | $output = ''; |
||
| 616 | foreach (str_split($unicodeChar) as $octet) { |
||
| 617 | $ordInt = ord($octet); |
||
| 618 | // Convert from int (base 10) to hex (base 16), for PHP \x syntax |
||
| 619 | $ordHex = base_convert($ordInt, 10, 16); |
||
| 620 | $output .= '\x' . $ordHex; |
||
| 621 | } |
||
| 622 | return $output; |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Removes the extension from the specified filename |
||
| 627 | * and returns the name without the extension. |
||
| 628 | * |
||
| 629 | * Example: |
||
| 630 | * filename.html > filename |
||
| 631 | * passed.test.jpg > passed.test |
||
| 632 | * path/to/file/document.txt > document |
||
| 633 | * |
||
| 634 | * @param string $filename |
||
| 635 | * @return string |
||
| 636 | */ |
||
| 637 | public static function filenameRemoveExtension($filename) |
||
| 640 | } |
||
| 641 | |||
| 642 | public static function areVariablesEqual($a, $b) : bool |
||
| 643 | { |
||
| 644 | $a = self::convertScalarForComparison($a); |
||
| 645 | $b = self::convertScalarForComparison($b); |
||
| 646 | |||
| 647 | return $a === $b; |
||
| 648 | } |
||
| 649 | |||
| 650 | protected static function convertScalarForComparison($scalar) |
||
| 651 | { |
||
| 652 | if($scalar === '' || is_null($scalar)) { |
||
| 653 | return null; |
||
| 654 | } |
||
| 655 | |||
| 656 | if(is_bool($scalar)) { |
||
| 657 | return self::bool2string($scalar); |
||
| 658 | } |
||
| 659 | |||
| 660 | if(is_array($scalar)) { |
||
| 661 | $scalar = md5(serialize($scalar)); |
||
| 662 | } |
||
| 663 | |||
| 664 | if($scalar !== null && !is_scalar($scalar)) { |
||
| 665 | throw new ConvertHelper_Exception( |
||
| 666 | 'Not a scalar value in comparison', |
||
| 667 | null, |
||
| 668 | self::ERROR_CANNOT_NORMALIZE_NON_SCALAR_VALUE |
||
| 669 | ); |
||
| 670 | } |
||
| 671 | |||
| 672 | return strval($scalar); |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Compares two strings to check whether they are equal. |
||
| 677 | * null and empty strings are considered equal. |
||
| 678 | * |
||
| 679 | * @param string $a |
||
| 680 | * @param string $b |
||
| 681 | * @return boolean |
||
| 682 | */ |
||
| 683 | public static function areStringsEqual($a, $b) : bool |
||
| 684 | { |
||
| 685 | return self::areVariablesEqual($a, $b); |
||
| 686 | } |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Checks whether the two specified numbers are equal. |
||
| 690 | * null and empty strings are considered as 0 values. |
||
| 691 | * |
||
| 692 | * @param number|string $a |
||
| 693 | * @param number|string $b |
||
| 694 | * @return boolean |
||
| 695 | */ |
||
| 696 | public static function areNumbersEqual($a, $b) : bool |
||
| 697 | { |
||
| 698 | return self::areVariablesEqual($a, $b); |
||
| 699 | } |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Converts a boolean value to a string. Defaults to returning |
||
| 703 | * 'true' or 'false', with the additional parameter it can also |
||
| 704 | * return the 'yes' and 'no' variants. |
||
| 705 | * |
||
| 706 | * @param boolean|string $boolean |
||
| 707 | * @param boolean $yesno |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | public static function bool2string($boolean, bool $yesno = false) : string |
||
| 711 | { |
||
| 712 | // allow 'yes', 'true', 'no', 'false' string notations as well |
||
| 713 | if(!is_bool($boolean)) { |
||
| 714 | $boolean = self::string2bool($boolean); |
||
| 715 | } |
||
| 716 | |||
| 717 | if ($boolean) { |
||
| 718 | if ($yesno) { |
||
| 719 | return 'yes'; |
||
| 720 | } |
||
| 721 | |||
| 722 | return 'true'; |
||
| 723 | } |
||
| 724 | |||
| 725 | if ($yesno) { |
||
| 726 | return 'no'; |
||
| 727 | } |
||
| 728 | |||
| 729 | return 'false'; |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Converts an associative array with attribute name > value pairs |
||
| 734 | * to an attribute string that can be used in an HTML tag. Empty |
||
| 735 | * attribute values are ignored. |
||
| 736 | * |
||
| 737 | * Example: |
||
| 738 | * |
||
| 739 | * array2attributeString(array( |
||
| 740 | * 'id' => 45, |
||
| 741 | * 'href' => 'http://www.mistralys.com' |
||
| 742 | * )); |
||
| 743 | * |
||
| 744 | * Result: |
||
| 745 | * |
||
| 746 | * id="45" href="http://www.mistralys.com" |
||
| 747 | * |
||
| 748 | * @param array $array |
||
| 749 | * @return string |
||
| 750 | */ |
||
| 751 | public static function array2attributeString($array) |
||
| 752 | { |
||
| 753 | $tokens = array(); |
||
| 754 | foreach($array as $attr => $value) { |
||
| 755 | if($value == '' || $value == null) { |
||
| 756 | continue; |
||
| 757 | } |
||
| 758 | |||
| 759 | $tokens[] = $attr.'="'.$value.'"'; |
||
| 760 | } |
||
| 761 | |||
| 762 | if(empty($tokens)) { |
||
| 763 | return ''; |
||
| 764 | } |
||
| 765 | |||
| 766 | return ' '.implode(' ', $tokens); |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Converts a string so it can safely be used in a javascript |
||
| 771 | * statement in an HTML tag: uses single quotes around the string |
||
| 772 | * and encodes all special characters as needed. |
||
| 773 | * |
||
| 774 | * @param string $string |
||
| 775 | * @return string |
||
| 776 | */ |
||
| 777 | public static function string2attributeJS($string, $quoted=true) |
||
| 778 | { |
||
| 779 | $converted = addslashes(htmlspecialchars(strip_tags($string), ENT_QUOTES, 'UTF-8')); |
||
| 780 | if($quoted) { |
||
| 781 | $converted = "'".$converted."'"; |
||
| 782 | } |
||
| 783 | |||
| 784 | return $converted; |
||
| 785 | } |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Checks if the specified string is a boolean value, which |
||
| 789 | * includes string representations of boolean values, like |
||
| 790 | * <code>yes</code> or <code>no</code>, and <code>true</code> |
||
| 791 | * or <code>false</code>. |
||
| 792 | * |
||
| 793 | * @param mixed $value |
||
| 794 | * @return boolean |
||
| 795 | */ |
||
| 796 | public static function isBoolean($value) : bool |
||
| 797 | { |
||
| 798 | if(is_bool($value)) { |
||
| 799 | return true; |
||
| 800 | } |
||
| 801 | |||
| 802 | if(!is_scalar($value)) { |
||
| 803 | return false; |
||
| 804 | } |
||
| 805 | |||
| 806 | return array_key_exists($value, self::$booleanStrings); |
||
| 807 | } |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Converts an associative array to an HTML style attribute value string. |
||
| 811 | * |
||
| 812 | * @param array $subject |
||
| 813 | * @return string |
||
| 814 | */ |
||
| 815 | public static function array2styleString(array $subject) : string |
||
| 816 | { |
||
| 817 | $tokens = array(); |
||
| 818 | foreach($subject as $name => $value) { |
||
| 819 | $tokens[] = $name.':'.$value; |
||
| 820 | } |
||
| 821 | |||
| 822 | return implode(';', $tokens); |
||
| 823 | } |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Converts a DateTime object to a timestamp, which |
||
| 827 | * is PHP 5.2 compatible. |
||
| 828 | * |
||
| 829 | * @param \DateTime $date |
||
| 830 | * @return integer |
||
| 831 | */ |
||
| 832 | public static function date2timestamp(\DateTime $date) : int |
||
| 833 | { |
||
| 834 | return (int)$date->format('U'); |
||
| 835 | } |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Converts a timestamp into a DateTime instance. |
||
| 839 | * @param int $timestamp |
||
| 840 | * @return \DateTime |
||
| 841 | */ |
||
| 842 | public static function timestamp2date(int $timestamp) : \DateTime |
||
| 843 | { |
||
| 844 | $date = new \DateTime(); |
||
| 845 | $date->setTimestamp($timestamp); |
||
| 846 | return $date; |
||
| 847 | } |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Strips an absolute path to a file within the application |
||
| 851 | * to make the path relative to the application root path. |
||
| 852 | * |
||
| 853 | * @param string $path |
||
| 854 | * @return string |
||
| 855 | * |
||
| 856 | * @see FileHelper::relativizePath() |
||
| 857 | * @see FileHelper::relativizePathByDepth() |
||
| 858 | */ |
||
| 859 | public static function fileRelativize(string $path) : string |
||
| 860 | { |
||
| 861 | return FileHelper::relativizePathByDepth($path); |
||
| 862 | } |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Converts a PHP regex to a javascript RegExp object statement. |
||
| 866 | * |
||
| 867 | * NOTE: This is an alias for the JSHelper's `convertRegex` method. |
||
| 868 | * More details are available on its usage there. |
||
| 869 | * |
||
| 870 | * @param string $regex A PHP preg regex |
||
| 871 | * @param string $statementType The type of statement to return: Defaults to a statement to create a RegExp object. |
||
| 872 | * @return array|string Depending on the specified return type. |
||
| 873 | * |
||
| 874 | * @see JSHelper::buildRegexStatement() |
||
| 875 | */ |
||
| 876 | public static function regex2js(string $regex, string $statementType=JSHelper::JS_REGEX_OBJECT) |
||
| 877 | { |
||
| 878 | return JSHelper::buildRegexStatement($regex, $statementType); |
||
| 879 | } |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Converts the specified variable to JSON. Works just |
||
| 883 | * like the native `json_encode` method, except that it |
||
| 884 | * will trigger an exception on failure, which has the |
||
| 885 | * json error details included in its developer details. |
||
| 886 | * |
||
| 887 | * @param mixed $variable |
||
| 888 | * @param int|NULL $options JSON encode options. |
||
| 889 | * @param int|NULL $depth |
||
| 890 | * @throws ConvertHelper_Exception |
||
| 891 | * @return string |
||
| 892 | */ |
||
| 893 | public static function var2json($variable, int $options=0, int $depth=512) : string |
||
| 894 | { |
||
| 895 | $result = json_encode($variable, $options, $depth); |
||
| 896 | |||
| 897 | if($result !== false) { |
||
| 898 | return $result; |
||
| 899 | } |
||
| 900 | |||
| 901 | throw new ConvertHelper_Exception( |
||
| 902 | 'Could not create json array'.json_last_error_msg(), |
||
| 903 | sprintf( |
||
| 904 | 'The call to json_encode failed for the variable [%s]. JSON error details: #%s, %s', |
||
| 905 | parseVariable($variable)->toString(), |
||
| 906 | json_last_error(), |
||
| 907 | json_last_error_msg() |
||
| 908 | ), |
||
| 909 | self::ERROR_JSON_ENCODE_FAILED |
||
| 910 | ); |
||
| 911 | } |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Strips all known UTF byte order marks from the specified string. |
||
| 915 | * |
||
| 916 | * @param string $string |
||
| 917 | * @return string |
||
| 918 | */ |
||
| 919 | public static function stripUTFBom($string) |
||
| 920 | { |
||
| 921 | $boms = FileHelper::getUTFBOMs(); |
||
| 922 | foreach($boms as $bomChars) { |
||
| 923 | $length = mb_strlen($bomChars); |
||
| 924 | $text = mb_substr($string, 0, $length); |
||
| 925 | if($text==$bomChars) { |
||
| 926 | return mb_substr($string, $length); |
||
| 927 | } |
||
| 928 | } |
||
| 929 | |||
| 930 | return $string; |
||
| 931 | } |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Converts a string to valid utf8, regardless |
||
| 935 | * of the string's encoding(s). |
||
| 936 | * |
||
| 937 | * @param string $string |
||
| 938 | * @return string |
||
| 939 | */ |
||
| 940 | public static function string2utf8($string) |
||
| 941 | { |
||
| 942 | if(!self::isStringASCII($string)) { |
||
| 943 | return \ForceUTF8\Encoding::toUTF8($string); |
||
| 944 | } |
||
| 945 | |||
| 946 | return $string; |
||
| 947 | } |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Checks whether the specified string is an ASCII |
||
| 951 | * string, without any special or UTF8 characters. |
||
| 952 | * Note: empty strings and NULL are considered ASCII. |
||
| 953 | * Any variable types other than strings are not. |
||
| 954 | * |
||
| 955 | * @param mixed $string |
||
| 956 | * @return boolean |
||
| 957 | */ |
||
| 958 | public static function isStringASCII($string) : bool |
||
| 959 | { |
||
| 960 | if($string === '' || $string === NULL) { |
||
| 961 | return true; |
||
| 962 | } |
||
| 963 | |||
| 964 | if(!is_string($string)) { |
||
| 965 | return false; |
||
| 966 | } |
||
| 967 | |||
| 968 | return !preg_match('/[^\x00-\x7F]/', $string); |
||
| 969 | } |
||
| 970 | |||
| 971 | public static function highlight_url($url) |
||
| 972 | { |
||
| 973 | $url = htmlspecialchars($url); |
||
| 974 | $url = str_replace( |
||
| 975 | array('/', '='), |
||
| 976 | array('/<wbr>', '=<wbr>'), |
||
| 977 | $url |
||
| 978 | ); |
||
| 979 | return $url; |
||
| 980 | } |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Calculates a percentage match of the source string with the target string. |
||
| 984 | * |
||
| 985 | * Options are: |
||
| 986 | * |
||
| 987 | * - maxLevenshtein, default: 10 |
||
| 988 | * Any levenshtein results above this value are ignored. |
||
| 989 | * |
||
| 990 | * - precision, default: 1 |
||
| 991 | * The precision of the percentage float value |
||
| 992 | * |
||
| 993 | * @param string $source |
||
| 994 | * @param string $target |
||
| 995 | * @param array $options |
||
| 996 | * @return float |
||
| 997 | */ |
||
| 998 | public static function matchString($source, $target, $options=array()) |
||
| 1019 | } |
||
| 1020 | |||
| 1021 | public static function interval2string(\DateInterval $interval) |
||
| 1022 | { |
||
| 1023 | $tokens = array('y', 'm', 'd', 'h', 'i', 's'); |
||
| 1024 | |||
| 1025 | $offset = 0; |
||
| 1026 | $keep = array(); |
||
| 1027 | foreach($tokens as $token) { |
||
| 1028 | if($interval->$token > 0) { |
||
| 1029 | $keep = array_slice($tokens, $offset); |
||
| 1030 | break; |
||
| 1031 | } |
||
| 1032 | |||
| 1033 | $offset++; |
||
| 1034 | } |
||
| 1035 | |||
| 1036 | $parts = array(); |
||
| 1037 | foreach($keep as $token) |
||
| 1038 | { |
||
| 1039 | $value = $interval->$token; |
||
| 1040 | $label = ''; |
||
| 1041 | |||
| 1042 | $suffix = 'p'; |
||
| 1043 | if($value == 1) { $suffix = 's'; } |
||
| 1044 | $token .= $suffix; |
||
| 1045 | |||
| 1046 | switch($token) { |
||
| 1047 | case 'ys': $label = t('1 year'); break; |
||
| 1048 | case 'yp': $label = t('%1$s years', $value); break; |
||
| 1049 | case 'ms': $label = t('1 month'); break; |
||
| 1050 | case 'mp': $label = t('%1$s months', $value); break; |
||
| 1051 | case 'ds': $label = t('1 day'); break; |
||
| 1052 | case 'dp': $label = t('%1$s days', $value); break; |
||
| 1053 | case 'hs': $label = t('1 hour'); break; |
||
| 1054 | case 'hp': $label = t('%1$s hours', $value); break; |
||
| 1055 | case 'is': $label = t('1 minute'); break; |
||
| 1056 | case 'ip': $label = t('%1$s minutes', $value); break; |
||
| 1057 | case 'ss': $label = t('1 second'); break; |
||
| 1058 | case 'sp': $label = t('%1$s seconds', $value); break; |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | $parts[] = $label; |
||
| 1062 | } |
||
| 1063 | |||
| 1064 | if(count($parts) == 1) { |
||
| 1065 | return $parts[0]; |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | $last = array_pop($parts); |
||
| 1069 | |||
| 1070 | return t('%1$s and %2$s', implode(', ', $parts), $last); |
||
| 1071 | } |
||
| 1072 | |||
| 1073 | const INTERVAL_DAYS = 'days'; |
||
| 1074 | |||
| 1075 | const INTERVAL_HOURS = 'hours'; |
||
| 1076 | |||
| 1077 | const INTERVAL_MINUTES = 'minutes'; |
||
| 1078 | |||
| 1079 | const INTERVAL_SECONDS = 'seconds'; |
||
| 1080 | |||
| 1081 | /** |
||
| 1082 | * Converts an interval to its total amount of days. |
||
| 1083 | * @param \DateInterval $interval |
||
| 1084 | * @return int |
||
| 1085 | */ |
||
| 1086 | public static function interval2days(\DateInterval $interval) : int |
||
| 1087 | { |
||
| 1088 | return self::interval2total($interval, self::INTERVAL_DAYS); |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Converts an interval to its total amount of hours. |
||
| 1093 | * @param \DateInterval $interval |
||
| 1094 | * @return int |
||
| 1095 | */ |
||
| 1096 | public static function interval2hours(\DateInterval $interval) : int |
||
| 1099 | } |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Converts an interval to its total amount of minutes. |
||
| 1103 | * @param \DateInterval $interval |
||
| 1104 | * @return int |
||
| 1105 | */ |
||
| 1106 | public static function interval2minutes(\DateInterval $interval) : int |
||
| 1107 | { |
||
| 1108 | return self::interval2total($interval, self::INTERVAL_MINUTES); |
||
| 1109 | } |
||
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Converts an interval to its total amount of seconds. |
||
| 1113 | * @param \DateInterval $interval |
||
| 1114 | * @return int |
||
| 1115 | */ |
||
| 1116 | public static function interval2seconds(\DateInterval $interval) : int |
||
| 1117 | { |
||
| 1118 | return self::interval2total($interval, self::INTERVAL_SECONDS); |
||
| 1119 | } |
||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Calculates the total amount of days / hours / minutes or seconds |
||
| 1123 | * of a date interval object (depending in the specified units), and |
||
| 1124 | * returns the total amount. |
||
| 1125 | * |
||
| 1126 | * @param \DateInterval $interval |
||
| 1127 | * @param string $unit What total value to calculate. |
||
| 1128 | * @return integer |
||
| 1129 | * |
||
| 1130 | * @see ConvertHelper::INTERVAL_SECONDS |
||
| 1131 | * @see ConvertHelper::INTERVAL_MINUTES |
||
| 1132 | * @see ConvertHelper::INTERVAL_HOURS |
||
| 1133 | * @see ConvertHelper::INTERVAL_DAYS |
||
| 1134 | */ |
||
| 1135 | public static function interval2total(\DateInterval $interval, $unit=self::INTERVAL_SECONDS) : int |
||
| 1136 | { |
||
| 1137 | $total = $interval->format('%a'); |
||
| 1138 | if ($unit == self::INTERVAL_DAYS) { |
||
| 1139 | return (int)$total; |
||
| 1140 | } |
||
| 1141 | |||
| 1142 | $total = ($total * 24) + ($interval->h ); |
||
| 1143 | if ($unit == self::INTERVAL_HOURS) { |
||
| 1144 | return (int)$total; |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | $total = ($total * 60) + ($interval->i ); |
||
| 1148 | if ($unit == self::INTERVAL_MINUTES) { |
||
| 1149 | return (int)$total; |
||
| 1150 | } |
||
| 1151 | |||
| 1152 | $total = ($total * 60) + ($interval->s ); |
||
| 1153 | if ($unit == self::INTERVAL_SECONDS) { |
||
| 1154 | return (int)$total; |
||
| 1155 | } |
||
| 1156 | |||
| 1157 | return 0; |
||
| 1158 | } |
||
| 1159 | |||
| 1160 | protected static $days; |
||
| 1161 | |||
| 1162 | protected static $daysShort; |
||
| 1163 | |||
| 1164 | protected static $daysInvariant = array( |
||
| 1165 | 'Monday', |
||
| 1166 | 'Tuesday', |
||
| 1167 | 'Wednesday', |
||
| 1168 | 'Thursday', |
||
| 1169 | 'Friday', |
||
| 1170 | 'Saturday', |
||
| 1171 | 'Sunday' |
||
| 1172 | ); |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Converts a date to the corresponding day name. |
||
| 1176 | * |
||
| 1177 | * @param \DateTime $date |
||
| 1178 | * @param string $short |
||
| 1179 | * @return string|NULL |
||
| 1180 | */ |
||
| 1181 | public static function date2dayName(\DateTime $date, $short=false) |
||
| 1182 | { |
||
| 1183 | $day = $date->format('l'); |
||
| 1184 | $invariant = self::getDayNamesInvariant(); |
||
| 1185 | |||
| 1186 | $idx = array_search($day, $invariant); |
||
| 1187 | if($idx !== false) { |
||
| 1188 | $localized = self::getDayNames($short); |
||
|
|
|||
| 1189 | return $localized[$idx]; |
||
| 1190 | } |
||
| 1191 | |||
| 1192 | return null; |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Retrieves a list of english day names. |
||
| 1197 | * @return string[] |
||
| 1198 | */ |
||
| 1199 | public static function getDayNamesInvariant() |
||
| 1200 | { |
||
| 1201 | return self::$daysInvariant; |
||
| 1202 | } |
||
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Retrieves the day names list for the current locale. |
||
| 1206 | * |
||
| 1207 | * @param string $short |
||
| 1208 | * @return string[] |
||
| 1209 | */ |
||
| 1210 | public static function getDayNames($short=false) |
||
| 1211 | { |
||
| 1212 | if($short) { |
||
| 1213 | if(!isset(self::$daysShort)) { |
||
| 1214 | self::$daysShort = array( |
||
| 1215 | t('Mon'), |
||
| 1216 | t('Tue'), |
||
| 1217 | t('Wed'), |
||
| 1218 | t('Thu'), |
||
| 1219 | t('Fri'), |
||
| 1220 | t('Sat'), |
||
| 1221 | t('Sun') |
||
| 1222 | ); |
||
| 1223 | } |
||
| 1224 | |||
| 1225 | return self::$daysShort; |
||
| 1226 | } |
||
| 1227 | |||
| 1228 | if(!isset(self::$days)) { |
||
| 1229 | self::$days = array( |
||
| 1230 | t('Monday'), |
||
| 1231 | t('Tuesday'), |
||
| 1232 | t('Wednesday'), |
||
| 1233 | t('Thursday'), |
||
| 1234 | t('Friday'), |
||
| 1235 | t('Saturday'), |
||
| 1236 | t('Sunday') |
||
| 1237 | ); |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | return self::$days; |
||
| 1241 | } |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Implodes an array with a separator character, and the last item with "add". |
||
| 1245 | * |
||
| 1246 | * @param array $list The indexed array with items to implode. |
||
| 1247 | * @param string $sep The separator character to use. |
||
| 1248 | * @param string $conjunction The word to use as conjunction with the last item in the list. NOTE: include spaces as needed. |
||
| 1249 | * @return string |
||
| 1250 | */ |
||
| 1251 | public static function implodeWithAnd(array $list, $sep = ', ', $conjunction = null) |
||
| 1252 | { |
||
| 1253 | if(empty($list)) { |
||
| 1254 | return ''; |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | if(empty($conjunction)) { |
||
| 1258 | $conjunction = t('and'); |
||
| 1259 | } |
||
| 1260 | |||
| 1261 | $last = array_pop($list); |
||
| 1262 | if($list) { |
||
| 1263 | return implode($sep, $list) . $conjunction . ' ' . $last; |
||
| 1264 | } |
||
| 1265 | |||
| 1266 | return $last; |
||
| 1267 | } |
||
| 1268 | |||
| 1269 | /** |
||
| 1270 | * Splits a string into an array of all characters it is composed of. |
||
| 1271 | * Unicode character safe. |
||
| 1272 | * |
||
| 1273 | * NOTE: Spaces and newlines (both \r and \n) are also considered single |
||
| 1274 | * characters. |
||
| 1275 | * |
||
| 1276 | * @param string $string |
||
| 1277 | * @return array |
||
| 1278 | */ |
||
| 1279 | public static function string2array(string $string) : array |
||
| 1280 | { |
||
| 1281 | $result = preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY); |
||
| 1282 | if($result !== false) { |
||
| 1283 | return $result; |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | return array(); |
||
| 1287 | } |
||
| 1288 | |||
| 1289 | /** |
||
| 1290 | * Checks whether the specified string contains HTML code. |
||
| 1291 | * |
||
| 1292 | * @param string $string |
||
| 1293 | * @return boolean |
||
| 1294 | */ |
||
| 1295 | public static function isStringHTML(string $string) : bool |
||
| 1307 | } |
||
| 1308 | |||
| 1309 | /** |
||
| 1310 | * UTF8-safe wordwrap method: works like the regular wordwrap |
||
| 1311 | * PHP function but compatible with UTF8. Otherwise the lengths |
||
| 1312 | * are not calculated correctly. |
||
| 1313 | * |
||
| 1314 | * @param string $str |
||
| 1315 | * @param int $width |
||
| 1316 | * @param string $break |
||
| 1317 | * @param bool $cut |
||
| 1318 | * @return string |
||
| 1319 | */ |
||
| 1320 | public static function wordwrap(string $str, int $width = 75, string $break = "\n", bool $cut = false) : string |
||
| 1321 | { |
||
| 1322 | $wrapper = new ConvertHelper_WordWrapper(); |
||
| 1323 | |||
| 1324 | return $wrapper |
||
| 1325 | ->setLineWidth($width) |
||
| 1326 | ->setBreakCharacter($break) |
||
| 1327 | ->setCuttingEnabled($cut) |
||
| 1328 | ->wrapText($str); |
||
| 1329 | } |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * Calculates the byte length of a string, taking into |
||
| 1333 | * account any unicode characters. |
||
| 1334 | * |
||
| 1335 | * @param string $string |
||
| 1336 | * @return int |
||
| 1337 | * @see https://stackoverflow.com/a/9718273/2298192 |
||
| 1338 | */ |
||
| 1339 | public static function string2bytes($string) |
||
| 1340 | { |
||
| 1341 | return mb_strlen($string, '8bit'); |
||
| 1342 | } |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Creates a short, 8-character long hash for the specified string. |
||
| 1346 | * |
||
| 1347 | * WARNING: Not cryptographically safe. |
||
| 1348 | * |
||
| 1349 | * @param string $string |
||
| 1350 | * @return string |
||
| 1351 | */ |
||
| 1352 | public static function string2shortHash($string) |
||
| 1353 | { |
||
| 1354 | return hash('crc32', $string, false); |
||
| 1355 | } |
||
| 1356 | |||
| 1357 | public static function string2hash($string) |
||
| 1358 | { |
||
| 1359 | return md5($string); |
||
| 1360 | } |
||
| 1361 | |||
| 1362 | public static function callback2string($callback) : string |
||
| 1363 | { |
||
| 1364 | return parseVariable($callback)->toString(); |
||
| 1365 | } |
||
| 1366 | |||
| 1367 | public static function exception2info(\Throwable $e) : ConvertHelper_ThrowableInfo |
||
| 1368 | { |
||
| 1369 | return self::throwable2info($e); |
||
| 1370 | } |
||
| 1371 | |||
| 1372 | public static function throwable2info(\Throwable $e) : ConvertHelper_ThrowableInfo |
||
| 1373 | { |
||
| 1374 | return ConvertHelper_ThrowableInfo::fromThrowable($e); |
||
| 1375 | } |
||
| 1376 | |||
| 1377 | /** |
||
| 1378 | * Parses the specified query string like the native |
||
| 1379 | * function <code>parse_str</code>, without the key |
||
| 1380 | * naming limitations. |
||
| 1381 | * |
||
| 1382 | * Using parse_str, dots or spaces in key names are |
||
| 1383 | * replaced by underscores. This method keeps all names |
||
| 1384 | * intact. |
||
| 1385 | * |
||
| 1386 | * It still uses the parse_str implementation as it |
||
| 1387 | * is tested and tried, but fixes the parameter names |
||
| 1388 | * after parsing, as needed. |
||
| 1389 | * |
||
| 1390 | * @param string $queryString |
||
| 1391 | * @return array |
||
| 1392 | * @see ConvertHelper_QueryParser |
||
| 1393 | */ |
||
| 1394 | public static function parseQueryString(string $queryString) : array |
||
| 1395 | { |
||
| 1396 | $parser = new ConvertHelper_QueryParser(); |
||
| 1397 | return $parser->parse($queryString); |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * Searches for needle in the specified string, and returns a list |
||
| 1402 | * of all occurrences, including the matched string. The matched |
||
| 1403 | * string is useful when doing a case insensitive search, as it |
||
| 1404 | * shows the exact matched case of needle. |
||
| 1405 | * |
||
| 1406 | * @param string $needle |
||
| 1407 | * @param string $haystack |
||
| 1408 | * @param bool $caseInsensitive |
||
| 1409 | * @return ConvertHelper_StringMatch[] |
||
| 1410 | */ |
||
| 1411 | public static function findString(string $needle, string $haystack, bool $caseInsensitive=false) |
||
| 1412 | { |
||
| 1413 | if($needle === '') { |
||
| 1414 | return array(); |
||
| 1415 | } |
||
| 1416 | |||
| 1417 | $function = 'mb_strpos'; |
||
| 1418 | if($caseInsensitive) { |
||
| 1419 | $function = 'mb_stripos'; |
||
| 1420 | } |
||
| 1421 | |||
| 1422 | $pos = 0; |
||
| 1423 | $positions = array(); |
||
| 1424 | $length = mb_strlen($needle); |
||
| 1425 | |||
| 1426 | while( ($pos = $function($haystack, $needle, $pos)) !== false) |
||
| 1427 | { |
||
| 1428 | $match = mb_substr($haystack, $pos, $length); |
||
| 1429 | $positions[] = new ConvertHelper_StringMatch($pos, $match); |
||
| 1430 | $pos += $length; |
||
| 1431 | } |
||
| 1432 | |||
| 1433 | return $positions; |
||
| 1434 | } |
||
| 1435 | |||
| 1436 | /** |
||
| 1437 | * Like explode, but trims all entries, and removes |
||
| 1438 | * empty entries from the resulting array. |
||
| 1439 | * |
||
| 1440 | * @param string $delimiter |
||
| 1441 | * @param string $string |
||
| 1442 | * @return string[] |
||
| 1443 | */ |
||
| 1444 | public static function explodeTrim(string $delimiter, string $string) : array |
||
| 1445 | { |
||
| 1446 | if(empty($string) || empty($delimiter)) { |
||
| 1447 | return array(); |
||
| 1448 | } |
||
| 1449 | |||
| 1450 | $tokens = explode($delimiter, $string); |
||
| 1451 | $tokens = array_map('trim', $tokens); |
||
| 1452 | |||
| 1453 | $keep = array(); |
||
| 1454 | foreach($tokens as $token) { |
||
| 1455 | if($token !== '') { |
||
| 1456 | $keep[] = $token; |
||
| 1457 | } |
||
| 1458 | } |
||
| 1459 | |||
| 1460 | return $keep; |
||
| 1461 | } |
||
| 1462 | |||
| 1463 | protected static $eolChars; |
||
| 1464 | |||
| 1465 | /** |
||
| 1466 | * Detects the most used end-of-line character in the subject string. |
||
| 1467 | * |
||
| 1468 | * @param string $str The string to check. |
||
| 1469 | * @return NULL|ConvertHelper_EOL The detected EOL instance, or NULL if none has been detected. |
||
| 1470 | */ |
||
| 1471 | public static function detectEOLCharacter(string $subjectString) : ?ConvertHelper_EOL |
||
| 1472 | { |
||
| 1473 | if(empty($subjectString)) { |
||
| 1474 | return null; |
||
| 1475 | } |
||
| 1476 | |||
| 1477 | if(!isset(self::$eolChars)) |
||
| 1478 | { |
||
| 1479 | $cr = chr((int)hexdec('0d')); |
||
| 1480 | $lf = chr((int)hexdec('0a')); |
||
| 1481 | |||
| 1482 | self::$eolChars = array( |
||
| 1483 | array( |
||
| 1484 | 'char' => $cr.$lf, |
||
| 1485 | 'type' => ConvertHelper_EOL::TYPE_CRLF, |
||
| 1486 | 'description' => t('Carriage return followed by a line feed'), |
||
| 1487 | ), |
||
| 1488 | array( |
||
| 1489 | 'char' => $lf.$cr, |
||
| 1490 | 'type' => ConvertHelper_EOL::TYPE_LFCR, |
||
| 1491 | 'description' => t('Line feed followed by a carriage return'), |
||
| 1492 | ), |
||
| 1493 | array( |
||
| 1494 | 'char' => $lf, |
||
| 1495 | 'type' => ConvertHelper_EOL::TYPE_LF, |
||
| 1496 | 'description' => t('Line feed'), |
||
| 1497 | ), |
||
| 1498 | array( |
||
| 1499 | 'char' => $cr, |
||
| 1500 | 'type' => ConvertHelper_EOL::TYPE_CR, |
||
| 1501 | 'description' => t('Carriage Return'), |
||
| 1502 | ), |
||
| 1503 | ); |
||
| 1504 | } |
||
| 1505 | |||
| 1506 | $max = 0; |
||
| 1507 | $results = array(); |
||
| 1508 | foreach(self::$eolChars as $def) |
||
| 1509 | { |
||
| 1510 | $amount = substr_count($subjectString, $def['char']); |
||
| 1511 | |||
| 1512 | if($amount > $max) |
||
| 1513 | { |
||
| 1514 | $max = $amount; |
||
| 1515 | $results[] = $def; |
||
| 1516 | } |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | if(empty($results)) { |
||
| 1520 | return null; |
||
| 1521 | } |
||
| 1522 | |||
| 1523 | return new ConvertHelper_EOL( |
||
| 1524 | $results[0]['char'], |
||
| 1525 | $results[0]['type'], |
||
| 1526 | $results[0]['description'] |
||
| 1527 | ); |
||
| 1528 | } |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Removes the specified keys from the target array, |
||
| 1532 | * if they exist. |
||
| 1533 | * |
||
| 1534 | * @param array $array |
||
| 1535 | * @param array $keys |
||
| 1536 | */ |
||
| 1537 | public static function arrayRemoveKeys(array &$array, array $keys) : void |
||
| 1543 | } |
||
| 1544 | } |
||
| 1545 | } |
||
| 1546 | |||
| 1547 | /** |
||
| 1548 | * Checks if the specified variable is an integer or a string containing an integer. |
||
| 1549 | * Accepts both positive and negative integers. |
||
| 1550 | * |
||
| 1551 | * @param mixed $value |
||
| 1552 | * @return bool |
||
| 1553 | */ |
||
| 1554 | public static function isInteger($value) : bool |
||
| 1571 | } |
||
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Converts an amount of seconds to a DateInterval object. |
||
| 1575 | * |
||
| 1576 | * @param int $seconds |
||
| 1577 | * @return \DateInterval |
||
| 1578 | * @throws ConvertHelper_Exception If the date interval cannot be created. |
||
| 1579 | * |
||
| 1580 | * @see ConvertHelper::ERROR_CANNOT_GET_DATE_DIFF |
||
| 1581 | */ |
||
| 1582 | public static function seconds2interval(int $seconds) : \DateInterval |
||
| 1601 | ); |
||
| 1602 | } |
||
| 1603 | } |
||
| 1604 |