Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Stringy 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Stringy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * An instance's string. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $str; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The string's encoding, which should be one of the mbstring module's |
||
| 26 | * supported encodings. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $encoding; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Initializes a Stringy object and assigns both str and encoding properties |
||
| 34 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
| 35 | * $encoding is not specified, it defaults to mb_internal_encoding(). Throws |
||
| 36 | * an InvalidArgumentException if the first argument is an array or object |
||
| 37 | * without a __toString method. |
||
| 38 | * |
||
| 39 | * @param mixed $str Value to modify, after being cast to string |
||
| 40 | * @param string $encoding The character encoding |
||
| 41 | * |
||
| 42 | * @throws \InvalidArgumentException if an array or object without a |
||
| 43 | * __toString method is passed as the first argument |
||
| 44 | */ |
||
| 45 | 1086 | public function __construct($str = '', $encoding = null) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Returns the value in $str. |
||
| 83 | * |
||
| 84 | * @return string The current value of the $str property |
||
| 85 | */ |
||
| 86 | 163 | public function __toString() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Returns a new string with $string appended. |
||
| 93 | * |
||
| 94 | * @param string $string The string to append |
||
| 95 | * |
||
| 96 | * @return static Object with appended $string |
||
| 97 | */ |
||
| 98 | 5 | public function append($string) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Append an password (limited to chars that are good readable). |
||
| 105 | * |
||
| 106 | * @param int $length length of the random string |
||
| 107 | * |
||
| 108 | * @return static Object with appended password |
||
| 109 | */ |
||
| 110 | 1 | public function appendPassword($length) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Append an unique identifier. |
||
| 119 | * |
||
| 120 | * @param string|int $extraPrefix |
||
| 121 | * |
||
| 122 | * @return static Object with appended unique identifier as md5-hash |
||
| 123 | */ |
||
| 124 | 1 | public function appendUniqueIdentifier($extraPrefix = '') |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Append an random string. |
||
| 137 | * |
||
| 138 | * @param int $length length of the random string |
||
| 139 | * @param string $possibleChars characters string for the random selection |
||
| 140 | * |
||
| 141 | * @return static Object with appended random string |
||
| 142 | */ |
||
| 143 | 2 | public function appendRandomString($length, $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Creates a Stringy object and assigns both str and encoding properties |
||
| 167 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
| 168 | * $encoding is not specified, it defaults to mb_internal_encoding(). It |
||
| 169 | * then returns the initialized object. Throws an InvalidArgumentException |
||
| 170 | * if the first argument is an array or object without a __toString method. |
||
| 171 | * |
||
| 172 | * @param mixed $str Value to modify, after being cast to string |
||
| 173 | * @param string $encoding The character encoding |
||
| 174 | * |
||
| 175 | * @return static A Stringy object |
||
| 176 | * @throws \InvalidArgumentException if an array or object without a |
||
| 177 | * __toString method is passed as the first argument |
||
| 178 | */ |
||
| 179 | 1076 | public static function create($str = '', $encoding = null) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Returns the substring between $start and $end, if found, or an empty |
||
| 186 | * string. An optional offset may be supplied from which to begin the |
||
| 187 | * search for the start string. |
||
| 188 | * |
||
| 189 | * @param string $start Delimiter marking the start of the substring |
||
| 190 | * @param string $end Delimiter marking the end of the substring |
||
| 191 | * @param int $offset Index from which to begin the search |
||
| 192 | * |
||
| 193 | * @return static Object whose $str is a substring between $start and $end |
||
| 194 | */ |
||
| 195 | 16 | public function between($start, $end, $offset = 0) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Returns the index of the first occurrence of $needle in the string, |
||
| 213 | * and false if not found. Accepts an optional offset from which to begin |
||
| 214 | * the search. |
||
| 215 | * |
||
| 216 | * @param string $needle Substring to look for |
||
| 217 | * @param int $offset Offset from which to search |
||
| 218 | * |
||
| 219 | * @return int|bool The occurrence's index if found, otherwise false |
||
| 220 | */ |
||
| 221 | 28 | public function indexOf($needle, $offset = 0) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the substring beginning at $start with the specified $length. |
||
| 228 | * It differs from the UTF8::substr() function in that providing a $length of |
||
| 229 | * null will return the rest of the string, rather than an empty string. |
||
| 230 | * |
||
| 231 | * @param int $start Position of the first character to use |
||
| 232 | * @param int $length Maximum number of characters used |
||
| 233 | * |
||
| 234 | * @return static Object with its $str being the substring |
||
| 235 | */ |
||
| 236 | 64 | public function substr($start, $length = null) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Returns the length of the string. |
||
| 249 | * |
||
| 250 | * @return int The number of characters in $str given the encoding |
||
| 251 | */ |
||
| 252 | 248 | public function length() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Trims the string and replaces consecutive whitespace characters with a |
||
| 259 | * single space. This includes tabs and newline characters, as well as |
||
| 260 | * multibyte whitespace such as the thin space and ideographic space. |
||
| 261 | * |
||
| 262 | * @return static Object with a trimmed $str and condensed whitespace |
||
| 263 | */ |
||
| 264 | 52 | public function collapseWhitespace() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Returns a string with whitespace removed from the start and end of the |
||
| 271 | * string. Supports the removal of unicode whitespace. Accepts an optional |
||
| 272 | * string of characters to strip instead of the defaults. |
||
| 273 | * |
||
| 274 | * @param string $chars Optional string of characters to strip |
||
| 275 | * |
||
| 276 | * @return static Object with a trimmed $str |
||
| 277 | */ |
||
| 278 | 153 | View Code Duplication | public function trim($chars = null) |
| 288 | |||
| 289 | /** |
||
| 290 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
| 291 | * |
||
| 292 | * @param string $pattern The regular expression pattern |
||
| 293 | * @param string $replacement The string to replace with |
||
| 294 | * @param string $options Matching conditions to be used |
||
| 295 | * |
||
| 296 | * @return static Object with the result2ing $str after the replacements |
||
| 297 | */ |
||
| 298 | 223 | public function regexReplace($pattern, $replacement, $options = '') |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Returns true if the string contains all $needles, false otherwise. By |
||
| 315 | * default the comparison is case-sensitive, but can be made insensitive by |
||
| 316 | * setting $caseSensitive to false. |
||
| 317 | * |
||
| 318 | * @param array $needles SubStrings to look for |
||
| 319 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 320 | * |
||
| 321 | * @return bool Whether or not $str contains $needle |
||
| 322 | */ |
||
| 323 | 43 | View Code Duplication | public function containsAll($needles, $caseSensitive = true) |
| 338 | |||
| 339 | /** |
||
| 340 | * Returns true if the string contains $needle, false otherwise. By default |
||
| 341 | * the comparison is case-sensitive, but can be made insensitive by setting |
||
| 342 | * $caseSensitive to false. |
||
| 343 | * |
||
| 344 | * @param string $needle Substring to look for |
||
| 345 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 346 | * |
||
| 347 | * @return bool Whether or not $str contains $needle |
||
| 348 | */ |
||
| 349 | 105 | public function contains($needle, $caseSensitive = true) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Returns true if the string contains any $needles, false otherwise. By |
||
| 362 | * default the comparison is case-sensitive, but can be made insensitive by |
||
| 363 | * setting $caseSensitive to false. |
||
| 364 | * |
||
| 365 | * @param array $needles SubStrings to look for |
||
| 366 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 367 | * |
||
| 368 | * @return bool Whether or not $str contains $needle |
||
| 369 | */ |
||
| 370 | 43 | View Code Duplication | public function containsAny($needles, $caseSensitive = true) |
| 385 | |||
| 386 | /** |
||
| 387 | * Returns the length of the string, implementing the countable interface. |
||
| 388 | * |
||
| 389 | * @return int The number of characters in the string, given the encoding |
||
| 390 | */ |
||
| 391 | 1 | public function count() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Returns the number of occurrences of $substring in the given string. |
||
| 398 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
| 399 | * by setting $caseSensitive to false. |
||
| 400 | * |
||
| 401 | * @param string $substring The substring to search for |
||
| 402 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 403 | * |
||
| 404 | * @return int The number of $substring occurrences |
||
| 405 | */ |
||
| 406 | 15 | public function countSubstr($substring, $caseSensitive = true) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Returns a lowercase and trimmed string separated by dashes. Dashes are |
||
| 420 | * inserted before uppercase characters (with the exception of the first |
||
| 421 | * character of the string), and in place of spaces as well as underscores. |
||
| 422 | * |
||
| 423 | * @return static Object with a dasherized $str |
||
| 424 | */ |
||
| 425 | 19 | public function dasherize() |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Returns a lowercase and trimmed string separated by the given delimiter. |
||
| 432 | * Delimiters are inserted before uppercase characters (with the exception |
||
| 433 | * of the first character of the string), and in place of spaces, dashes, |
||
| 434 | * and underscores. Alpha delimiters are not converted to lowercase. |
||
| 435 | * |
||
| 436 | * @param string $delimiter Sequence used to separate parts of the string |
||
| 437 | * |
||
| 438 | * @return static Object with a delimited $str |
||
| 439 | */ |
||
| 440 | 49 | public function delimit($delimiter) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Ensures that the string begins with $substring. If it doesn't, it's |
||
| 455 | * prepended. |
||
| 456 | * |
||
| 457 | * @param string $substring The substring to add if not present |
||
| 458 | * |
||
| 459 | * @return static Object with its $str prefixed by the $substring |
||
| 460 | */ |
||
| 461 | 10 | View Code Duplication | public function ensureLeft($substring) |
| 471 | |||
| 472 | /** |
||
| 473 | * Returns true if the string begins with $substring, false otherwise. By |
||
| 474 | * default, the comparison is case-sensitive, but can be made insensitive |
||
| 475 | * by setting $caseSensitive to false. |
||
| 476 | * |
||
| 477 | * @param string $substring The substring to look for |
||
| 478 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 479 | * |
||
| 480 | * @return bool Whether or not $str starts with $substring |
||
| 481 | */ |
||
| 482 | 45 | public function startsWith($substring, $caseSensitive = true) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Returns true if the string begins with any of $substrings, false otherwise. |
||
| 496 | * By default the comparison is case-sensitive, but can be made insensitive by |
||
| 497 | * setting $caseSensitive to false. |
||
| 498 | * |
||
| 499 | * @param array $substrings Substrings to look for |
||
| 500 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 501 | * |
||
| 502 | * @return bool Whether or not $str starts with $substring |
||
| 503 | */ |
||
| 504 | 12 | View Code Duplication | public function startsWithAny(array $substrings, $caseSensitive = true) |
| 505 | { |
||
| 506 | 12 | if (empty($substrings)) { |
|
| 507 | return false; |
||
| 508 | } |
||
| 509 | |||
| 510 | 12 | foreach ($substrings as $substring) { |
|
| 511 | 12 | if ($this->startsWith($substring, $caseSensitive)) { |
|
| 512 | 6 | return true; |
|
| 513 | } |
||
| 514 | 6 | } |
|
| 515 | |||
| 516 | 6 | return false; |
|
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Ensures that the string ends with $substring. If it doesn't, it's |
||
| 521 | * appended. |
||
| 522 | * |
||
| 523 | * @param string $substring The substring to add if not present |
||
| 524 | * |
||
| 525 | * @return static Object with its $str suffixed by the $substring |
||
| 526 | */ |
||
| 527 | 10 | View Code Duplication | public function ensureRight($substring) |
| 537 | |||
| 538 | /** |
||
| 539 | * Returns true if the string ends with $substring, false otherwise. By |
||
| 540 | * default, the comparison is case-sensitive, but can be made insensitive |
||
| 541 | * by setting $caseSensitive to false. |
||
| 542 | * |
||
| 543 | * @param string $substring The substring to look for |
||
| 544 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 545 | * |
||
| 546 | * @return bool Whether or not $str ends with $substring |
||
| 547 | */ |
||
| 548 | 33 | public function endsWith($substring, $caseSensitive = true) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Returns true if the string ends with any of $substrings, false otherwise. |
||
| 570 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
| 571 | * by setting $caseSensitive to false. |
||
| 572 | * |
||
| 573 | * @param string[] $substrings Substrings to look for |
||
| 574 | * @param bool $caseSensitive Whether or not to enforce |
||
| 575 | 12 | * case-sensitivity |
|
| 576 | * @return bool Whether or not $str ends with $substring |
||
| 577 | 12 | */ |
|
| 578 | View Code Duplication | public function endsWithAny($substrings, $caseSensitive = true) |
|
| 592 | |||
| 593 | 3 | /** |
|
| 594 | * Returns the first $n characters of the string. |
||
| 595 | 3 | * |
|
| 596 | * @param int $n Number of characters to retrieve from the start |
||
| 597 | * |
||
| 598 | * @return static Object with its $str being the first $n chars |
||
| 599 | */ |
||
| 600 | View Code Duplication | public function first($n) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Returns the encoding used by the Stringy object. |
||
| 615 | * |
||
| 616 | 4 | * @return string The current value of the $encoding property |
|
| 617 | */ |
||
| 618 | public function getEncoding() |
||
| 622 | 4 | ||
| 623 | 3 | /** |
|
| 624 | 3 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
|
| 625 | * interface. The ArrayIterator's constructor is passed an array of chars |
||
| 626 | 4 | * in the multibyte string. This enables the use of foreach with instances |
|
| 627 | * of Stringy\Stringy. |
||
| 628 | * |
||
| 629 | * @return \ArrayIterator An iterator for the characters in the string |
||
| 630 | */ |
||
| 631 | public function getIterator() |
||
| 635 | |||
| 636 | 11 | /** |
|
| 637 | * Returns an array consisting of the characters in the string. |
||
| 638 | 11 | * |
|
| 639 | * @return array An array of string chars |
||
| 640 | */ |
||
| 641 | public function chars() |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Returns the character at $index, with indexes starting at 0. |
||
| 656 | * |
||
| 657 | * @param int $index Position of the character |
||
| 658 | * |
||
| 659 | 103 | * @return static The character at $index |
|
| 660 | */ |
||
| 661 | 103 | public function at($index) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Returns true if the string contains a lower case char, false |
||
| 668 | * otherwise. |
||
| 669 | * |
||
| 670 | * @return bool Whether or not the string contains a lower case character. |
||
| 671 | */ |
||
| 672 | public function hasLowerCase() |
||
| 676 | 12 | ||
| 677 | /** |
||
| 678 | * Returns true if $str matches the supplied pattern, false otherwise. |
||
| 679 | * |
||
| 680 | * @param string $pattern Regex pattern to match against |
||
| 681 | * |
||
| 682 | * @return bool Whether or not $str matches the pattern |
||
| 683 | */ |
||
| 684 | protected function matchesPattern($pattern) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Returns true if the string contains an upper case char, false |
||
| 695 | * otherwise. |
||
| 696 | * |
||
| 697 | * @return bool Whether or not the string contains an upper case character. |
||
| 698 | */ |
||
| 699 | public function hasUpperCase() |
||
| 703 | |||
| 704 | 5 | /** |
|
| 705 | * Convert all HTML entities to their applicable characters. |
||
| 706 | * |
||
| 707 | * @param int|null $flags Optional flags |
||
| 708 | * |
||
| 709 | * @return static Object with the resulting $str after being html decoded. |
||
| 710 | */ |
||
| 711 | public function htmlDecode($flags = ENT_COMPAT) |
||
| 717 | 3 | ||
| 718 | /** |
||
| 719 | * Convert all applicable characters to HTML entities. |
||
| 720 | * |
||
| 721 | * @param int|null $flags Optional flags |
||
| 722 | * |
||
| 723 | * @return static Object with the resulting $str after being html encoded. |
||
| 724 | */ |
||
| 725 | 27 | public function htmlEncode($flags = ENT_COMPAT) |
|
| 731 | 27 | ||
| 732 | 27 | /** |
|
| 733 | 27 | * Capitalizes the first word of the string, replaces underscores with |
|
| 734 | * spaces, and strips '_id'. |
||
| 735 | 27 | * |
|
| 736 | * @return static Object with a humanized $str |
||
| 737 | 27 | */ |
|
| 738 | public function humanize() |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Converts the first character of the supplied string to upper case. |
||
| 747 | * |
||
| 748 | * @return static Object with the first character of $str being upper case |
||
| 749 | */ |
||
| 750 | View Code Duplication | public function upperCaseFirst() |
|
| 764 | 8 | ||
| 765 | /** |
||
| 766 | 8 | * Returns the index of the last occurrence of $needle in the string, |
|
| 767 | 8 | * and false if not found. Accepts an optional offset from which to begin |
|
| 768 | 1 | * the search. Offsets may be negative to count from the last character |
|
| 769 | * in the string. |
||
| 770 | * |
||
| 771 | 7 | * @param string $needle Substring to look for |
|
| 772 | 7 | * @param int $offset Offset from which to search |
|
| 773 | * |
||
| 774 | 7 | * @return int|bool The last occurrence's index if found, otherwise false |
|
| 775 | */ |
||
| 776 | 7 | public function indexOfLast($needle, $offset = 0) |
|
| 780 | |||
| 781 | /** |
||
| 782 | * Inserts $substring into the string at the $index provided. |
||
| 783 | * |
||
| 784 | * @param string $substring String to be inserted |
||
| 785 | * @param int $index The index at which to insert the substring |
||
| 786 | * |
||
| 787 | * @return static Object with the resulting $str after the insertion |
||
| 788 | */ |
||
| 789 | View Code Duplication | public function insert($substring, $index) |
|
| 803 | |||
| 804 | /** |
||
| 805 | * Returns true if the string contains the $pattern, otherwise false. |
||
| 806 | * |
||
| 807 | * WARNING: Asterisks ("*") are translated into (".*") zero-or-more regular |
||
| 808 | * expression wildcards. |
||
| 809 | 10 | * |
|
| 810 | * @credit Originally from Laravel, thanks Taylor. |
||
| 811 | 10 | * |
|
| 812 | * @param string $pattern The string or pattern to match against. |
||
| 813 | * |
||
| 814 | * @return bool Whether or not we match the provided pattern. |
||
| 815 | */ |
||
| 816 | public function is($pattern) |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Returns true if the string contains only alphabetic chars, false |
||
| 830 | * otherwise. |
||
| 831 | * |
||
| 832 | * @return bool Whether or not $str contains only alphabetic chars |
||
| 833 | 13 | */ |
|
| 834 | public function isAlpha() |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Determine whether the string is considered to be empty. |
||
| 841 | * |
||
| 842 | * A variable is considered empty if it does not exist or if its value equals FALSE. |
||
| 843 | * empty() does not generate a warning if the variable does not exist. |
||
| 844 | 15 | * |
|
| 845 | * @return bool |
||
| 846 | 15 | */ |
|
| 847 | public function isEmpty() |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Returns true if the string contains only alphabetic and numeric chars, |
||
| 854 | * false otherwise. |
||
| 855 | 13 | * |
|
| 856 | * @return bool Whether or not $str contains only alphanumeric chars |
||
| 857 | 13 | */ |
|
| 858 | public function isAlphanumeric() |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Returns true if the string contains only whitespace chars, false |
||
| 865 | 1 | * otherwise. |
|
| 866 | * |
||
| 867 | 1 | * @return bool Whether or not $str contains only whitespace characters |
|
| 868 | */ |
||
| 869 | public function isBlank() |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Returns true if the string contains only hexadecimal chars, false |
||
| 876 | * otherwise. |
||
| 877 | * |
||
| 878 | * @return bool Whether or not $str contains only hexadecimal chars |
||
| 879 | */ |
||
| 880 | 1 | public function isHexadecimal() |
|
| 884 | |||
| 885 | /** |
||
| 886 | * Returns true if the string contains HTML-Tags, false otherwise. |
||
| 887 | * |
||
| 888 | * @return bool Whether or not $str contains HTML-Tags |
||
| 889 | */ |
||
| 890 | public function isHtml() |
||
| 894 | 20 | ||
| 895 | 1 | /** |
|
| 896 | * Returns true if the string contains a valid E-Mail address, false otherwise. |
||
| 897 | * |
||
| 898 | 19 | * @param bool $useExampleDomainCheck |
|
| 899 | * @param bool $useTypoInDomainCheck |
||
| 900 | 19 | * @param bool $useTemporaryDomainCheck |
|
| 901 | 11 | * @param bool $useDnsCheck |
|
| 902 | * |
||
| 903 | 8 | * @return bool Whether or not $str contains a valid E-Mail address |
|
| 904 | */ |
||
| 905 | public function isEmail($useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false) |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Returns true if the string is JSON, false otherwise. Unlike json_decode |
||
| 912 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
||
| 913 | 8 | * in that an empty string is not considered valid JSON. |
|
| 914 | * |
||
| 915 | 8 | * @return bool Whether or not $str is JSON |
|
| 916 | 3 | */ |
|
| 917 | public function isJson() |
||
| 931 | |||
| 932 | /** |
||
| 933 | * Returns true if the string contains only lower case chars, false |
||
| 934 | * otherwise. |
||
| 935 | 6 | * |
|
| 936 | 6 | * @return bool Whether or not $str contains only lower case characters |
|
| 937 | 6 | */ |
|
| 938 | 6 | public function isLowerCase() |
|
| 946 | |||
| 947 | /** |
||
| 948 | * Returns true if the string is serialized, false otherwise. |
||
| 949 | * |
||
| 950 | * @return bool Whether or not $str is serialized |
||
| 951 | 8 | */ |
|
| 952 | public function isSerialized() |
||
| 969 | 4 | ||
| 970 | 8 | /** |
|
| 971 | * Returns true if the string contains only lower case chars, false |
||
| 972 | * otherwise. |
||
| 973 | 4 | * |
|
| 974 | * @return bool Whether or not $str contains only lower case characters |
||
| 975 | */ |
||
| 976 | public function isUpperCase() |
||
| 980 | |||
| 981 | /** |
||
| 982 | 15 | * Returns the last $n characters of the string. |
|
| 983 | * |
||
| 984 | 15 | * @param int $n Number of characters to retrieve from the end |
|
| 985 | * |
||
| 986 | * @return static Object with its $str being the last $n chars |
||
| 987 | 15 | */ |
|
| 988 | 15 | View Code Duplication | public function last($n) |
| 1000 | |||
| 1001 | 10 | /** |
|
| 1002 | * Splits on newlines and carriage returns, returning an array of Stringy |
||
| 1003 | 10 | * objects corresponding to the lines in the string. |
|
| 1004 | 10 | * |
|
| 1005 | * @return static [] An array of Stringy objects |
||
| 1006 | 10 | */ |
|
| 1007 | 10 | public function lines() |
|
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Returns the longest common prefix between the string and $otherStr. |
||
| 1021 | * |
||
| 1022 | * @param string $otherStr Second string for comparison |
||
| 1023 | * |
||
| 1024 | * @return static Object with its $str being the longest common prefix |
||
| 1025 | */ |
||
| 1026 | public function longestCommonPrefix($otherStr) |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Returns the longest common suffix between the string and $otherStr. |
||
| 1047 | * |
||
| 1048 | * @param string $otherStr Second string for comparison |
||
| 1049 | * |
||
| 1050 | * @return static Object with its $str being the longest common suffix |
||
| 1051 | */ |
||
| 1052 | public function longestCommonSuffix($otherStr) |
||
| 1070 | 8 | ||
| 1071 | 8 | /** |
|
| 1072 | 8 | * Returns the longest common substring between the string and $otherStr. |
|
| 1073 | 8 | * In the case of ties, it returns that which occurs first. |
|
| 1074 | 8 | * |
|
| 1075 | 8 | * @param string $otherStr Second string for comparison |
|
| 1076 | * |
||
| 1077 | 8 | * @return static Object with its $str being the longest common substring |
|
| 1078 | 8 | */ |
|
| 1079 | 8 | public function longestCommonSubstring($otherStr) |
|
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Returns whether or not a character exists at an index. Offsets may be |
||
| 1126 | * negative to count from the last character in the string. Implements |
||
| 1127 | * part of the ArrayAccess interface. |
||
| 1128 | * |
||
| 1129 | * @param mixed $offset The index to check |
||
| 1130 | * |
||
| 1131 | * @return boolean Whether or not the index exists |
||
| 1132 | */ |
||
| 1133 | 2 | public function offsetExists($offset) |
|
| 1145 | |||
| 1146 | /** |
||
| 1147 | 1 | * Returns the character at the given index. Offsets may be negative to |
|
| 1148 | * count from the last character in the string. Implements part of the |
||
| 1149 | * ArrayAccess interface, and throws an OutOfBoundsException if the index |
||
| 1150 | * does not exist. |
||
| 1151 | * |
||
| 1152 | * @param mixed $offset The index from which to retrieve the char |
||
| 1153 | * |
||
| 1154 | * @return string The character at the specified index |
||
| 1155 | * @throws \OutOfBoundsException If the positive or negative offset does |
||
| 1156 | * not exist |
||
| 1157 | */ |
||
| 1158 | public function offsetGet($offset) |
||
| 1174 | |||
| 1175 | /** |
||
| 1176 | 1 | * Implements part of the ArrayAccess interface, but throws an exception |
|
| 1177 | * when called. This maintains the immutability of Stringy objects. |
||
| 1178 | * |
||
| 1179 | * @param mixed $offset The index of the character |
||
| 1180 | * @param mixed $value Value to set |
||
| 1181 | * |
||
| 1182 | * @throws \Exception When called |
||
| 1183 | */ |
||
| 1184 | public function offsetSet($offset, $value) |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Implements part of the ArrayAccess interface, but throws an exception |
||
| 1192 | * when called. This maintains the immutability of Stringy objects. |
||
| 1193 | 13 | * |
|
| 1194 | * @param mixed $offset The index of the character |
||
| 1195 | 13 | * |
|
| 1196 | 1 | * @throws \Exception When called |
|
| 1197 | */ |
||
| 1198 | 1 | public function offsetUnset($offset) |
|
| 1203 | 3 | ||
| 1204 | 9 | /** |
|
| 1205 | 6 | * Pads the string to a given length with $padStr. If length is less than |
|
| 1206 | 3 | * or equal to the length of the string, no padding takes places. The |
|
| 1207 | 3 | * default string used for padding is a space, and the default type (one of |
|
| 1208 | 3 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
|
| 1209 | * if $padType isn't one of those 3 values. |
||
| 1210 | * |
||
| 1211 | * @param int $length Desired string length after padding |
||
| 1212 | * @param string $padStr String used to pad, defaults to space |
||
| 1213 | * @param string $padType One of 'left', 'right', 'both' |
||
| 1214 | * |
||
| 1215 | * @return static Object with a padded $str |
||
| 1216 | * @throws \InvalidArgumentException If $padType isn't one of 'right', 'left' or 'both' |
||
| 1217 | */ |
||
| 1218 | public function pad($length, $padStr = ' ', $padType = 'right') |
||
| 1235 | 37 | ||
| 1236 | /** |
||
| 1237 | 37 | * Returns a new string of a given length such that the beginning of the |
|
| 1238 | * string is padded. Alias for pad() with a $padType of 'left'. |
||
| 1239 | 37 | * |
|
| 1240 | * @param int $length Desired string length after padding |
||
| 1241 | 37 | * @param string $padStr String used to pad, defaults to space |
|
| 1242 | 37 | * |
|
| 1243 | * @return static String with left padding |
||
| 1244 | 37 | */ |
|
| 1245 | 3 | public function padLeft($length, $padStr = ' ') |
|
| 1249 | 34 | ||
| 1250 | 34 | /** |
|
| 1251 | 34 | * Adds the specified amount of left and right padding to the given string. |
|
| 1252 | 34 | * The default character used is a space. |
|
| 1253 | 34 | * |
|
| 1254 | 34 | * @param int $left Length of left padding |
|
| 1255 | 34 | * @param int $right Length of right padding |
|
| 1256 | 34 | * @param string $padStr String used to pad |
|
| 1257 | * |
||
| 1258 | 34 | * @return static String with padding applied |
|
| 1259 | 34 | */ |
|
| 1260 | 34 | protected function applyPadding($left = 0, $right = 0, $padStr = ' ') |
|
| 1297 | |||
| 1298 | 14 | /** |
|
| 1299 | * Returns a new string of a given length such that the end of the string |
||
| 1300 | 14 | * is padded. Alias for pad() with a $padType of 'right'. |
|
| 1301 | * |
||
| 1302 | * @param int $length Desired string length after padding |
||
| 1303 | * @param string $padStr String used to pad, defaults to space |
||
| 1304 | * |
||
| 1305 | * @return static String with right padding |
||
| 1306 | */ |
||
| 1307 | public function padRight($length, $padStr = ' ') |
||
| 1311 | |||
| 1312 | 2 | /** |
|
| 1313 | * Returns a new string of a given length such that both sides of the |
||
| 1314 | * string are padded. Alias for pad() with a $padType of 'both'. |
||
| 1315 | * |
||
| 1316 | * @param int $length Desired string length after padding |
||
| 1317 | * @param string $padStr String used to pad, defaults to space |
||
| 1318 | * |
||
| 1319 | * @return static String with padding applied |
||
| 1320 | */ |
||
| 1321 | public function padBoth($length, $padStr = ' ') |
||
| 1327 | 6 | ||
| 1328 | /** |
||
| 1329 | 6 | * Returns a new string starting with $string. |
|
| 1330 | * |
||
| 1331 | * @param string $string The string to append |
||
| 1332 | 6 | * |
|
| 1333 | * @return static Object with appended $string |
||
| 1334 | */ |
||
| 1335 | public function prepend($string) |
||
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Returns a new string with the prefix $substring removed, if present. |
||
| 1342 | 12 | * |
|
| 1343 | * @param string $substring The prefix to remove |
||
| 1344 | 12 | * |
|
| 1345 | * @return static Object having a $str without the prefix $substring |
||
| 1346 | 12 | */ |
|
| 1347 | 8 | View Code Duplication | public function removeLeft($substring) |
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Returns a new string with the suffix $substring removed, if present. |
||
| 1362 | 7 | * |
|
| 1363 | * @param string $substring The suffix to remove |
||
| 1364 | 7 | * |
|
| 1365 | * @return static Object having a $str without the suffix $substring |
||
| 1366 | 7 | */ |
|
| 1367 | View Code Duplication | public function removeRight($substring) |
|
| 1379 | |||
| 1380 | 28 | /** |
|
| 1381 | 21 | * Returns a repeated string given a multiplier. |
|
| 1382 | 21 | * |
|
| 1383 | 7 | * @param int $multiplier The number of times to repeat the string |
|
| 1384 | * |
||
| 1385 | * @return static Object with a repeated str |
||
| 1386 | 28 | */ |
|
| 1387 | public function repeat($multiplier) |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * Replaces all occurrences of $search in $str by $replacement. |
||
| 1396 | * |
||
| 1397 | * @param string $search The needle to search for |
||
| 1398 | 30 | * @param string $replacement The string to replace with |
|
| 1399 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 1400 | 30 | * |
|
| 1401 | 23 | * @return static Object with the resulting $str after the replacements |
|
| 1402 | 23 | */ |
|
| 1403 | 7 | View Code Duplication | public function replace($search, $replacement, $caseSensitive = true) |
| 1413 | |||
| 1414 | /** |
||
| 1415 | * Replaces all occurrences of $search in $str by $replacement. |
||
| 1416 | * |
||
| 1417 | 16 | * @param array $search The elements to search for |
|
| 1418 | * @param string|array $replacement The string to replace with |
||
| 1419 | 16 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
|
| 1420 | * |
||
| 1421 | 16 | * @return static Object with the resulting $str after the replacements |
|
| 1422 | */ |
||
| 1423 | View Code Duplication | public function replaceAll(array $search, $replacement, $caseSensitive = true) |
|
| 1433 | |||
| 1434 | 16 | /** |
|
| 1435 | * Replaces all occurrences of $search from the beginning of string with $replacement |
||
| 1436 | 16 | * |
|
| 1437 | * @param string $search |
||
| 1438 | * @param string $replacement |
||
| 1439 | * |
||
| 1440 | * @return static Object with the resulting $str after the replacements |
||
| 1441 | */ |
||
| 1442 | public function replaceBeginning($search, $replacement) |
||
| 1448 | 5 | ||
| 1449 | /** |
||
| 1450 | * Replaces all occurrences of $search from the ending of string with $replacement |
||
| 1451 | * |
||
| 1452 | * @param string $search |
||
| 1453 | * @param string $replacement |
||
| 1454 | * |
||
| 1455 | * @return static Object with the resulting $str after the replacements |
||
| 1456 | */ |
||
| 1457 | public function replaceEnding($search, $replacement) |
||
| 1463 | |||
| 1464 | 23 | /** |
|
| 1465 | 23 | * Returns a reversed string. A multibyte version of strrev(). |
|
| 1466 | 4 | * |
|
| 1467 | * @return static Object with a reversed $str |
||
| 1468 | */ |
||
| 1469 | public function reverse() |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | 19 | * Truncates the string to a given length, while ensuring that it does not |
|
| 1478 | 19 | * split words. If $substring is provided, and truncating occurs, the |
|
| 1479 | * string is further truncated so that the substring may be appended without |
||
| 1480 | 12 | * exceeding the desired length. |
|
| 1481 | * |
||
| 1482 | 12 | * @param int $length Desired length of the truncated string |
|
| 1483 | 11 | * @param string $substring The substring to append if it can fit |
|
| 1484 | 11 | * |
|
| 1485 | 12 | * @return static Object with the resulting $str after truncating |
|
| 1486 | */ |
||
| 1487 | 19 | public function safeTruncate($length, $substring = '') |
|
| 1516 | |||
| 1517 | /** |
||
| 1518 | 15 | * A multibyte string shuffle function. It returns a string with its |
|
| 1519 | * characters in random order. |
||
| 1520 | 15 | * |
|
| 1521 | * @return static Object with a shuffled $str |
||
| 1522 | 15 | */ |
|
| 1523 | public function shuffle() |
||
| 1529 | |||
| 1530 | 1 | /** |
|
| 1531 | * Converts the string into an URL slug. This includes replacing non-ASCII |
||
| 1532 | 1 | * characters with their closest ASCII equivalents, removing remaining |
|
| 1533 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
||
| 1534 | 1 | * $replacement. The replacement defaults to a single dash, and the string |
|
| 1535 | * is also converted to lowercase. |
||
| 1536 | * |
||
| 1537 | * @param string $replacement The string used to replace whitespace |
||
| 1538 | * @param string $language The language for the url |
||
| 1539 | * @param bool $strToLower string to lower |
||
| 1540 | * |
||
| 1541 | * @return static Object whose $str has been converted to an URL slug |
||
| 1542 | */ |
||
| 1543 | public function slugify($replacement = '-', $language = 'de', $strToLower = true) |
||
| 1549 | |||
| 1550 | /** |
||
| 1551 | * Remove css media-queries. |
||
| 1552 | * |
||
| 1553 | * @return static |
||
| 1554 | */ |
||
| 1555 | public function stripeCssMediaQueries() |
||
| 1561 | |||
| 1562 | /** |
||
| 1563 | * Strip all whitespace characters. This includes tabs and newline characters, |
||
| 1564 | * as well as multibyte whitespace such as the thin space and ideographic space. |
||
| 1565 | * |
||
| 1566 | 6 | * @return Stringy |
|
| 1567 | */ |
||
| 1568 | 6 | public function stripWhitespace() |
|
| 1572 | 6 | ||
| 1573 | /** |
||
| 1574 | 6 | * Remove empty html-tag. |
|
| 1575 | * |
||
| 1576 | * e.g.: <tag></tag> |
||
| 1577 | * |
||
| 1578 | * @return static |
||
| 1579 | */ |
||
| 1580 | public function stripeEmptyHtmlTags() |
||
| 1586 | 1 | ||
| 1587 | /** |
||
| 1588 | * Converts the string into an valid UTF-8 string. |
||
| 1589 | 1 | * |
|
| 1590 | * @return static |
||
| 1591 | 1 | */ |
|
| 1592 | 1 | public function utf8ify() |
|
| 1596 | |||
| 1597 | 1 | /** |
|
| 1598 | 1 | * escape html |
|
| 1599 | 1 | * |
|
| 1600 | * @return static |
||
| 1601 | 1 | */ |
|
| 1602 | public function escape() |
||
| 1612 | |||
| 1613 | /** |
||
| 1614 | * Create an extract from a text, so if the search-string was found, it will be centered in the output. |
||
| 1615 | * |
||
| 1616 | * @param string $search |
||
| 1617 | * @param int|null $length |
||
| 1618 | * @param string $ellipsis |
||
| 1619 | * |
||
| 1620 | * @return static |
||
| 1621 | 1 | */ |
|
| 1622 | 1 | public function extractText($search = '', $length = null, $ellipsis = '...') |
|
| 1739 | |||
| 1740 | |||
| 1741 | /** |
||
| 1742 | * remove xss from html |
||
| 1743 | * |
||
| 1744 | 6 | * @return static |
|
| 1745 | */ |
||
| 1746 | 6 | public function removeXss() |
|
| 1758 | |||
| 1759 | /** |
||
| 1760 | * remove html-break [br | \r\n | \r | \n | ...] |
||
| 1761 | * |
||
| 1762 | 18 | * @param string $replacement |
|
| 1763 | * |
||
| 1764 | 18 | * @return static |
|
| 1765 | 4 | */ |
|
| 1766 | 18 | public function removeHtmlBreak($replacement = '') |
|
| 1772 | |||
| 1773 | /** |
||
| 1774 | 14 | * remove html |
|
| 1775 | * |
||
| 1776 | 14 | * @param $allowableTags |
|
| 1777 | * |
||
| 1778 | * @return static |
||
| 1779 | */ |
||
| 1780 | public function removeHtml($allowableTags = null) |
||
| 1786 | |||
| 1787 | /** |
||
| 1788 | * Returns the substring beginning at $start, and up to, but not including |
||
| 1789 | 19 | * the index specified by $end. If $end is omitted, the function extracts |
|
| 1790 | * the remaining string. If $end is negative, it is computed from the end |
||
| 1791 | 19 | * of the string. |
|
| 1792 | 2 | * |
|
| 1793 | * @param int $start Initial index from which to begin extraction |
||
| 1794 | * @param int $end Optional index at which to end extraction |
||
| 1795 | * |
||
| 1796 | * @return static Object with its $str being the extracted substring |
||
| 1797 | 17 | */ |
|
| 1798 | 1 | public function slice($start, $end = null) |
|
| 1814 | |||
| 1815 | /** |
||
| 1816 | * Splits the string with the provided regular expression, returning an |
||
| 1817 | 16 | * array of Stringy objects. An optional integer $limit will truncate the |
|
| 1818 | 16 | * results. |
|
| 1819 | 16 | * |
|
| 1820 | * @param string $pattern The regex with which to split the string |
||
| 1821 | 16 | * @param int $limit Optional maximum number of results to return |
|
| 1822 | * |
||
| 1823 | * @return static [] An array of Stringy objects |
||
| 1824 | */ |
||
| 1825 | public function split($pattern, $limit = null) |
||
| 1859 | |||
| 1860 | 4 | /** |
|
| 1861 | * Surrounds $str with the given substring. |
||
| 1862 | 4 | * |
|
| 1863 | * @param string $substring The substring to add to both sides |
||
| 1864 | 4 | * |
|
| 1865 | * @return static Object whose $str had the substring both prepended and |
||
| 1866 | * appended |
||
| 1867 | */ |
||
| 1868 | public function surround($substring) |
||
| 1874 | |||
| 1875 | /** |
||
| 1876 | 5 | * Returns a case swapped version of the string. |
|
| 1877 | * |
||
| 1878 | 5 | * @return static Object whose $str has each character's case swapped |
|
| 1879 | 5 | */ |
|
| 1880 | public function swapCase() |
||
| 1888 | |||
| 1889 | 5 | /** |
|
| 1890 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
||
| 1891 | 5 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
|
| 1892 | 5 | * equivalents. |
|
| 1893 | 5 | * |
|
| 1894 | * @return static Object whose $str has those characters removed |
||
| 1895 | 5 | */ |
|
| 1896 | public function tidy() |
||
| 1902 | |||
| 1903 | /** |
||
| 1904 | 27 | * Returns a trimmed string with the first letter of each word capitalized. |
|
| 1905 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
| 1906 | 27 | * capitalized. |
|
| 1907 | * |
||
| 1908 | 27 | * @param array $ignore An array of words not to capitalize |
|
| 1909 | * |
||
| 1910 | * @return static Object with a titleized $str |
||
| 1911 | */ |
||
| 1912 | public function titleize($ignore = null) |
||
| 1933 | |||
| 1934 | 16 | /** |
|
| 1935 | * Converts all characters in the string to lowercase. An alias for PHP's |
||
| 1936 | * UTF8::strtolower(). |
||
| 1937 | * |
||
| 1938 | * @return static Object with all characters of $str being lowercase |
||
| 1939 | */ |
||
| 1940 | public function toLowerCase() |
||
| 1946 | |||
| 1947 | /** |
||
| 1948 | 15 | * Returns true if the string is base64 encoded, false otherwise. |
|
| 1949 | * |
||
| 1950 | 15 | * @return bool Whether or not $str is base64 encoded |
|
| 1951 | */ |
||
| 1952 | 15 | public function isBase64() |
|
| 1956 | 15 | ||
| 1957 | 15 | /** |
|
| 1958 | 15 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
|
| 1959 | 15 | * replaced with their closest ASCII counterparts, and the rest are removed |
|
| 1960 | 15 | * unless instructed otherwise. |
|
| 1961 | * |
||
| 1962 | 15 | * @param $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance</p> |
|
| 1963 | 10 | * |
|
| 1964 | 5 | * @return static Object whose $str contains only ASCII characters |
|
| 1965 | 2 | */ |
|
| 1966 | public function toAscii($strict = false) |
||
| 1972 | |||
| 1973 | /** |
||
| 1974 | * Returns a boolean representation of the given logical string value. |
||
| 1975 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
| 1976 | 993 | * 'off', and 'no' will return false. In all instances, case is ignored. |
|
| 1977 | * For other numeric strings, their sign will determine the return value. |
||
| 1978 | 993 | * In addition, blank strings consisting of only whitespace will return |
|
| 1979 | * false. For all other strings, the return value is a result of a |
||
| 1980 | * boolean cast. |
||
| 1981 | * |
||
| 1982 | * @return bool A boolean value for the string |
||
| 1983 | */ |
||
| 1984 | public function toBoolean() |
||
| 2006 | 5 | ||
| 2007 | /** |
||
| 2008 | 5 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
|
| 2009 | 5 | * |
|
| 2010 | * @return string |
||
| 2011 | 5 | */ |
|
| 2012 | public function toString() |
||
| 2016 | |||
| 2017 | /** |
||
| 2018 | * Converts each tab in the string to some number of spaces, as defined by |
||
| 2019 | 5 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
|
| 2020 | * |
||
| 2021 | * @param int $tabLength Number of spaces to replace each tab with |
||
| 2022 | 5 | * |
|
| 2023 | * @return static Object whose $str has had tabs switched to spaces |
||
| 2024 | 5 | */ |
|
| 2025 | View Code Duplication | public function toSpaces($tabLength = 4) |
|
| 2032 | |||
| 2033 | 5 | /** |
|
| 2034 | * Converts each occurrence of some consecutive number of spaces, as |
||
| 2035 | 5 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
|
| 2036 | * are converted to a tab. |
||
| 2037 | 5 | * |
|
| 2038 | * @param int $tabLength Number of spaces to replace with a tab |
||
| 2039 | * |
||
| 2040 | * @return static Object whose $str has had spaces switched to tabs |
||
| 2041 | */ |
||
| 2042 | View Code Duplication | public function toTabs($tabLength = 4) |
|
| 2049 | 13 | ||
| 2050 | /** |
||
| 2051 | 13 | * Converts the first character of each word in the string to uppercase. |
|
| 2052 | 11 | * |
|
| 2053 | 11 | * @return static Object with all characters of $str being title-cased |
|
| 2054 | 2 | */ |
|
| 2055 | public function toTitleCase() |
||
| 2062 | |||
| 2063 | /** |
||
| 2064 | * Converts all characters in the string to uppercase. An alias for PHP's |
||
| 2065 | * UTF8::strtoupper(). |
||
| 2066 | * |
||
| 2067 | * @return static Object with all characters of $str being uppercase |
||
| 2068 | */ |
||
| 2069 | 13 | public function toUpperCase() |
|
| 2075 | |||
| 2076 | /** |
||
| 2077 | 13 | * Returns a string with whitespace removed from the start of the string. |
|
| 2078 | * Supports the removal of unicode whitespace. Accepts an optional |
||
| 2079 | * string of characters to strip instead of the defaults. |
||
| 2080 | * |
||
| 2081 | * @param string $chars Optional string of characters to strip |
||
| 2082 | * |
||
| 2083 | * @return static Object with a trimmed $str |
||
| 2084 | */ |
||
| 2085 | View Code Duplication | public function trimLeft($chars = null) |
|
| 2095 | |||
| 2096 | /** |
||
| 2097 | * Returns a string with whitespace removed from the end of the string. |
||
| 2098 | 18 | * Supports the removal of unicode whitespace. Accepts an optional |
|
| 2099 | 18 | * string of characters to strip instead of the defaults. |
|
| 2100 | * |
||
| 2101 | 18 | * @param string $chars Optional string of characters to strip |
|
| 2102 | 18 | * |
|
| 2103 | * @return static Object with a trimmed $str |
||
| 2104 | 18 | */ |
|
| 2105 | View Code Duplication | public function trimRight($chars = null) |
|
| 2115 | 16 | ||
| 2116 | /** |
||
| 2117 | 16 | * Truncates the string to a given length. If $substring is provided, and |
|
| 2118 | * truncating occurs, the string is further truncated so that the substring |
||
| 2119 | * may be appended without exceeding the desired length. |
||
| 2120 | * |
||
| 2121 | * @param int $length Desired length of the truncated string |
||
| 2122 | * @param string $substring The substring to append if it can fit |
||
| 2123 | * |
||
| 2124 | * @return static Object with the resulting $str after truncating |
||
| 2125 | */ |
||
| 2126 | View Code Duplication | public function truncate($length, $substring = '') |
|
| 2142 | 32 | ||
| 2143 | 32 | /** |
|
| 2144 | * Returns a lowercase and trimmed string separated by underscores. |
||
| 2145 | 32 | * Underscores are inserted before uppercase characters (with the exception |
|
| 2146 | 32 | * of the first character of the string), and in place of spaces as well as |
|
| 2147 | * dashes. |
||
| 2148 | 27 | * |
|
| 2149 | 27 | * @return static Object with an underscored $str |
|
| 2150 | */ |
||
| 2151 | 1 | public function underscored() |
|
| 2155 | 32 | ||
| 2156 | /** |
||
| 2157 | 32 | * Returns an UpperCamelCase version of the supplied string. It trims |
|
| 2158 | 32 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
|
| 2159 | * and underscores, and removes spaces, dashes, underscores. |
||
| 2160 | 6 | * |
|
| 2161 | 32 | * @return static Object with $str in UpperCamelCase |
|
| 2162 | 32 | */ |
|
| 2163 | 32 | public function upperCamelize() |
|
| 2167 | |||
| 2168 | /** |
||
| 2169 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
| 2170 | * capitalizes letters following digits, spaces, dashes and underscores, |
||
| 2171 | * and removes spaces, dashes, as well as underscores. |
||
| 2172 | * |
||
| 2173 | 20 | * @return static Object with $str in camelCase |
|
| 2174 | */ |
||
| 2175 | 20 | public function camelize() |
|
| 2203 | |||
| 2204 | 20 | /** |
|
| 2205 | 20 | * Convert a string to e.g.: "snake_case" |
|
| 2206 | 20 | * |
|
| 2207 | 20 | * @return static Object with $str in snake_case |
|
| 2208 | */ |
||
| 2209 | 20 | public function snakeize() |
|
| 2252 | |||
| 2253 | /** |
||
| 2254 | 1 | * Converts the first character of the string to lower case. |
|
| 2255 | * |
||
| 2256 | 1 | * @return static Object with the first character of $str being lower case |
|
| 2257 | */ |
||
| 2258 | 1 | View Code Duplication | public function lowerCaseFirst() |
| 2267 | |||
| 2268 | /** |
||
| 2269 | * Shorten the string after $length, but also after the next word. |
||
| 2270 | * |
||
| 2271 | * @param int $length |
||
| 2272 | * @param string $strAddOn |
||
| 2273 | * |
||
| 2274 | * @return static |
||
| 2275 | 1 | */ |
|
| 2276 | public function shortenAfterWord($length, $strAddOn = '...') |
||
| 2282 | 1 | ||
| 2283 | 1 | /** |
|
| 2284 | 1 | * Line-Wrap the string after $limit, but also after the next word. |
|
| 2285 | 1 | * |
|
| 2286 | 1 | * @param int $limit |
|
| 2287 | 1 | * |
|
| 2288 | 1 | * @return static |
|
| 2289 | 1 | */ |
|
| 2290 | public function lineWrapAfterWord($limit) |
||
| 2302 | 1 | ||
| 2303 | 1 | /** |
|
| 2304 | 1 | * Gets the substring after the first occurrence of a separator. |
|
| 2305 | * If no match is found returns new empty Stringy object. |
||
| 2306 | * |
||
| 2307 | 1 | * @param string $separator |
|
| 2308 | 1 | * |
|
| 2309 | 1 | * @return static |
|
| 2310 | 1 | */ |
|
| 2311 | 1 | View Code Duplication | public function afterFirst($separator) |
| 2327 | |||
| 2328 | 1 | /** |
|
| 2329 | 1 | * Gets the substring after the last occurrence of a separator. |
|
| 2330 | 1 | * If no match is found returns new empty Stringy object. |
|
| 2331 | * |
||
| 2332 | * @param string $separator |
||
| 2333 | 1 | * |
|
| 2334 | 1 | * @return static |
|
| 2335 | 1 | */ |
|
| 2336 | 1 | public function afterLast($separator) |
|
| 2353 | |||
| 2354 | 1 | /** |
|
| 2355 | 1 | * Gets the substring before the first occurrence of a separator. |
|
| 2356 | 1 | * If no match is found returns new empty Stringy object. |
|
| 2357 | * |
||
| 2358 | * @param string $separator |
||
| 2359 | 1 | * |
|
| 2360 | 1 | * @return static |
|
| 2361 | 1 | */ |
|
| 2362 | 1 | View Code Duplication | public function beforeFirst($separator) |
| 2379 | 39 | ||
| 2380 | 39 | /** |
|
| 2381 | * Gets the substring before the last occurrence of a separator. |
||
| 2382 | 39 | * If no match is found returns new empty Stringy object. |
|
| 2383 | * |
||
| 2384 | * @param string $separator |
||
| 2385 | * |
||
| 2386 | * @return static |
||
| 2387 | */ |
||
| 2388 | View Code Duplication | public function beforeLast($separator) |
|
| 2405 | |||
| 2406 | /** |
||
| 2407 | * Returns the string with the first letter of each word capitalized, |
||
| 2408 | * except for when the word is a name which shouldn't be capitalized. |
||
| 2409 | 39 | * |
|
| 2410 | * @return static Object with $str capitalized |
||
| 2411 | */ |
||
| 2412 | 39 | public function capitalizePersonalName() |
|
| 2420 | 39 | ||
| 2421 | 39 | /** |
|
| 2422 | 39 | * @param string $word |
|
| 2423 | 39 | * |
|
| 2424 | 39 | * @return string |
|
| 2425 | 39 | */ |
|
| 2426 | 39 | protected function capitalizeWord($word) |
|
| 2436 | 39 | ||
| 2437 | 39 | /** |
|
| 2438 | 39 | * Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius"). |
|
| 2439 | 39 | * |
|
| 2440 | 39 | * @param string $names |
|
| 2441 | 39 | * @param string $delimiter |
|
| 2442 | 39 | * |
|
| 2443 | 39 | * @return string |
|
| 2444 | */ |
||
| 2445 | 39 | protected function capitalizePersonalNameByDelimiter($names, $delimiter) |
|
| 2521 | } |
||
| 2522 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: