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) |
|
| 86 | 163 | ||
| 87 | /** |
||
| 88 | 163 | * Returns the value in $str. |
|
| 89 | * |
||
| 90 | * @return string The current value of the $str property |
||
| 91 | */ |
||
| 92 | public function __toString() |
||
| 96 | |||
| 97 | /** |
||
| 98 | 5 | * Returns a new string with $string appended. |
|
| 99 | * |
||
| 100 | 5 | * @param string $string The string to append |
|
| 101 | * |
||
| 102 | * @return static Object with appended $string |
||
| 103 | */ |
||
| 104 | public function append($string) |
||
| 108 | |||
| 109 | /** |
||
| 110 | 1 | * Append an password (limited to chars that are good readable). |
|
| 111 | * |
||
| 112 | 1 | * @param int $length length of the random string |
|
| 113 | * |
||
| 114 | 1 | * @return static Object with appended password |
|
| 115 | */ |
||
| 116 | public function appendPassword($length) |
||
| 122 | |||
| 123 | /** |
||
| 124 | 1 | * Append an unique identifier. |
|
| 125 | * |
||
| 126 | 1 | * @param string|int $extraPrefix |
|
| 127 | 1 | * |
|
| 128 | 1 | * @return static Object with appended unique identifier as md5-hash |
|
| 129 | 1 | */ |
|
| 130 | 1 | public function appendUniqueIdentifier($extraPrefix = '') |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Append an random string. |
||
| 143 | 2 | * |
|
| 144 | * @param int $length length of the random string |
||
| 145 | * @param string $possibleChars characters string for the random selection |
||
| 146 | 2 | * |
|
| 147 | 2 | * @return static Object with appended random string |
|
| 148 | 2 | */ |
|
| 149 | 2 | public function appendRandomString($length, $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Creates a Stringy object and assigns both str and encoding properties |
||
| 173 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
| 174 | * $encoding is not specified, it defaults to mb_internal_encoding(). It |
||
| 175 | * then returns the initialized object. Throws an InvalidArgumentException |
||
| 176 | * if the first argument is an array or object without a __toString method. |
||
| 177 | * |
||
| 178 | * @param mixed $str Value to modify, after being cast to string |
||
| 179 | 1076 | * @param string $encoding The character encoding |
|
| 180 | * |
||
| 181 | 1076 | * @return static A Stringy object |
|
| 182 | * @throws \InvalidArgumentException if an array or object without a |
||
| 183 | * __toString method is passed as the first argument |
||
| 184 | */ |
||
| 185 | public static function create($str = '', $encoding = null) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns the substring between $start and $end, if found, or an empty |
||
| 192 | * string. An optional offset may be supplied from which to begin the |
||
| 193 | * search for the start string. |
||
| 194 | * |
||
| 195 | 16 | * @param string $start Delimiter marking the start of the substring |
|
| 196 | * @param string $end Delimiter marking the end of the substring |
||
| 197 | 16 | * @param int $offset Index from which to begin the search |
|
| 198 | 16 | * |
|
| 199 | 2 | * @return static Object whose $str is a substring between $start and $end |
|
| 200 | */ |
||
| 201 | public function between($start, $end, $offset = 0) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns the index of the first occurrence of $needle in the string, |
||
| 219 | * and false if not found. Accepts an optional offset from which to begin |
||
| 220 | * the search. |
||
| 221 | 28 | * |
|
| 222 | * @param string $needle Substring to look for |
||
| 223 | 28 | * @param int $offset Offset from which to search |
|
| 224 | * |
||
| 225 | * @return int|bool The occurrence's index if found, otherwise false |
||
| 226 | */ |
||
| 227 | public function indexOf($needle, $offset = 0) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the substring beginning at $start with the specified $length. |
||
| 234 | * It differs from the UTF8::substr() function in that providing a $length of |
||
| 235 | * null will return the rest of the string, rather than an empty string. |
||
| 236 | 64 | * |
|
| 237 | * @param int $start Position of the first character to use |
||
| 238 | 64 | * @param int $length Maximum number of characters used |
|
| 239 | 19 | * |
|
| 240 | 19 | * @return static Object with its $str being the substring |
|
| 241 | */ |
||
| 242 | 64 | public function substr($start, $length = null) |
|
| 252 | 248 | ||
| 253 | /** |
||
| 254 | 248 | * Returns the length of the string. |
|
| 255 | * |
||
| 256 | * @return int The number of characters in $str given the encoding |
||
| 257 | */ |
||
| 258 | public function length() |
||
| 262 | |||
| 263 | /** |
||
| 264 | 52 | * Trims the string and replaces consecutive whitespace characters with a |
|
| 265 | * single space. This includes tabs and newline characters, as well as |
||
| 266 | 52 | * multibyte whitespace such as the thin space and ideographic space. |
|
| 267 | * |
||
| 268 | * @return static Object with a trimmed $str and condensed whitespace |
||
| 269 | */ |
||
| 270 | public function collapseWhitespace() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns a string with whitespace removed from the start and end of the |
||
| 277 | * string. Supports the removal of unicode whitespace. Accepts an optional |
||
| 278 | 153 | * string of characters to strip instead of the defaults. |
|
| 279 | * |
||
| 280 | 153 | * @param string $chars Optional string of characters to strip |
|
| 281 | 152 | * |
|
| 282 | 152 | * @return static Object with a trimmed $str |
|
| 283 | 1 | */ |
|
| 284 | View Code Duplication | public function trim($chars = null) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
| 297 | * |
||
| 298 | 223 | * @param string $pattern The regular expression pattern |
|
| 299 | * @param string $replacement The string to replace with |
||
| 300 | 223 | * @param string $options Matching conditions to be used |
|
| 301 | 8 | * |
|
| 302 | 8 | * @return static Object with the result2ing $str after the replacements |
|
| 303 | */ |
||
| 304 | 223 | public function regexReplace($pattern, $replacement, $options = '') |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Returns true if the string contains all $needles, false otherwise. By |
||
| 321 | * default the comparison is case-sensitive, but can be made insensitive by |
||
| 322 | * setting $caseSensitive to false. |
||
| 323 | 43 | * |
|
| 324 | * @param array $needles SubStrings to look for |
||
| 325 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 326 | 43 | * |
|
| 327 | 1 | * @return bool Whether or not $str contains $needle |
|
| 328 | */ |
||
| 329 | View Code Duplication | public function containsAll($needles, $caseSensitive = true) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Returns true if the string contains $needle, false otherwise. By default |
||
| 347 | * the comparison is case-sensitive, but can be made insensitive by setting |
||
| 348 | * $caseSensitive to false. |
||
| 349 | 105 | * |
|
| 350 | * @param string $needle Substring to look for |
||
| 351 | 105 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
|
| 352 | * |
||
| 353 | 105 | * @return bool Whether or not $str contains $needle |
|
| 354 | 55 | */ |
|
| 355 | public function contains($needle, $caseSensitive = true) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Returns true if the string contains any $needles, false otherwise. By |
||
| 368 | * default the comparison is case-sensitive, but can be made insensitive by |
||
| 369 | * setting $caseSensitive to false. |
||
| 370 | 43 | * |
|
| 371 | * @param array $needles SubStrings to look for |
||
| 372 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 373 | 43 | * |
|
| 374 | 1 | * @return bool Whether or not $str contains $needle |
|
| 375 | */ |
||
| 376 | View Code Duplication | public function containsAny($needles, $caseSensitive = true) |
|
| 391 | 1 | ||
| 392 | /** |
||
| 393 | 1 | * Returns the length of the string, implementing the countable interface. |
|
| 394 | * |
||
| 395 | * @return int The number of characters in the string, given the encoding |
||
| 396 | */ |
||
| 397 | public function count() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Returns the number of occurrences of $substring in the given string. |
||
| 404 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
| 405 | * by setting $caseSensitive to false. |
||
| 406 | 15 | * |
|
| 407 | * @param string $substring The substring to search for |
||
| 408 | 15 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
|
| 409 | 9 | * |
|
| 410 | * @return int The number of $substring occurrences |
||
| 411 | */ |
||
| 412 | 6 | public function countSubstr($substring, $caseSensitive = true) |
|
| 423 | |||
| 424 | /** |
||
| 425 | 19 | * Returns a lowercase and trimmed string separated by dashes. Dashes are |
|
| 426 | * inserted before uppercase characters (with the exception of the first |
||
| 427 | 19 | * character of the string), and in place of spaces as well as underscores. |
|
| 428 | * |
||
| 429 | * @return static Object with a dasherized $str |
||
| 430 | */ |
||
| 431 | public function dasherize() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns a lowercase and trimmed string separated by the given delimiter. |
||
| 438 | * Delimiters are inserted before uppercase characters (with the exception |
||
| 439 | * of the first character of the string), and in place of spaces, dashes, |
||
| 440 | 49 | * and underscores. Alpha delimiters are not converted to lowercase. |
|
| 441 | * |
||
| 442 | 49 | * @param string $delimiter Sequence used to separate parts of the string |
|
| 443 | * |
||
| 444 | 49 | * @return static Object with a delimited $str |
|
| 445 | */ |
||
| 446 | 49 | public function delimit($delimiter) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Ensures that the string begins with $substring. If it doesn't, it's |
||
| 461 | 10 | * prepended. |
|
| 462 | * |
||
| 463 | 10 | * @param string $substring The substring to add if not present |
|
| 464 | * |
||
| 465 | 10 | * @return static Object with its $str prefixed by the $substring |
|
| 466 | 4 | */ |
|
| 467 | 4 | View Code Duplication | public function ensureLeft($substring) |
| 477 | |||
| 478 | /** |
||
| 479 | * Returns true if the string begins with $substring, false otherwise. By |
||
| 480 | * default, the comparison is case-sensitive, but can be made insensitive |
||
| 481 | * by setting $caseSensitive to false. |
||
| 482 | 45 | * |
|
| 483 | * @param string $substring The substring to look for |
||
| 484 | 45 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
|
| 485 | * |
||
| 486 | 45 | * @return bool Whether or not $str starts with $substring |
|
| 487 | 8 | */ |
|
| 488 | 8 | public function startsWith($substring, $caseSensitive = true) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Returns true if the string begins with any of $substrings, false otherwise. |
||
| 502 | * By default the comparison is case-sensitive, but can be made insensitive by |
||
| 503 | * setting $caseSensitive to false. |
||
| 504 | 12 | * |
|
| 505 | * @param array $substrings Substrings to look for |
||
| 506 | 12 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
|
| 507 | * |
||
| 508 | * @return bool Whether or not $str starts with $substring |
||
| 509 | */ |
||
| 510 | 12 | View Code Duplication | public function startsWithAny(array $substrings, $caseSensitive = true) |
| 524 | |||
| 525 | /** |
||
| 526 | * Ensures that the string ends with $substring. If it doesn't, it's |
||
| 527 | 10 | * appended. |
|
| 528 | * |
||
| 529 | 10 | * @param string $substring The substring to add if not present |
|
| 530 | * |
||
| 531 | 10 | * @return static Object with its $str suffixed by the $substring |
|
| 532 | 4 | */ |
|
| 533 | 4 | View Code Duplication | public function ensureRight($substring) |
| 543 | |||
| 544 | /** |
||
| 545 | * Returns true if the string ends with $substring, false otherwise. By |
||
| 546 | * default, the comparison is case-sensitive, but can be made insensitive |
||
| 547 | * by setting $caseSensitive to false. |
||
| 548 | 33 | * |
|
| 549 | * @param string $substring The substring to look for |
||
| 550 | 33 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
|
| 551 | 33 | * |
|
| 552 | * @return bool Whether or not $str ends with $substring |
||
| 553 | 33 | */ |
|
| 554 | 33 | public function endsWith($substring, $caseSensitive = true) |
|
| 573 | |||
| 574 | /** |
||
| 575 | 12 | * Returns true if the string ends with any of $substrings, false otherwise. |
|
| 576 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
| 577 | 12 | * by setting $caseSensitive to false. |
|
| 578 | * |
||
| 579 | 12 | * @param string[] $substrings Substrings to look for |
|
| 580 | 2 | * @param bool $caseSensitive Whether or not to enforce |
|
| 581 | 2 | * case-sensitivity |
|
| 582 | 10 | * @return bool Whether or not $str ends with $substring |
|
| 583 | */ |
||
| 584 | View Code Duplication | public function endsWithAny($substrings, $caseSensitive = true) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Returns the first $n characters of the string. |
||
| 601 | * |
||
| 602 | * @param int $n Number of characters to retrieve from the start |
||
| 603 | * |
||
| 604 | * @return static Object with its $str being the first $n chars |
||
| 605 | */ |
||
| 606 | 1 | View Code Duplication | public function first($n) |
| 618 | |||
| 619 | 4 | /** |
|
| 620 | 4 | * Returns the encoding used by the Stringy object. |
|
| 621 | * |
||
| 622 | 4 | * @return string The current value of the $encoding property |
|
| 623 | 3 | */ |
|
| 624 | 3 | public function getEncoding() |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
| 631 | * interface. The ArrayIterator's constructor is passed an array of chars |
||
| 632 | * in the multibyte string. This enables the use of foreach with instances |
||
| 633 | * of Stringy\Stringy. |
||
| 634 | * |
||
| 635 | * @return \ArrayIterator An iterator for the characters in the string |
||
| 636 | 11 | */ |
|
| 637 | public function getIterator() |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Returns an array consisting of the characters in the string. |
||
| 644 | * |
||
| 645 | * @return array An array of string chars |
||
| 646 | */ |
||
| 647 | 12 | public function chars() |
|
| 659 | 103 | ||
| 660 | /** |
||
| 661 | 103 | * Returns the character at $index, with indexes starting at 0. |
|
| 662 | 64 | * |
|
| 663 | * @param int $index Position of the character |
||
| 664 | 39 | * |
|
| 665 | * @return static The character at $index |
||
| 666 | */ |
||
| 667 | public function at($index) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Returns true if the string contains a lower case char, false |
||
| 674 | 12 | * otherwise. |
|
| 675 | * |
||
| 676 | 12 | * @return bool Whether or not the string contains a lower case character. |
|
| 677 | */ |
||
| 678 | public function hasLowerCase() |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Returns true if $str matches the supplied pattern, false otherwise. |
||
| 685 | * |
||
| 686 | 5 | * @param string $pattern Regex pattern to match against |
|
| 687 | * |
||
| 688 | 5 | * @return bool Whether or not $str matches the pattern |
|
| 689 | */ |
||
| 690 | 5 | protected function matchesPattern($pattern) |
|
| 698 | |||
| 699 | /** |
||
| 700 | 5 | * Returns true if the string contains an upper case char, false |
|
| 701 | * otherwise. |
||
| 702 | 5 | * |
|
| 703 | * @return bool Whether or not the string contains an upper case character. |
||
| 704 | 5 | */ |
|
| 705 | public function hasUpperCase() |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Convert all HTML entities to their applicable characters. |
||
| 712 | * |
||
| 713 | 3 | * @param int|null $flags Optional flags |
|
| 714 | * |
||
| 715 | 3 | * @return static Object with the resulting $str after being html decoded. |
|
| 716 | */ |
||
| 717 | 3 | public function htmlDecode($flags = ENT_COMPAT) |
|
| 723 | |||
| 724 | /** |
||
| 725 | 27 | * Convert all applicable characters to HTML entities. |
|
| 726 | * |
||
| 727 | 27 | * @param int|null $flags Optional flags |
|
| 728 | 27 | * |
|
| 729 | 27 | * @return static Object with the resulting $str after being html encoded. |
|
| 730 | 27 | */ |
|
| 731 | 27 | public function htmlEncode($flags = ENT_COMPAT) |
|
| 737 | 27 | ||
| 738 | /** |
||
| 739 | * Capitalizes the first word of the string, replaces underscores with |
||
| 740 | * spaces, and strips '_id'. |
||
| 741 | * |
||
| 742 | * @return static Object with a humanized $str |
||
| 743 | */ |
||
| 744 | public function humanize() |
||
| 750 | |||
| 751 | 12 | /** |
|
| 752 | * Converts the first character of the supplied string to upper case. |
||
| 753 | 12 | * |
|
| 754 | * @return static Object with the first character of $str being upper case |
||
| 755 | */ |
||
| 756 | View Code Duplication | public function upperCaseFirst() |
|
| 770 | |||
| 771 | 7 | /** |
|
| 772 | 7 | * Returns the index of the last occurrence of $needle in the string, |
|
| 773 | * and false if not found. Accepts an optional offset from which to begin |
||
| 774 | 7 | * the search. Offsets may be negative to count from the last character |
|
| 775 | * in the string. |
||
| 776 | 7 | * |
|
| 777 | * @param string $needle Substring to look for |
||
| 778 | * @param int $offset Offset from which to search |
||
| 779 | * |
||
| 780 | * @return int|bool The last occurrence's index if found, otherwise false |
||
| 781 | */ |
||
| 782 | public function indexOfLast($needle, $offset = 0) |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Inserts $substring into the string at the $index provided. |
||
| 789 | * |
||
| 790 | * @param string $substring String to be inserted |
||
| 791 | 13 | * @param int $index The index at which to insert the substring |
|
| 792 | * |
||
| 793 | 13 | * @return static Object with the resulting $str after the insertion |
|
| 794 | 1 | */ |
|
| 795 | View Code Duplication | public function insert($substring, $index) |
|
| 809 | 10 | ||
| 810 | /** |
||
| 811 | 10 | * Returns true if the string contains the $pattern, otherwise false. |
|
| 812 | * |
||
| 813 | * WARNING: Asterisks ("*") are translated into (".*") zero-or-more regular |
||
| 814 | * expression wildcards. |
||
| 815 | * |
||
| 816 | * @credit Originally from Laravel, thanks Taylor. |
||
| 817 | * |
||
| 818 | * @param string $pattern The string or pattern to match against. |
||
| 819 | * |
||
| 820 | * @return bool Whether or not we match the provided pattern. |
||
| 821 | */ |
||
| 822 | public function is($pattern) |
||
| 833 | 13 | ||
| 834 | /** |
||
| 835 | 13 | * Returns true if the string contains only alphabetic chars, false |
|
| 836 | * otherwise. |
||
| 837 | * |
||
| 838 | * @return bool Whether or not $str contains only alphabetic chars |
||
| 839 | */ |
||
| 840 | public function isAlpha() |
||
| 844 | 15 | ||
| 845 | /** |
||
| 846 | 15 | * Determine whether the string is considered to be empty. |
|
| 847 | * |
||
| 848 | * A variable is considered empty if it does not exist or if its value equals FALSE. |
||
| 849 | * empty() does not generate a warning if the variable does not exist. |
||
| 850 | * |
||
| 851 | * @return bool |
||
| 852 | */ |
||
| 853 | public function isEmpty() |
||
| 857 | 13 | ||
| 858 | /** |
||
| 859 | * Returns true if the string contains only alphabetic and numeric chars, |
||
| 860 | * false otherwise. |
||
| 861 | * |
||
| 862 | * @return bool Whether or not $str contains only alphanumeric chars |
||
| 863 | */ |
||
| 864 | public function isAlphanumeric() |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Returns true if the string contains only whitespace chars, false |
||
| 871 | * otherwise. |
||
| 872 | * |
||
| 873 | * @return bool Whether or not $str contains only whitespace characters |
||
| 874 | */ |
||
| 875 | public function isBlank() |
||
| 879 | |||
| 880 | 1 | /** |
|
| 881 | * Returns true if the string contains only hexadecimal chars, false |
||
| 882 | 1 | * otherwise. |
|
| 883 | * |
||
| 884 | * @return bool Whether or not $str contains only hexadecimal chars |
||
| 885 | */ |
||
| 886 | public function isHexadecimal() |
||
| 890 | |||
| 891 | /** |
||
| 892 | 20 | * Returns true if the string contains HTML-Tags, false otherwise. |
|
| 893 | * |
||
| 894 | 20 | * @return bool Whether or not $str contains HTML-Tags |
|
| 895 | 1 | */ |
|
| 896 | public function isHtml() |
||
| 900 | 19 | ||
| 901 | 11 | /** |
|
| 902 | * Returns true if the string contains a valid E-Mail address, false otherwise. |
||
| 903 | 8 | * |
|
| 904 | * @param bool $useExampleDomainCheck |
||
| 905 | * @param bool $useTypoInDomainCheck |
||
| 906 | * @param bool $useTemporaryDomainCheck |
||
| 907 | * @param bool $useDnsCheck |
||
| 908 | * |
||
| 909 | * @return bool Whether or not $str contains a valid E-Mail address |
||
| 910 | */ |
||
| 911 | public function isEmail($useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false) |
||
| 915 | 8 | ||
| 916 | 3 | /** |
|
| 917 | * Returns true if the string is JSON, false otherwise. Unlike json_decode |
||
| 918 | 5 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
|
| 919 | * in that an empty string is not considered valid JSON. |
||
| 920 | * |
||
| 921 | * @return bool Whether or not $str is JSON |
||
| 922 | */ |
||
| 923 | public function isJson() |
||
| 937 | 6 | ||
| 938 | 6 | /** |
|
| 939 | 4 | * Returns true if the string contains only lower case chars, false |
|
| 940 | * otherwise. |
||
| 941 | 2 | * |
|
| 942 | * @return bool Whether or not $str contains only lower case characters |
||
| 943 | */ |
||
| 944 | public function isLowerCase() |
||
| 952 | |||
| 953 | 8 | /** |
|
| 954 | * Returns true if the string is serialized, false otherwise. |
||
| 955 | * |
||
| 956 | * @return bool Whether or not $str is serialized |
||
| 957 | */ |
||
| 958 | public function isSerialized() |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Returns true if the string contains only lower case chars, false |
||
| 978 | * otherwise. |
||
| 979 | * |
||
| 980 | * @return bool Whether or not $str contains only lower case characters |
||
| 981 | */ |
||
| 982 | 15 | public function isUpperCase() |
|
| 986 | |||
| 987 | 15 | /** |
|
| 988 | 15 | * Returns the last $n characters of the string. |
|
| 989 | 15 | * |
|
| 990 | * @param int $n Number of characters to retrieve from the end |
||
| 991 | 15 | * |
|
| 992 | * @return static Object with its $str being the last $n chars |
||
| 993 | */ |
||
| 994 | View Code Duplication | public function last($n) |
|
| 1006 | 10 | ||
| 1007 | 10 | /** |
|
| 1008 | 8 | * Splits on newlines and carriage returns, returning an array of Stringy |
|
| 1009 | * objects corresponding to the lines in the string. |
||
| 1010 | 8 | * |
|
| 1011 | 6 | * @return static [] An array of Stringy objects |
|
| 1012 | 6 | */ |
|
| 1013 | 6 | public function lines() |
|
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Returns the longest common prefix between the string and $otherStr. |
||
| 1027 | 10 | * |
|
| 1028 | * @param string $otherStr Second string for comparison |
||
| 1029 | 10 | * |
|
| 1030 | 10 | * @return static Object with its $str being the longest common prefix |
|
| 1031 | */ |
||
| 1032 | 10 | public function longestCommonPrefix($otherStr) |
|
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Returns the longest common suffix between the string and $otherStr. |
||
| 1053 | * |
||
| 1054 | 10 | * @param string $otherStr Second string for comparison |
|
| 1055 | * |
||
| 1056 | * @return static Object with its $str being the longest common suffix |
||
| 1057 | */ |
||
| 1058 | 10 | public function longestCommonSuffix($otherStr) |
|
| 1076 | |||
| 1077 | 8 | /** |
|
| 1078 | 8 | * Returns the longest common substring between the string and $otherStr. |
|
| 1079 | 8 | * In the case of ties, it returns that which occurs first. |
|
| 1080 | 8 | * |
|
| 1081 | * @param string $otherStr Second string for comparison |
||
| 1082 | 8 | * |
|
| 1083 | 8 | * @return static Object with its $str being the longest common substring |
|
| 1084 | 8 | */ |
|
| 1085 | 8 | public function longestCommonSubstring($otherStr) |
|
| 1129 | |||
| 1130 | /** |
||
| 1131 | * Returns whether or not a character exists at an index. Offsets may be |
||
| 1132 | * negative to count from the last character in the string. Implements |
||
| 1133 | 2 | * part of the ArrayAccess interface. |
|
| 1134 | * |
||
| 1135 | * @param mixed $offset The index to check |
||
| 1136 | 2 | * |
|
| 1137 | 2 | * @return boolean Whether or not the index exists |
|
| 1138 | */ |
||
| 1139 | public function offsetExists($offset) |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Returns the character at the given index. Offsets may be negative to |
||
| 1154 | * count from the last character in the string. Implements part of the |
||
| 1155 | * ArrayAccess interface, and throws an OutOfBoundsException if the index |
||
| 1156 | * does not exist. |
||
| 1157 | * |
||
| 1158 | * @param mixed $offset The index from which to retrieve the char |
||
| 1159 | 1 | * |
|
| 1160 | * @return string The character at the specified index |
||
| 1161 | * @throws \OutOfBoundsException If the positive or negative offset does |
||
| 1162 | 1 | * not exist |
|
| 1163 | */ |
||
| 1164 | public function offsetGet($offset) |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Implements part of the ArrayAccess interface, but throws an exception |
||
| 1183 | * when called. This maintains the immutability of Stringy objects. |
||
| 1184 | * |
||
| 1185 | * @param mixed $offset The index of the character |
||
| 1186 | * @param mixed $value Value to set |
||
| 1187 | * |
||
| 1188 | * @throws \Exception When called |
||
| 1189 | */ |
||
| 1190 | public function offsetSet($offset, $value) |
||
| 1195 | 13 | ||
| 1196 | 1 | /** |
|
| 1197 | * Implements part of the ArrayAccess interface, but throws an exception |
||
| 1198 | 1 | * when called. This maintains the immutability of Stringy objects. |
|
| 1199 | * |
||
| 1200 | * @param mixed $offset The index of the character |
||
| 1201 | * |
||
| 1202 | 12 | * @throws \Exception When called |
|
| 1203 | 3 | */ |
|
| 1204 | 9 | public function offsetUnset($offset) |
|
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Pads the string to a given length with $padStr. If length is less than |
||
| 1212 | * or equal to the length of the string, no padding takes places. The |
||
| 1213 | * default string used for padding is a space, and the default type (one of |
||
| 1214 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
||
| 1215 | * if $padType isn't one of those 3 values. |
||
| 1216 | * |
||
| 1217 | * @param int $length Desired string length after padding |
||
| 1218 | * @param string $padStr String used to pad, defaults to space |
||
| 1219 | * @param string $padType One of 'left', 'right', 'both' |
||
| 1220 | 10 | * |
|
| 1221 | * @return static Object with a padded $str |
||
| 1222 | 10 | * @throws \InvalidArgumentException If $padType isn't one of 'right', 'left' or 'both' |
|
| 1223 | */ |
||
| 1224 | public function pad($length, $padStr = ' ', $padType = 'right') |
||
| 1241 | 37 | ||
| 1242 | 37 | /** |
|
| 1243 | * Returns a new string of a given length such that the beginning of the |
||
| 1244 | 37 | * string is padded. Alias for pad() with a $padType of 'left'. |
|
| 1245 | 3 | * |
|
| 1246 | * @param int $length Desired string length after padding |
||
| 1247 | * @param string $padStr String used to pad, defaults to space |
||
| 1248 | 34 | * |
|
| 1249 | 34 | * @return static String with left padding |
|
| 1250 | 34 | */ |
|
| 1251 | 34 | public function padLeft($length, $padStr = ' ') |
|
| 1255 | 34 | ||
| 1256 | 34 | /** |
|
| 1257 | * Adds the specified amount of left and right padding to the given string. |
||
| 1258 | 34 | * The default character used is a space. |
|
| 1259 | 34 | * |
|
| 1260 | 34 | * @param int $left Length of left padding |
|
| 1261 | 34 | * @param int $right Length of right padding |
|
| 1262 | 34 | * @param string $padStr String used to pad |
|
| 1263 | 34 | * |
|
| 1264 | 34 | * @return static String with padding applied |
|
| 1265 | 34 | */ |
|
| 1266 | 34 | protected function applyPadding($left = 0, $right = 0, $padStr = ' ') |
|
| 1303 | |||
| 1304 | /** |
||
| 1305 | * Returns a new string of a given length such that the end of the string |
||
| 1306 | * is padded. Alias for pad() with a $padType of 'right'. |
||
| 1307 | * |
||
| 1308 | * @param int $length Desired string length after padding |
||
| 1309 | * @param string $padStr String used to pad, defaults to space |
||
| 1310 | 2 | * |
|
| 1311 | * @return static String with right padding |
||
| 1312 | 2 | */ |
|
| 1313 | public function padRight($length, $padStr = ' ') |
||
| 1317 | |||
| 1318 | /** |
||
| 1319 | * Returns a new string of a given length such that both sides of the |
||
| 1320 | * string are padded. Alias for pad() with a $padType of 'both'. |
||
| 1321 | * |
||
| 1322 | 12 | * @param int $length Desired string length after padding |
|
| 1323 | * @param string $padStr String used to pad, defaults to space |
||
| 1324 | 12 | * |
|
| 1325 | * @return static String with padding applied |
||
| 1326 | 12 | */ |
|
| 1327 | 6 | public function padBoth($length, $padStr = ' ') |
|
| 1333 | |||
| 1334 | /** |
||
| 1335 | * Returns a new string starting with $string. |
||
| 1336 | * |
||
| 1337 | * @param string $string The string to append |
||
| 1338 | * |
||
| 1339 | * @return static Object with appended $string |
||
| 1340 | */ |
||
| 1341 | public function prepend($string) |
||
| 1345 | |||
| 1346 | 12 | /** |
|
| 1347 | 8 | * Returns a new string with the prefix $substring removed, if present. |
|
| 1348 | * |
||
| 1349 | 8 | * @param string $substring The prefix to remove |
|
| 1350 | * |
||
| 1351 | * @return static Object having a $str without the prefix $substring |
||
| 1352 | 4 | */ |
|
| 1353 | View Code Duplication | public function removeLeft($substring) |
|
| 1365 | |||
| 1366 | 7 | /** |
|
| 1367 | * Returns a new string with the suffix $substring removed, if present. |
||
| 1368 | * |
||
| 1369 | * @param string $substring The suffix to remove |
||
| 1370 | * |
||
| 1371 | * @return static Object having a $str without the suffix $substring |
||
| 1372 | */ |
||
| 1373 | View Code Duplication | public function removeRight($substring) |
|
| 1385 | |||
| 1386 | 28 | /** |
|
| 1387 | * Returns a repeated string given a multiplier. |
||
| 1388 | * |
||
| 1389 | * @param int $multiplier The number of times to repeat the string |
||
| 1390 | * |
||
| 1391 | * @return static Object with a repeated str |
||
| 1392 | */ |
||
| 1393 | public function repeat($multiplier) |
||
| 1399 | |||
| 1400 | 30 | /** |
|
| 1401 | 23 | * Replaces all occurrences of $search in $str by $replacement. |
|
| 1402 | 23 | * |
|
| 1403 | 7 | * @param string $search The needle to search for |
|
| 1404 | * @param string $replacement The string to replace with |
||
| 1405 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 1406 | 30 | * |
|
| 1407 | * @return static Object with the resulting $str after the replacements |
||
| 1408 | */ |
||
| 1409 | View Code Duplication | public function replace($search, $replacement, $caseSensitive = true) |
|
| 1419 | 16 | ||
| 1420 | /** |
||
| 1421 | 16 | * Replaces all occurrences of $search in $str by $replacement. |
|
| 1422 | * |
||
| 1423 | * @param array $search The elements to search for |
||
| 1424 | * @param string|array $replacement The string to replace with |
||
| 1425 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 1426 | * |
||
| 1427 | * @return static Object with the resulting $str after the replacements |
||
| 1428 | */ |
||
| 1429 | View Code Duplication | public function replaceAll(array $search, $replacement, $caseSensitive = true) |
|
| 1439 | |||
| 1440 | /** |
||
| 1441 | * Replaces all occurrences of $search from the beginning of string with $replacement |
||
| 1442 | * |
||
| 1443 | * @param string $search |
||
| 1444 | 5 | * @param string $replacement |
|
| 1445 | * |
||
| 1446 | 5 | * @return static Object with the resulting $str after the replacements |
|
| 1447 | */ |
||
| 1448 | 5 | public function replaceBeginning($search, $replacement) |
|
| 1454 | |||
| 1455 | /** |
||
| 1456 | * Replaces all occurrences of $search from the ending of string with $replacement |
||
| 1457 | * |
||
| 1458 | * @param string $search |
||
| 1459 | * @param string $replacement |
||
| 1460 | * |
||
| 1461 | * @return static Object with the resulting $str after the replacements |
||
| 1462 | 23 | */ |
|
| 1463 | public function replaceEnding($search, $replacement) |
||
| 1469 | |||
| 1470 | 19 | /** |
|
| 1471 | 19 | * Returns a reversed string. A multibyte version of strrev(). |
|
| 1472 | 19 | * |
|
| 1473 | * @return static Object with a reversed $str |
||
| 1474 | 19 | */ |
|
| 1475 | public function reverse() |
||
| 1481 | |||
| 1482 | 12 | /** |
|
| 1483 | 11 | * Truncates the string to a given length, while ensuring that it does not |
|
| 1484 | 11 | * split words. If $substring is provided, and truncating occurs, the |
|
| 1485 | 12 | * string is further truncated so that the substring may be appended without |
|
| 1486 | * exceeding the desired length. |
||
| 1487 | 19 | * |
|
| 1488 | * @param int $length Desired length of the truncated string |
||
| 1489 | 19 | * @param string $substring The substring to append if it can fit |
|
| 1490 | * |
||
| 1491 | * @return static Object with the resulting $str after truncating |
||
| 1492 | */ |
||
| 1493 | public function safeTruncate($length, $substring = '') |
||
| 1522 | 15 | ||
| 1523 | /** |
||
| 1524 | * A multibyte string shuffle function. It returns a string with its |
||
| 1525 | * characters in random order. |
||
| 1526 | * |
||
| 1527 | * @return static Object with a shuffled $str |
||
| 1528 | */ |
||
| 1529 | public function shuffle() |
||
| 1535 | |||
| 1536 | /** |
||
| 1537 | * Converts the string into an URL slug. This includes replacing non-ASCII |
||
| 1538 | * characters with their closest ASCII equivalents, removing remaining |
||
| 1539 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
||
| 1540 | * $replacement. The replacement defaults to a single dash, and the string |
||
| 1541 | * is also converted to lowercase. |
||
| 1542 | * |
||
| 1543 | * @param string $replacement The string used to replace whitespace |
||
| 1544 | 1 | * @param string $language The language for the url |
|
| 1545 | * @param bool $strToLower string to lower |
||
| 1546 | 1 | * |
|
| 1547 | * @return static Object whose $str has been converted to an URL slug |
||
| 1548 | 1 | */ |
|
| 1549 | public function slugify($replacement = '-', $language = 'de', $strToLower = true) |
||
| 1555 | |||
| 1556 | 1 | /** |
|
| 1557 | * Remove css media-queries. |
||
| 1558 | 1 | * |
|
| 1559 | * @return static |
||
| 1560 | */ |
||
| 1561 | public function stripeCssMediaQueries() |
||
| 1567 | |||
| 1568 | 6 | /** |
|
| 1569 | 6 | * Strip all whitespace characters. This includes tabs and newline characters, |
|
| 1570 | 6 | * as well as multibyte whitespace such as the thin space and ideographic space. |
|
| 1571 | 6 | * |
|
| 1572 | 6 | * @return Stringy |
|
| 1573 | */ |
||
| 1574 | 6 | public function stripWhitespace() |
|
| 1578 | |||
| 1579 | /** |
||
| 1580 | * Remove empty html-tag. |
||
| 1581 | * |
||
| 1582 | * e.g.: <tag></tag> |
||
| 1583 | * |
||
| 1584 | * @return static |
||
| 1585 | */ |
||
| 1586 | 1 | public function stripeEmptyHtmlTags() |
|
| 1592 | 1 | ||
| 1593 | /** |
||
| 1594 | * Converts the string into an valid UTF-8 string. |
||
| 1595 | 1 | * |
|
| 1596 | * @return static |
||
| 1597 | 1 | */ |
|
| 1598 | 1 | public function utf8ify() |
|
| 1602 | |||
| 1603 | /** |
||
| 1604 | * escape html |
||
| 1605 | * |
||
| 1606 | * @return static |
||
| 1607 | */ |
||
| 1608 | public function escape() |
||
| 1618 | |||
| 1619 | /** |
||
| 1620 | * Create an extract from a text, so if the search-string was found, it will be centered in the output. |
||
| 1621 | 1 | * |
|
| 1622 | 1 | * @param string $search |
|
| 1623 | 1 | * @param int|null $length |
|
| 1624 | 1 | * @param string $ellipsis |
|
| 1625 | 1 | * |
|
| 1626 | 1 | * @return static |
|
| 1627 | 1 | */ |
|
| 1628 | public function extractText($search = '', $length = null, $ellipsis = '...') |
||
| 1744 | 6 | ||
| 1745 | |||
| 1746 | 6 | /** |
|
| 1747 | * remove xss from html |
||
| 1748 | 6 | * |
|
| 1749 | * @return static |
||
| 1750 | */ |
||
| 1751 | public function removeXss() |
||
| 1763 | |||
| 1764 | 18 | /** |
|
| 1765 | 4 | * remove html-break [br | \r\n | \r | \n | ...] |
|
| 1766 | 18 | * |
|
| 1767 | 4 | * @param string $replacement |
|
| 1768 | 10 | * |
|
| 1769 | 2 | * @return static |
|
| 1770 | 2 | */ |
|
| 1771 | 8 | public function removeHtmlBreak($replacement = '') |
|
| 1777 | |||
| 1778 | /** |
||
| 1779 | * remove html |
||
| 1780 | * |
||
| 1781 | * @param $allowableTags |
||
| 1782 | * |
||
| 1783 | * @return static |
||
| 1784 | */ |
||
| 1785 | public function removeHtml($allowableTags = null) |
||
| 1791 | 19 | ||
| 1792 | 2 | /** |
|
| 1793 | * Returns the substring beginning at $start, and up to, but not including |
||
| 1794 | * the index specified by $end. If $end is omitted, the function extracts |
||
| 1795 | * the remaining string. If $end is negative, it is computed from the end |
||
| 1796 | * of the string. |
||
| 1797 | 17 | * |
|
| 1798 | 1 | * @param int $start Initial index from which to begin extraction |
|
| 1799 | * @param int $end Optional index at which to end extraction |
||
| 1800 | * |
||
| 1801 | * @return static Object with its $str being the extracted substring |
||
| 1802 | */ |
||
| 1803 | 16 | public function slice($start, $end = null) |
|
| 1819 | 16 | ||
| 1820 | /** |
||
| 1821 | 16 | * Splits the string with the provided regular expression, returning an |
|
| 1822 | * array of Stringy objects. An optional integer $limit will truncate the |
||
| 1823 | * results. |
||
| 1824 | * |
||
| 1825 | * @param string $pattern The regex with which to split the string |
||
| 1826 | * @param int $limit Optional maximum number of results to return |
||
| 1827 | * |
||
| 1828 | * @return static [] An array of Stringy objects |
||
| 1829 | */ |
||
| 1830 | public function split($pattern, $limit = null) |
||
| 1864 | 4 | ||
| 1865 | /** |
||
| 1866 | * Surrounds $str with the given substring. |
||
| 1867 | * |
||
| 1868 | * @param string $substring The substring to add to both sides |
||
| 1869 | * |
||
| 1870 | * @return static Object whose $str had the substring both prepended and |
||
| 1871 | * appended |
||
| 1872 | */ |
||
| 1873 | public function surround($substring) |
||
| 1879 | 5 | ||
| 1880 | /** |
||
| 1881 | 5 | * Returns a case swapped version of the string. |
|
| 1882 | 5 | * |
|
| 1883 | * @return static Object whose $str has each character's case swapped |
||
| 1884 | 5 | */ |
|
| 1885 | 2 | public function swapCase() |
|
| 1893 | 5 | ||
| 1894 | /** |
||
| 1895 | 5 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
|
| 1896 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
||
| 1897 | * equivalents. |
||
| 1898 | * |
||
| 1899 | * @return static Object whose $str has those characters removed |
||
| 1900 | */ |
||
| 1901 | public function tidy() |
||
| 1907 | |||
| 1908 | 27 | /** |
|
| 1909 | * Returns a trimmed string with the first letter of each word capitalized. |
||
| 1910 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
| 1911 | * capitalized. |
||
| 1912 | * |
||
| 1913 | * @param array $ignore An array of words not to capitalize |
||
| 1914 | * |
||
| 1915 | * @return static Object with a titleized $str |
||
| 1916 | 7 | */ |
|
| 1917 | public function titleize($ignore = null) |
||
| 1938 | |||
| 1939 | /** |
||
| 1940 | * Converts all characters in the string to lowercase. An alias for PHP's |
||
| 1941 | * UTF8::strtolower(). |
||
| 1942 | * |
||
| 1943 | * @return static Object with all characters of $str being lowercase |
||
| 1944 | */ |
||
| 1945 | public function toLowerCase() |
||
| 1951 | |||
| 1952 | 15 | /** |
|
| 1953 | 15 | * Returns true if the string is base64 encoded, false otherwise. |
|
| 1954 | 15 | * |
|
| 1955 | 15 | * @return bool Whether or not $str is base64 encoded |
|
| 1956 | 15 | */ |
|
| 1957 | 15 | public function isBase64() |
|
| 1961 | |||
| 1962 | 15 | /** |
|
| 1963 | 10 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
|
| 1964 | 5 | * replaced with their closest ASCII counterparts, and the rest are removed |
|
| 1965 | 2 | * unless instructed otherwise. |
|
| 1966 | * |
||
| 1967 | 3 | * @param $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance</p> |
|
| 1968 | * |
||
| 1969 | * @return static Object whose $str contains only ASCII characters |
||
| 1970 | */ |
||
| 1971 | public function toAscii($strict = false) |
||
| 1977 | |||
| 1978 | 993 | /** |
|
| 1979 | * Returns a boolean representation of the given logical string value. |
||
| 1980 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
| 1981 | * 'off', and 'no' will return false. In all instances, case is ignored. |
||
| 1982 | * For other numeric strings, their sign will determine the return value. |
||
| 1983 | * In addition, blank strings consisting of only whitespace will return |
||
| 1984 | * false. For all other strings, the return value is a result of a |
||
| 1985 | * boolean cast. |
||
| 1986 | * |
||
| 1987 | * @return bool A boolean value for the string |
||
| 1988 | */ |
||
| 1989 | 6 | public function toBoolean() |
|
| 2013 | |||
| 2014 | /** |
||
| 2015 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
||
| 2016 | * |
||
| 2017 | * @return string |
||
| 2018 | */ |
||
| 2019 | 5 | public function toString() |
|
| 2023 | |||
| 2024 | 5 | /** |
|
| 2025 | * Converts each tab in the string to some number of spaces, as defined by |
||
| 2026 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
||
| 2027 | * |
||
| 2028 | * @param int $tabLength Number of spaces to replace each tab with |
||
| 2029 | * |
||
| 2030 | * @return static Object whose $str has had tabs switched to spaces |
||
| 2031 | */ |
||
| 2032 | View Code Duplication | public function toSpaces($tabLength = 4) |
|
| 2039 | |||
| 2040 | /** |
||
| 2041 | * Converts each occurrence of some consecutive number of spaces, as |
||
| 2042 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
||
| 2043 | * are converted to a tab. |
||
| 2044 | * |
||
| 2045 | * @param int $tabLength Number of spaces to replace with a tab |
||
| 2046 | * |
||
| 2047 | * @return static Object whose $str has had spaces switched to tabs |
||
| 2048 | */ |
||
| 2049 | 13 | View Code Duplication | public function toTabs($tabLength = 4) |
| 2056 | |||
| 2057 | 13 | /** |
|
| 2058 | * Converts the first character of each word in the string to uppercase. |
||
| 2059 | * |
||
| 2060 | * @return static Object with all characters of $str being title-cased |
||
| 2061 | */ |
||
| 2062 | public function toTitleCase() |
||
| 2069 | 13 | ||
| 2070 | /** |
||
| 2071 | 13 | * Converts all characters in the string to uppercase. An alias for PHP's |
|
| 2072 | 11 | * UTF8::strtoupper(). |
|
| 2073 | 11 | * |
|
| 2074 | 2 | * @return static Object with all characters of $str being uppercase |
|
| 2075 | */ |
||
| 2076 | public function toUpperCase() |
||
| 2082 | |||
| 2083 | /** |
||
| 2084 | * Returns a string with whitespace removed from the start of the string. |
||
| 2085 | * Supports the removal of unicode whitespace. Accepts an optional |
||
| 2086 | * string of characters to strip instead of the defaults. |
||
| 2087 | * |
||
| 2088 | * @param string $chars Optional string of characters to strip |
||
| 2089 | * |
||
| 2090 | 22 | * @return static Object with a trimmed $str |
|
| 2091 | */ |
||
| 2092 | 22 | View Code Duplication | public function trimLeft($chars = null) |
| 2102 | 18 | ||
| 2103 | /** |
||
| 2104 | 18 | * Returns a string with whitespace removed from the end of the string. |
|
| 2105 | * Supports the removal of unicode whitespace. Accepts an optional |
||
| 2106 | * string of characters to strip instead of the defaults. |
||
| 2107 | * |
||
| 2108 | * @param string $chars Optional string of characters to strip |
||
| 2109 | * |
||
| 2110 | * @return static Object with a trimmed $str |
||
| 2111 | */ |
||
| 2112 | View Code Duplication | public function trimRight($chars = null) |
|
| 2122 | |||
| 2123 | /** |
||
| 2124 | * Truncates the string to a given length. If $substring is provided, and |
||
| 2125 | * truncating occurs, the string is further truncated so that the substring |
||
| 2126 | * may be appended without exceeding the desired length. |
||
| 2127 | 13 | * |
|
| 2128 | * @param int $length Desired length of the truncated string |
||
| 2129 | 13 | * @param string $substring The substring to append if it can fit |
|
| 2130 | * |
||
| 2131 | * @return static Object with the resulting $str after truncating |
||
| 2132 | */ |
||
| 2133 | View Code Duplication | public function truncate($length, $substring = '') |
|
| 2149 | 27 | ||
| 2150 | /** |
||
| 2151 | 1 | * Returns a lowercase and trimmed string separated by underscores. |
|
| 2152 | * Underscores are inserted before uppercase characters (with the exception |
||
| 2153 | 32 | * of the first character of the string), and in place of spaces as well as |
|
| 2154 | 32 | * dashes. |
|
| 2155 | 32 | * |
|
| 2156 | * @return static Object with an underscored $str |
||
| 2157 | 32 | */ |
|
| 2158 | 32 | public function underscored() |
|
| 2162 | 32 | ||
| 2163 | 32 | /** |
|
| 2164 | * Returns an UpperCamelCase version of the supplied string. It trims |
||
| 2165 | 32 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
|
| 2166 | * and underscores, and removes spaces, dashes, underscores. |
||
| 2167 | * |
||
| 2168 | * @return static Object with $str in UpperCamelCase |
||
| 2169 | */ |
||
| 2170 | public function upperCamelize() |
||
| 2174 | |||
| 2175 | 20 | /** |
|
| 2176 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
| 2177 | 20 | * capitalizes letters following digits, spaces, dashes and underscores, |
|
| 2178 | 20 | * and removes spaces, dashes, as well as underscores. |
|
| 2179 | 20 | * |
|
| 2180 | * @return static Object with $str in camelCase |
||
| 2181 | 20 | */ |
|
| 2182 | 20 | public function camelize() |
|
| 2210 | |||
| 2211 | 20 | /** |
|
| 2212 | 20 | * Convert a string to e.g.: "snake_case" |
|
| 2213 | * |
||
| 2214 | 20 | * @return static Object with $str in snake_case |
|
| 2215 | */ |
||
| 2216 | public function snakeize() |
||
| 2259 | 1 | ||
| 2260 | 1 | /** |
|
| 2261 | 1 | * Converts the first character of the string to lower case. |
|
| 2262 | 1 | * |
|
| 2263 | * @return static Object with the first character of $str being lower case |
||
| 2264 | 1 | */ |
|
| 2265 | View Code Duplication | public function lowerCaseFirst() |
|
| 2274 | |||
| 2275 | 1 | /** |
|
| 2276 | * Shorten the string after $length, but also after the next word. |
||
| 2277 | 1 | * |
|
| 2278 | 1 | * @param int $length |
|
| 2279 | * @param string $strAddOn |
||
| 2280 | * |
||
| 2281 | 1 | * @return static |
|
| 2282 | 1 | */ |
|
| 2283 | 1 | public function shortenAfterWord($length, $strAddOn = '...') |
|
| 2289 | 1 | ||
| 2290 | /** |
||
| 2291 | * Line-Wrap the string after $limit, but also after the next word. |
||
| 2292 | * |
||
| 2293 | * @param int $limit |
||
| 2294 | * |
||
| 2295 | * @return static |
||
| 2296 | */ |
||
| 2297 | public function lineWrapAfterWord($limit) |
||
| 2309 | 1 | ||
| 2310 | 1 | /** |
|
| 2311 | 1 | * Gets the substring after the first occurrence of a separator. |
|
| 2312 | 1 | * If no match is found returns new empty Stringy object. |
|
| 2313 | 1 | * |
|
| 2314 | 1 | * @param string $separator |
|
| 2315 | 1 | * |
|
| 2316 | * @return static |
||
| 2317 | */ |
||
| 2318 | View Code Duplication | public function afterFirst($separator) |
|
| 2334 | 1 | ||
| 2335 | 1 | /** |
|
| 2336 | 1 | * Gets the substring after the last occurrence of a separator. |
|
| 2337 | 1 | * If no match is found returns new empty Stringy object. |
|
| 2338 | 1 | * |
|
| 2339 | 1 | * @param string $separator |
|
| 2340 | 1 | * |
|
| 2341 | 1 | * @return static |
|
| 2342 | */ |
||
| 2343 | public function afterLast($separator) |
||
| 2360 | 1 | ||
| 2361 | 1 | /** |
|
| 2362 | 1 | * Gets the substring before the first occurrence of a separator. |
|
| 2363 | 1 | * If no match is found returns new empty Stringy object. |
|
| 2364 | 1 | * |
|
| 2365 | 1 | * @param string $separator |
|
| 2366 | 1 | * |
|
| 2367 | 1 | * @return static |
|
| 2368 | */ |
||
| 2369 | View Code Duplication | public function beforeFirst($separator) |
|
| 2386 | |||
| 2387 | /** |
||
| 2388 | * Gets the substring before the last occurrence of a separator. |
||
| 2389 | * If no match is found returns new empty Stringy object. |
||
| 2390 | 7 | * |
|
| 2391 | * @param string $separator |
||
| 2392 | 7 | * |
|
| 2393 | * @return static |
||
| 2394 | 7 | */ |
|
| 2395 | 7 | View Code Duplication | public function beforeLast($separator) |
| 2412 | 39 | ||
| 2413 | 39 | /** |
|
| 2414 | * Returns the string with the first letter of each word capitalized, |
||
| 2415 | * except for when the word is a name which shouldn't be capitalized. |
||
| 2416 | * |
||
| 2417 | 39 | * @return static Object with $str capitalized |
|
| 2418 | 39 | */ |
|
| 2419 | 39 | public function capitalizePersonalName() |
|
| 2427 | 39 | ||
| 2428 | 39 | /** |
|
| 2429 | 39 | * @param string $word |
|
| 2430 | 39 | * |
|
| 2431 | 39 | * @return string |
|
| 2432 | 39 | */ |
|
| 2433 | 39 | protected function capitalizeWord($word) |
|
| 2443 | 39 | ||
| 2444 | /** |
||
| 2445 | 39 | * Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius"). |
|
| 2446 | 39 | * |
|
| 2447 | 39 | * @param string $names |
|
| 2448 | 39 | * @param string $delimiter |
|
| 2449 | 39 | * |
|
| 2450 | 39 | * @return string |
|
| 2451 | 39 | */ |
|
| 2452 | 39 | protected function capitalizePersonalNameByDelimiter($names, $delimiter) |
|
| 2528 | } |
||
| 2529 |
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: