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 index of the first occurrence of $needle in the string, |
||
| 234 | * and false if not found. Accepts an optional offset from which to begin |
||
| 235 | * the search. |
||
| 236 | 64 | * |
|
| 237 | * @param string $needle Substring to look for |
||
| 238 | 64 | * @param int $offset Offset from which to search |
|
| 239 | 19 | * |
|
| 240 | 19 | * @return int|bool The occurrence's index if found, otherwise false |
|
| 241 | */ |
||
| 242 | 64 | public function indexOfIgnoreCase($needle, $offset = 0) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Returns the substring beginning at $start with the specified $length. |
||
| 249 | * It differs from the UTF8::substr() function in that providing a $length of |
||
| 250 | * null will return the rest of the string, rather than an empty string. |
||
| 251 | * |
||
| 252 | 248 | * @param int $start Position of the first character to use |
|
| 253 | * @param int $length Maximum number of characters used |
||
| 254 | 248 | * |
|
| 255 | * @return static Object with its $str being the substring |
||
| 256 | */ |
||
| 257 | public function substr($start, $length = null) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Returns the length of the string. |
||
| 270 | * |
||
| 271 | * @return int The number of characters in $str given the encoding |
||
| 272 | */ |
||
| 273 | public function length() |
||
| 277 | |||
| 278 | 153 | /** |
|
| 279 | * Trims the string and replaces consecutive whitespace characters with a |
||
| 280 | 153 | * single space. This includes tabs and newline characters, as well as |
|
| 281 | 152 | * multibyte whitespace such as the thin space and ideographic space. |
|
| 282 | 152 | * |
|
| 283 | 1 | * @return static Object with a trimmed $str and condensed whitespace |
|
| 284 | */ |
||
| 285 | public function collapseWhitespace() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns a string with whitespace removed from the start and end of the |
||
| 292 | * string. Supports the removal of unicode whitespace. Accepts an optional |
||
| 293 | * string of characters to strip instead of the defaults. |
||
| 294 | * |
||
| 295 | * @param string $chars Optional string of characters to strip |
||
| 296 | * |
||
| 297 | * @return static Object with a trimmed $str |
||
| 298 | 223 | */ |
|
| 299 | View Code Duplication | public function trim($chars = null) |
|
| 309 | |||
| 310 | 223 | /** |
|
| 311 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
| 312 | * |
||
| 313 | * @param string $pattern The regular expression pattern |
||
| 314 | * @param string $replacement The string to replace with |
||
| 315 | * @param string $options Matching conditions to be used |
||
| 316 | * |
||
| 317 | * @return static Object with the result2ing $str after the replacements |
||
| 318 | */ |
||
| 319 | public function regexReplace($pattern, $replacement, $options = '') |
||
| 333 | |||
| 334 | 24 | /** |
|
| 335 | * Returns true if the string contains all $needles, false otherwise. By |
||
| 336 | 24 | * default the comparison is case-sensitive, but can be made insensitive by |
|
| 337 | * setting $caseSensitive to false. |
||
| 338 | * |
||
| 339 | * @param array $needles SubStrings to look for |
||
| 340 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 341 | * |
||
| 342 | * @return bool Whether or not $str contains $needle |
||
| 343 | */ |
||
| 344 | View Code Duplication | public function containsAll($needles, $caseSensitive = true) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Returns true if the string contains $needle, false otherwise. By default |
||
| 362 | * the comparison is case-sensitive, but can be made insensitive by setting |
||
| 363 | * $caseSensitive to false. |
||
| 364 | * |
||
| 365 | * @param string $needle Substring 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 | public function contains($needle, $caseSensitive = true) |
|
| 380 | |||
| 381 | 18 | /** |
|
| 382 | * Returns true if the string contains any $needles, false otherwise. By |
||
| 383 | 18 | * default the comparison is case-sensitive, but can be made insensitive by |
|
| 384 | * setting $caseSensitive to false. |
||
| 385 | * |
||
| 386 | * @param array $needles SubStrings to look for |
||
| 387 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 388 | * |
||
| 389 | * @return bool Whether or not $str contains $needle |
||
| 390 | */ |
||
| 391 | 1 | View Code Duplication | public function containsAny($needles, $caseSensitive = true) |
| 406 | 15 | ||
| 407 | /** |
||
| 408 | 15 | * Returns the length of the string, implementing the countable interface. |
|
| 409 | 9 | * |
|
| 410 | * @return int The number of characters in the string, given the encoding |
||
| 411 | */ |
||
| 412 | 6 | public function count() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Returns the number of occurrences of $substring in the given string. |
||
| 419 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
| 420 | * by setting $caseSensitive to false. |
||
| 421 | * |
||
| 422 | * @param string $substring The substring to search for |
||
| 423 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 424 | * |
||
| 425 | 19 | * @return int The number of $substring occurrences |
|
| 426 | */ |
||
| 427 | 19 | public function countSubstr($substring, $caseSensitive = true) |
|
| 438 | |||
| 439 | /** |
||
| 440 | 49 | * Returns a lowercase and trimmed string separated by dashes. Dashes are |
|
| 441 | * inserted before uppercase characters (with the exception of the first |
||
| 442 | 49 | * character of the string), and in place of spaces as well as underscores. |
|
| 443 | * |
||
| 444 | 49 | * @return static Object with a dasherized $str |
|
| 445 | */ |
||
| 446 | 49 | public function dasherize() |
|
| 450 | 49 | ||
| 451 | /** |
||
| 452 | * Returns a lowercase and trimmed string separated by the given delimiter. |
||
| 453 | * Delimiters are inserted before uppercase characters (with the exception |
||
| 454 | * of the first character of the string), and in place of spaces, dashes, |
||
| 455 | * and underscores. Alpha delimiters are not converted to lowercase. |
||
| 456 | * |
||
| 457 | * @param string $delimiter Sequence used to separate parts of the string |
||
| 458 | * |
||
| 459 | * @return static Object with a delimited $str |
||
| 460 | */ |
||
| 461 | 10 | public function delimit($delimiter) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Ensures that the string begins with $substring. If it doesn't, it's |
||
| 476 | * prepended. |
||
| 477 | * |
||
| 478 | * @param string $substring The substring to add if not present |
||
| 479 | * |
||
| 480 | * @return static Object with its $str prefixed by the $substring |
||
| 481 | */ |
||
| 482 | 45 | View Code Duplication | public function ensureLeft($substring) |
| 492 | |||
| 493 | /** |
||
| 494 | * Returns true if the string begins with $substring, false otherwise. By |
||
| 495 | * default, the comparison is case-sensitive, but can be made insensitive |
||
| 496 | * by setting $caseSensitive to false. |
||
| 497 | * |
||
| 498 | * @param string $substring The substring to look for |
||
| 499 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 500 | * |
||
| 501 | * @return bool Whether or not $str starts with $substring |
||
| 502 | */ |
||
| 503 | public function startsWith($substring, $caseSensitive = true) |
||
| 514 | 6 | ||
| 515 | /** |
||
| 516 | 6 | * Returns true if the string begins with any of $substrings, false otherwise. |
|
| 517 | * By default the comparison is case-sensitive, but can be made insensitive by |
||
| 518 | * setting $caseSensitive to false. |
||
| 519 | * |
||
| 520 | * @param array $substrings Substrings to look for |
||
| 521 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 522 | * |
||
| 523 | * @return bool Whether or not $str starts with $substring |
||
| 524 | */ |
||
| 525 | View Code Duplication | public function startsWithAny(array $substrings, $caseSensitive = true) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Ensures that the string ends with $substring. If it doesn't, it's |
||
| 542 | * appended. |
||
| 543 | * |
||
| 544 | * @param string $substring The substring to add if not present |
||
| 545 | * |
||
| 546 | * @return static Object with its $str suffixed by the $substring |
||
| 547 | */ |
||
| 548 | 33 | View Code Duplication | public function ensureRight($substring) |
| 558 | 33 | ||
| 559 | /** |
||
| 560 | 33 | * Returns true if the string ends with $substring, false otherwise. By |
|
| 561 | 4 | * default, the comparison is case-sensitive, but can be made insensitive |
|
| 562 | 4 | * by setting $caseSensitive to false. |
|
| 563 | 4 | * |
|
| 564 | * @param string $substring The substring to look for |
||
| 565 | 33 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
|
| 566 | * |
||
| 567 | * @return bool Whether or not $str ends with $substring |
||
| 568 | */ |
||
| 569 | public function endsWith($substring, $caseSensitive = true) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Returns true if the string ends with any of $substrings, false otherwise. |
||
| 591 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
| 592 | * by setting $caseSensitive to false. |
||
| 593 | 3 | * |
|
| 594 | * @param string[] $substrings Substrings to look for |
||
| 595 | 3 | * @param bool $caseSensitive Whether or not to enforce |
|
| 596 | * case-sensitivity |
||
| 597 | * |
||
| 598 | * @return bool Whether or not $str ends with $substring |
||
| 599 | */ |
||
| 600 | View Code Duplication | public function endsWithAny($substrings, $caseSensitive = true) |
|
| 614 | |||
| 615 | /** |
||
| 616 | 4 | * Returns the first $n characters of the string. |
|
| 617 | * |
||
| 618 | * @param int $n Number of characters to retrieve from the start |
||
| 619 | 4 | * |
|
| 620 | 4 | * @return static Object with its $str being the first $n chars |
|
| 621 | */ |
||
| 622 | 4 | View Code Duplication | public function first($n) |
| 634 | |||
| 635 | /** |
||
| 636 | 11 | * Returns the encoding used by the Stringy object. |
|
| 637 | * |
||
| 638 | 11 | * @return string The current value of the $encoding property |
|
| 639 | */ |
||
| 640 | public function getEncoding() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
| 647 | 12 | * interface. The ArrayIterator's constructor is passed an array of chars |
|
| 648 | * in the multibyte string. This enables the use of foreach with instances |
||
| 649 | 12 | * of Stringy\Stringy. |
|
| 650 | * |
||
| 651 | * @return \ArrayIterator An iterator for the characters in the string |
||
| 652 | */ |
||
| 653 | public function getIterator() |
||
| 657 | |||
| 658 | /** |
||
| 659 | 103 | * Returns an array consisting of the characters in the string. |
|
| 660 | * |
||
| 661 | 103 | * @return array An array of string chars |
|
| 662 | 64 | */ |
|
| 663 | public function chars() |
||
| 675 | |||
| 676 | 12 | /** |
|
| 677 | * Returns the character at $index, with indexes starting at 0. |
||
| 678 | * |
||
| 679 | * @param int $index Position of the character |
||
| 680 | * |
||
| 681 | * @return static The character at $index |
||
| 682 | */ |
||
| 683 | public function at($index) |
||
| 687 | |||
| 688 | 5 | /** |
|
| 689 | * Returns true if the string contains a lower case char, false |
||
| 690 | 5 | * otherwise. |
|
| 691 | * |
||
| 692 | * @return bool Whether or not the string contains a lower case character. |
||
| 693 | */ |
||
| 694 | public function hasLowerCase() |
||
| 698 | |||
| 699 | /** |
||
| 700 | 5 | * Returns true if $str matches the supplied pattern, false otherwise. |
|
| 701 | * |
||
| 702 | 5 | * @param string $pattern Regex pattern to match against |
|
| 703 | * |
||
| 704 | 5 | * @return bool Whether or not $str matches the pattern |
|
| 705 | */ |
||
| 706 | protected function matchesPattern($pattern) |
||
| 714 | |||
| 715 | 3 | /** |
|
| 716 | * Returns true if the string contains an upper case char, false |
||
| 717 | 3 | * otherwise. |
|
| 718 | * |
||
| 719 | * @return bool Whether or not the string contains an upper case character. |
||
| 720 | */ |
||
| 721 | public function hasUpperCase() |
||
| 725 | 27 | ||
| 726 | /** |
||
| 727 | 27 | * Convert all HTML entities to their applicable characters. |
|
| 728 | 27 | * |
|
| 729 | 27 | * @param int|null $flags Optional flags |
|
| 730 | 27 | * |
|
| 731 | 27 | * @return static Object with the resulting $str after being html decoded. |
|
| 732 | 27 | */ |
|
| 733 | 27 | public function htmlDecode($flags = ENT_COMPAT) |
|
| 739 | |||
| 740 | /** |
||
| 741 | * Convert all applicable characters to HTML entities. |
||
| 742 | * |
||
| 743 | * @param int|null $flags Optional flags |
||
| 744 | * |
||
| 745 | * @return static Object with the resulting $str after being html encoded. |
||
| 746 | */ |
||
| 747 | public function htmlEncode($flags = ENT_COMPAT) |
||
| 753 | 12 | ||
| 754 | /** |
||
| 755 | * Capitalizes the first word of the string, replaces underscores with |
||
| 756 | * spaces, and strips '_id'. |
||
| 757 | * |
||
| 758 | * @return static Object with a humanized $str |
||
| 759 | */ |
||
| 760 | public function humanize() |
||
| 766 | 8 | ||
| 767 | 8 | /** |
|
| 768 | 1 | * Converts the first character of the supplied string to upper case. |
|
| 769 | * |
||
| 770 | * @return static Object with the first character of $str being upper case |
||
| 771 | 7 | */ |
|
| 772 | 7 | View Code Duplication | public function upperCaseFirst() |
| 786 | |||
| 787 | /** |
||
| 788 | * Returns the index of the last occurrence of $needle in the string, |
||
| 789 | * and false if not found. Accepts an optional offset from which to begin |
||
| 790 | * the search. Offsets may be negative to count from the last character |
||
| 791 | 13 | * in the string. |
|
| 792 | * |
||
| 793 | 13 | * @param string $needle Substring to look for |
|
| 794 | 1 | * @param int $offset Offset from which to search |
|
| 795 | * |
||
| 796 | * @return int|bool The last occurrence's index if found, otherwise false |
||
| 797 | 12 | */ |
|
| 798 | 12 | public function indexOfLast($needle, $offset = 0) |
|
| 802 | |||
| 803 | /** |
||
| 804 | * Returns the index of the last occurrence of $needle in the string, |
||
| 805 | * and false if not found. Accepts an optional offset from which to begin |
||
| 806 | * the search. Offsets may be negative to count from the last character |
||
| 807 | * in the string. |
||
| 808 | * |
||
| 809 | 10 | * @param string $needle Substring to look for |
|
| 810 | * @param int $offset Offset from which to search |
||
| 811 | 10 | * |
|
| 812 | * @return int|bool The last occurrence's index if found, otherwise false |
||
| 813 | */ |
||
| 814 | public function indexOfLastIgnoreCase($needle, $offset = 0) |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Inserts $substring into the string at the $index provided. |
||
| 821 | * |
||
| 822 | * @param string $substring String to be inserted |
||
| 823 | * @param int $index The index at which to insert the substring |
||
| 824 | * |
||
| 825 | * @return static Object with the resulting $str after the insertion |
||
| 826 | */ |
||
| 827 | View Code Duplication | public function insert($substring, $index) |
|
| 841 | |||
| 842 | /** |
||
| 843 | * Returns true if the string contains the $pattern, otherwise false. |
||
| 844 | 15 | * |
|
| 845 | * WARNING: Asterisks ("*") are translated into (".*") zero-or-more regular |
||
| 846 | 15 | * expression wildcards. |
|
| 847 | * |
||
| 848 | * @credit Originally from Laravel, thanks Taylor. |
||
| 849 | * |
||
| 850 | * @param string $pattern The string or pattern to match against. |
||
| 851 | * |
||
| 852 | * @return bool Whether or not we match the provided pattern. |
||
| 853 | */ |
||
| 854 | public function is($pattern) |
||
| 865 | 1 | ||
| 866 | /** |
||
| 867 | 1 | * Returns true if the string contains only alphabetic chars, false |
|
| 868 | * otherwise. |
||
| 869 | * |
||
| 870 | * @return bool Whether or not $str contains only alphabetic chars |
||
| 871 | */ |
||
| 872 | public function isAlpha() |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Determine whether the string is considered to be empty. |
||
| 879 | * |
||
| 880 | 1 | * A variable is considered empty if it does not exist or if its value equals FALSE. |
|
| 881 | * empty() does not generate a warning if the variable does not exist. |
||
| 882 | 1 | * |
|
| 883 | * @return bool |
||
| 884 | */ |
||
| 885 | public function isEmpty() |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Returns true if the string contains only alphabetic and numeric chars, |
||
| 892 | 20 | * false otherwise. |
|
| 893 | * |
||
| 894 | 20 | * @return bool Whether or not $str contains only alphanumeric chars |
|
| 895 | 1 | */ |
|
| 896 | public function isAlphanumeric() |
||
| 900 | 19 | ||
| 901 | 11 | /** |
|
| 902 | * Returns true if the string contains only whitespace chars, false |
||
| 903 | 8 | * otherwise. |
|
| 904 | * |
||
| 905 | * @return bool Whether or not $str contains only whitespace characters |
||
| 906 | */ |
||
| 907 | public function isBlank() |
||
| 911 | |||
| 912 | /** |
||
| 913 | 8 | * Returns true if the string contains only hexadecimal chars, false |
|
| 914 | * otherwise. |
||
| 915 | 8 | * |
|
| 916 | 3 | * @return bool Whether or not $str contains only hexadecimal chars |
|
| 917 | */ |
||
| 918 | 5 | public function isHexadecimal() |
|
| 922 | |||
| 923 | /** |
||
| 924 | * Returns true if the string contains HTML-Tags, false otherwise. |
||
| 925 | * |
||
| 926 | * @return bool Whether or not $str contains HTML-Tags |
||
| 927 | 7 | */ |
|
| 928 | public function isHtml() |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Returns true if the string contains a valid E-Mail address, false otherwise. |
||
| 935 | 6 | * |
|
| 936 | 6 | * @param bool $useExampleDomainCheck |
|
| 937 | 6 | * @param bool $useTypoInDomainCheck |
|
| 938 | 6 | * @param bool $useTemporaryDomainCheck |
|
| 939 | 4 | * @param bool $useDnsCheck |
|
| 940 | * |
||
| 941 | 2 | * @return bool Whether or not $str contains a valid E-Mail address |
|
| 942 | */ |
||
| 943 | public function isEmail($useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false) |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Returns true if the string is JSON, false otherwise. Unlike json_decode |
||
| 950 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
||
| 951 | 8 | * in that an empty string is not considered valid JSON. |
|
| 952 | * |
||
| 953 | 8 | * @return bool Whether or not $str is JSON |
|
| 954 | */ |
||
| 955 | public function isJson() |
||
| 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 isLowerCase() |
||
| 984 | 15 | ||
| 985 | /** |
||
| 986 | * Returns true if the string is serialized, false otherwise. |
||
| 987 | 15 | * |
|
| 988 | 15 | * @return bool Whether or not $str is serialized |
|
| 989 | 15 | */ |
|
| 990 | public function isSerialized() |
||
| 1007 | 10 | ||
| 1008 | 8 | /** |
|
| 1009 | * Returns true if the string contains only lower case chars, false |
||
| 1010 | 8 | * otherwise. |
|
| 1011 | 6 | * |
|
| 1012 | 6 | * @return bool Whether or not $str contains only lower case characters |
|
| 1013 | 6 | */ |
|
| 1014 | public function isUpperCase() |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Returns the last $n characters of the string. |
||
| 1021 | * |
||
| 1022 | * @param int $n Number of characters to retrieve from the end |
||
| 1023 | * |
||
| 1024 | * @return static Object with its $str being the last $n chars |
||
| 1025 | */ |
||
| 1026 | View Code Duplication | public function last($n) |
|
| 1038 | 6 | ||
| 1039 | 6 | /** |
|
| 1040 | * Splits on newlines and carriage returns, returning an array of Stringy |
||
| 1041 | 6 | * objects corresponding to the lines in the string. |
|
| 1042 | * |
||
| 1043 | 10 | * @return static [] An array of Stringy objects |
|
| 1044 | */ |
||
| 1045 | public function lines() |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | 10 | * Returns the longest common prefix between the string and $otherStr. |
|
| 1059 | 10 | * |
|
| 1060 | 10 | * @param string $otherStr Second string for comparison |
|
| 1061 | 10 | * |
|
| 1062 | * @return static Object with its $str being the longest common prefix |
||
| 1063 | */ |
||
| 1064 | 10 | public function longestCommonPrefix($otherStr) |
|
| 1082 | 8 | ||
| 1083 | 8 | /** |
|
| 1084 | 8 | * Returns the longest common suffix between the string and $otherStr. |
|
| 1085 | 8 | * |
|
| 1086 | 8 | * @param string $otherStr Second string for comparison |
|
| 1087 | 8 | * |
|
| 1088 | 8 | * @return static Object with its $str being the longest common suffix |
|
| 1089 | 8 | */ |
|
| 1090 | public function longestCommonSuffix($otherStr) |
||
| 1108 | 6 | ||
| 1109 | /** |
||
| 1110 | * Returns the longest common substring between the string and $otherStr. |
||
| 1111 | 6 | * In the case of ties, it returns that which occurs first. |
|
| 1112 | 6 | * |
|
| 1113 | * @param string $otherStr Second string for comparison |
||
| 1114 | 6 | * |
|
| 1115 | 3 | * @return static Object with its $str being the longest common substring |
|
| 1116 | */ |
||
| 1117 | public function longestCommonSubstring($otherStr) |
||
| 1161 | |||
| 1162 | 1 | /** |
|
| 1163 | * Returns whether or not a character exists at an index. Offsets may be |
||
| 1164 | * negative to count from the last character in the string. Implements |
||
| 1165 | * part of the ArrayAccess interface. |
||
| 1166 | * |
||
| 1167 | * @param mixed $offset The index to check |
||
| 1168 | * |
||
| 1169 | * @return boolean Whether or not the index exists |
||
| 1170 | */ |
||
| 1171 | public function offsetExists($offset) |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * Returns the character at the given index. Offsets may be negative to |
||
| 1186 | * count from the last character in the string. Implements part of the |
||
| 1187 | * ArrayAccess interface, and throws an OutOfBoundsException if the index |
||
| 1188 | * does not exist. |
||
| 1189 | * |
||
| 1190 | * @param mixed $offset The index from which to retrieve the char |
||
| 1191 | * |
||
| 1192 | * @return string The character at the specified index |
||
| 1193 | 13 | * @throws \OutOfBoundsException If the positive or negative offset does |
|
| 1194 | * not exist |
||
| 1195 | 13 | */ |
|
| 1196 | 1 | public function offsetGet($offset) |
|
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Implements part of the ArrayAccess interface, but throws an exception |
||
| 1215 | * when called. This maintains the immutability of Stringy objects. |
||
| 1216 | * |
||
| 1217 | * @param mixed $offset The index of the character |
||
| 1218 | * @param mixed $value Value to set |
||
| 1219 | * |
||
| 1220 | 10 | * @throws \Exception When called |
|
| 1221 | */ |
||
| 1222 | 10 | public function offsetSet($offset, $value) |
|
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Implements part of the ArrayAccess interface, but throws an exception |
||
| 1230 | * when called. This maintains the immutability of Stringy objects. |
||
| 1231 | * |
||
| 1232 | * @param mixed $offset The index of the character |
||
| 1233 | * |
||
| 1234 | * @throws \Exception When called |
||
| 1235 | 37 | */ |
|
| 1236 | public function offsetUnset($offset) |
||
| 1241 | 37 | ||
| 1242 | 37 | /** |
|
| 1243 | * Pads the string to a given length with $padStr. If length is less than |
||
| 1244 | 37 | * or equal to the length of the string, no padding takes places. The |
|
| 1245 | 3 | * default string used for padding is a space, and the default type (one of |
|
| 1246 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
||
| 1247 | * if $padType isn't one of those 3 values. |
||
| 1248 | 34 | * |
|
| 1249 | 34 | * @param int $length Desired string length after padding |
|
| 1250 | 34 | * @param string $padStr String used to pad, defaults to space |
|
| 1251 | 34 | * @param string $padType One of 'left', 'right', 'both' |
|
| 1252 | 34 | * |
|
| 1253 | 34 | * @return static Object with a padded $str |
|
| 1254 | 34 | * @throws \InvalidArgumentException If $padType isn't one of 'right', 'left' or 'both' |
|
| 1255 | 34 | */ |
|
| 1256 | 34 | public function pad($length, $padStr = ' ', $padType = 'right') |
|
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Returns a new string of a given length such that the beginning of the |
||
| 1276 | * string is padded. Alias for pad() with a $padType of 'left'. |
||
| 1277 | * |
||
| 1278 | * @param int $length Desired string length after padding |
||
| 1279 | * @param string $padStr String used to pad, defaults to space |
||
| 1280 | * |
||
| 1281 | * @return static String with left padding |
||
| 1282 | 13 | */ |
|
| 1283 | public function padLeft($length, $padStr = ' ') |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * Adds the specified amount of left and right padding to the given string. |
||
| 1290 | * The default character used is a space. |
||
| 1291 | * |
||
| 1292 | * @param int $left Length of left padding |
||
| 1293 | * @param int $right Length of right padding |
||
| 1294 | * @param string $padStr String used to pad |
||
| 1295 | * |
||
| 1296 | 14 | * @return static String with padding applied |
|
| 1297 | */ |
||
| 1298 | 14 | protected function applyPadding($left = 0, $right = 0, $padStr = ' ') |
|
| 1335 | |||
| 1336 | /** |
||
| 1337 | * Returns a new string of a given length such that the end of the string |
||
| 1338 | * is padded. Alias for pad() with a $padType of 'right'. |
||
| 1339 | * |
||
| 1340 | * @param int $length Desired string length after padding |
||
| 1341 | * @param string $padStr String used to pad, defaults to space |
||
| 1342 | 12 | * |
|
| 1343 | * @return static String with right padding |
||
| 1344 | 12 | */ |
|
| 1345 | public function padRight($length, $padStr = ' ') |
||
| 1349 | 8 | ||
| 1350 | /** |
||
| 1351 | * Returns a new string of a given length such that both sides of the |
||
| 1352 | 4 | * string are padded. Alias for pad() with a $padType of 'both'. |
|
| 1353 | * |
||
| 1354 | * @param int $length Desired string length after padding |
||
| 1355 | * @param string $padStr String used to pad, defaults to space |
||
| 1356 | * |
||
| 1357 | * @return static String with padding applied |
||
| 1358 | */ |
||
| 1359 | public function padBoth($length, $padStr = ' ') |
||
| 1365 | |||
| 1366 | 7 | /** |
|
| 1367 | * Returns a new string starting with $string. |
||
| 1368 | * |
||
| 1369 | * @param string $string The string to append |
||
| 1370 | * |
||
| 1371 | * @return static Object with appended $string |
||
| 1372 | */ |
||
| 1373 | public function prepend($string) |
||
| 1377 | |||
| 1378 | 28 | /** |
|
| 1379 | * Returns a new string with the prefix $substring removed, if present. |
||
| 1380 | 28 | * |
|
| 1381 | 21 | * @param string $substring The prefix to remove |
|
| 1382 | 21 | * |
|
| 1383 | 7 | * @return static Object having a $str without the prefix $substring |
|
| 1384 | */ |
||
| 1385 | View Code Duplication | public function removeLeft($substring) |
|
| 1397 | |||
| 1398 | 30 | /** |
|
| 1399 | * Returns a new string with the suffix $substring removed, if present. |
||
| 1400 | 30 | * |
|
| 1401 | 23 | * @param string $substring The suffix to remove |
|
| 1402 | 23 | * |
|
| 1403 | 7 | * @return static Object having a $str without the suffix $substring |
|
| 1404 | */ |
||
| 1405 | View Code Duplication | public function removeRight($substring) |
|
| 1417 | 16 | ||
| 1418 | /** |
||
| 1419 | 16 | * Returns a repeated string given a multiplier. |
|
| 1420 | * |
||
| 1421 | 16 | * @param int $multiplier The number of times to repeat the string |
|
| 1422 | * |
||
| 1423 | * @return static Object with a repeated str |
||
| 1424 | */ |
||
| 1425 | public function repeat($multiplier) |
||
| 1431 | |||
| 1432 | 16 | /** |
|
| 1433 | * Replaces all occurrences of $search in $str by $replacement. |
||
| 1434 | 16 | * |
|
| 1435 | * @param string $search The needle to search for |
||
| 1436 | 16 | * @param string $replacement The string to replace with |
|
| 1437 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 1438 | * |
||
| 1439 | * @return static Object with the resulting $str after the replacements |
||
| 1440 | */ |
||
| 1441 | View Code Duplication | public function replace($search, $replacement, $caseSensitive = true) |
|
| 1451 | |||
| 1452 | /** |
||
| 1453 | * Replaces all occurrences of $search in $str by $replacement. |
||
| 1454 | * |
||
| 1455 | * @param array $search The elements to search for |
||
| 1456 | * @param string|array $replacement The string to replace with |
||
| 1457 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
| 1458 | * |
||
| 1459 | * @return static Object with the resulting $str after the replacements |
||
| 1460 | */ |
||
| 1461 | View Code Duplication | public function replaceAll(array $search, $replacement, $caseSensitive = true) |
|
| 1471 | 19 | ||
| 1472 | 19 | /** |
|
| 1473 | * Replaces all occurrences of $search from the beginning of string with $replacement |
||
| 1474 | 19 | * |
|
| 1475 | * @param string $search |
||
| 1476 | * @param string $replacement |
||
| 1477 | 19 | * |
|
| 1478 | 19 | * @return static Object with the resulting $str after the replacements |
|
| 1479 | */ |
||
| 1480 | 12 | public function replaceBeginning($search, $replacement) |
|
| 1486 | |||
| 1487 | 19 | /** |
|
| 1488 | * Replaces all occurrences of $search from the ending of string with $replacement |
||
| 1489 | 19 | * |
|
| 1490 | * @param string $search |
||
| 1491 | * @param string $replacement |
||
| 1492 | * |
||
| 1493 | * @return static Object with the resulting $str after the replacements |
||
| 1494 | */ |
||
| 1495 | public function replaceEnding($search, $replacement) |
||
| 1501 | |||
| 1502 | 3 | /** |
|
| 1503 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". |
||
| 1504 | * If no match is found returns new empty Stringy object. |
||
| 1505 | * |
||
| 1506 | * @param string $needle |
||
| 1507 | * @param bool $beforeNeedle |
||
| 1508 | * |
||
| 1509 | * @return static |
||
| 1510 | */ |
||
| 1511 | View Code Duplication | public function substringOf($needle, $beforeNeedle = false) |
|
| 1523 | |||
| 1524 | /** |
||
| 1525 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". |
||
| 1526 | * If no match is found returns new empty Stringy object. |
||
| 1527 | * |
||
| 1528 | * @param string $needle |
||
| 1529 | * @param bool $beforeNeedle |
||
| 1530 | 1 | * |
|
| 1531 | * @return static |
||
| 1532 | 1 | */ |
|
| 1533 | View Code Duplication | public function substringOfIgnoreCase($needle, $beforeNeedle = false) |
|
| 1545 | |||
| 1546 | 1 | /** |
|
| 1547 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". |
||
| 1548 | 1 | * If no match is found returns new empty Stringy object. |
|
| 1549 | * |
||
| 1550 | * @param string $needle |
||
| 1551 | * @param bool $beforeNeedle |
||
| 1552 | * |
||
| 1553 | * @return static |
||
| 1554 | */ |
||
| 1555 | View Code Duplication | public function lastSubstringOf($needle, $beforeNeedle = false) |
|
| 1567 | |||
| 1568 | 6 | /** |
|
| 1569 | 6 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". |
|
| 1570 | 6 | * If no match is found returns new empty Stringy object. |
|
| 1571 | 6 | * |
|
| 1572 | 6 | * @param string $needle |
|
| 1573 | * @param bool $beforeNeedle |
||
| 1574 | 6 | * |
|
| 1575 | * @return static |
||
| 1576 | */ |
||
| 1577 | View Code Duplication | public function lastSubstringOfIgnoreCase($needle, $beforeNeedle = false) |
|
| 1589 | 1 | ||
| 1590 | /** |
||
| 1591 | 1 | * Returns a reversed string. A multibyte version of strrev(). |
|
| 1592 | 1 | * |
|
| 1593 | * @return static Object with a reversed $str |
||
| 1594 | */ |
||
| 1595 | 1 | public function reverse() |
|
| 1601 | 1 | ||
| 1602 | /** |
||
| 1603 | * Truncates the string to a given length, while ensuring that it does not |
||
| 1604 | * split words. If $substring is provided, and truncating occurs, the |
||
| 1605 | * string is further truncated so that the substring may be appended without |
||
| 1606 | * exceeding the desired length. |
||
| 1607 | * |
||
| 1608 | * @param int $length Desired length of the truncated string |
||
| 1609 | * @param string $substring The substring to append if it can fit |
||
| 1610 | * |
||
| 1611 | * @return static Object with the resulting $str after truncating |
||
| 1612 | */ |
||
| 1613 | public function safeTruncate($length, $substring = '') |
||
| 1642 | 1 | ||
| 1643 | 1 | /** |
|
| 1644 | 1 | * A multibyte string shuffle function. It returns a string with its |
|
| 1645 | * characters in random order. |
||
| 1646 | 1 | * |
|
| 1647 | * @return static Object with a shuffled $str |
||
| 1648 | */ |
||
| 1649 | public function shuffle() |
||
| 1655 | 1 | ||
| 1656 | 1 | /** |
|
| 1657 | 1 | * Converts the string into an URL slug. This includes replacing non-ASCII |
|
| 1658 | 1 | * characters with their closest ASCII equivalents, removing remaining |
|
| 1659 | 1 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
|
| 1660 | 1 | * $replacement. The replacement defaults to a single dash, and the string |
|
| 1661 | 1 | * is also converted to lowercase. |
|
| 1662 | 1 | * |
|
| 1663 | * @param string $replacement The string used to replace whitespace |
||
| 1664 | 1 | * @param string $language The language for the url |
|
| 1665 | 1 | * @param bool $strToLower string to lower |
|
| 1666 | 1 | * |
|
| 1667 | 1 | * @return static Object whose $str has been converted to an URL slug |
|
| 1668 | 1 | */ |
|
| 1669 | 1 | public function slugify($replacement = '-', $language = 'de', $strToLower = true) |
|
| 1675 | |||
| 1676 | /** |
||
| 1677 | 1 | * Remove css media-queries. |
|
| 1678 | * |
||
| 1679 | 1 | * @return static |
|
| 1680 | 1 | */ |
|
| 1681 | public function stripeCssMediaQueries() |
||
| 1687 | 1 | ||
| 1688 | 1 | /** |
|
| 1689 | 1 | * Strip all whitespace characters. This includes tabs and newline characters, |
|
| 1690 | * as well as multibyte whitespace such as the thin space and ideographic space. |
||
| 1691 | 1 | * |
|
| 1692 | 1 | * @return Stringy |
|
| 1693 | 1 | */ |
|
| 1694 | public function stripWhitespace() |
||
| 1698 | |||
| 1699 | /** |
||
| 1700 | * Remove empty html-tag. |
||
| 1701 | 1 | * |
|
| 1702 | * e.g.: <tag></tag> |
||
| 1703 | * |
||
| 1704 | * @return static |
||
| 1705 | */ |
||
| 1706 | public function stripeEmptyHtmlTags() |
||
| 1712 | 6 | ||
| 1713 | /** |
||
| 1714 | 6 | * Converts the string into an valid UTF-8 string. |
|
| 1715 | 1 | * |
|
| 1716 | 1 | * @return static |
|
| 1717 | */ |
||
| 1718 | 6 | public function utf8ify() |
|
| 1722 | |||
| 1723 | /** |
||
| 1724 | * escape html |
||
| 1725 | * |
||
| 1726 | * @return static |
||
| 1727 | */ |
||
| 1728 | public function escape() |
||
| 1738 | |||
| 1739 | /** |
||
| 1740 | * Create an extract from a text, so if the search-string was found, it will be centered in the output. |
||
| 1741 | * |
||
| 1742 | * @param string $search |
||
| 1743 | * @param int|null $length |
||
| 1744 | 6 | * @param string $ellipsis |
|
| 1745 | * |
||
| 1746 | 6 | * @return static |
|
| 1747 | */ |
||
| 1748 | 6 | public function extractText($search = '', $length = null, $ellipsis = '...') |
|
| 1864 | 4 | ||
| 1865 | |||
| 1866 | /** |
||
| 1867 | * remove xss from html |
||
| 1868 | * |
||
| 1869 | * @return static |
||
| 1870 | */ |
||
| 1871 | public function removeXss() |
||
| 1883 | |||
| 1884 | 5 | /** |
|
| 1885 | 2 | * remove html-break [br | \r\n | \r | \n | ...] |
|
| 1886 | * |
||
| 1887 | 5 | * @param string $replacement |
|
| 1888 | * |
||
| 1889 | 5 | * @return static |
|
| 1890 | */ |
||
| 1891 | 5 | public function removeHtmlBreak($replacement = '') |
|
| 1897 | |||
| 1898 | /** |
||
| 1899 | * remove html |
||
| 1900 | * |
||
| 1901 | * @param $allowableTags |
||
| 1902 | * |
||
| 1903 | * @return static |
||
| 1904 | 27 | */ |
|
| 1905 | public function removeHtml($allowableTags = null) |
||
| 1911 | |||
| 1912 | /** |
||
| 1913 | * Returns the substring beginning at $start, and up to, but not including |
||
| 1914 | * the index specified by $end. If $end is omitted, the function extracts |
||
| 1915 | * the remaining string. If $end is negative, it is computed from the end |
||
| 1916 | 7 | * of the string. |
|
| 1917 | * |
||
| 1918 | 7 | * @param int $start Initial index from which to begin extraction |
|
| 1919 | * @param int $end Optional index at which to end extraction |
||
| 1920 | * |
||
| 1921 | * @return static Object with its $str being the extracted substring |
||
| 1922 | */ |
||
| 1923 | public function slice($start, $end = null) |
||
| 1939 | |||
| 1940 | /** |
||
| 1941 | * Splits the string with the provided regular expression, returning an |
||
| 1942 | * array of Stringy objects. An optional integer $limit will truncate the |
||
| 1943 | * results. |
||
| 1944 | * |
||
| 1945 | * @param string $pattern The regex with which to split the string |
||
| 1946 | * @param int $limit Optional maximum number of results to return |
||
| 1947 | * |
||
| 1948 | 15 | * @return static [] An array of Stringy objects |
|
| 1949 | */ |
||
| 1950 | 15 | public function split($pattern, $limit = null) |
|
| 1984 | |||
| 1985 | /** |
||
| 1986 | * Surrounds $str with the given substring. |
||
| 1987 | * |
||
| 1988 | * @param string $substring The substring to add to both sides |
||
| 1989 | 6 | * |
|
| 1990 | * @return static Object whose $str had the substring both prepended and |
||
| 1991 | 6 | * appended |
|
| 1992 | 6 | */ |
|
| 1993 | public function surround($substring) |
||
| 1999 | |||
| 2000 | /** |
||
| 2001 | * Returns a case swapped version of the string. |
||
| 2002 | * |
||
| 2003 | * @return static Object whose $str has each character's case swapped |
||
| 2004 | */ |
||
| 2005 | public function swapCase() |
||
| 2013 | |||
| 2014 | /** |
||
| 2015 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
||
| 2016 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
||
| 2017 | * equivalents. |
||
| 2018 | * |
||
| 2019 | 5 | * @return static Object whose $str has those characters removed |
|
| 2020 | */ |
||
| 2021 | public function tidy() |
||
| 2027 | |||
| 2028 | /** |
||
| 2029 | * Returns a trimmed string with the first letter of each word capitalized. |
||
| 2030 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
| 2031 | * capitalized. |
||
| 2032 | * |
||
| 2033 | 5 | * @param array $ignore An array of words not to capitalize |
|
| 2034 | * |
||
| 2035 | 5 | * @return static Object with a titleized $str |
|
| 2036 | */ |
||
| 2037 | 5 | public function titleize($ignore = null) |
|
| 2058 | |||
| 2059 | /** |
||
| 2060 | * Converts all characters in the string to lowercase. An alias for PHP's |
||
| 2061 | * UTF8::strtolower(). |
||
| 2062 | * |
||
| 2063 | * @return static Object with all characters of $str being lowercase |
||
| 2064 | */ |
||
| 2065 | public function toLowerCase() |
||
| 2071 | 13 | ||
| 2072 | 11 | /** |
|
| 2073 | 11 | * Returns true if the string is base64 encoded, false otherwise. |
|
| 2074 | 2 | * |
|
| 2075 | * @return bool Whether or not $str is base64 encoded |
||
| 2076 | */ |
||
| 2077 | 13 | public function isBase64() |
|
| 2081 | |||
| 2082 | /** |
||
| 2083 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
||
| 2084 | * replaced with their closest ASCII counterparts, and the rest are removed |
||
| 2085 | * unless instructed otherwise. |
||
| 2086 | * |
||
| 2087 | * @param $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance</p> |
||
| 2088 | * |
||
| 2089 | * @return static Object whose $str contains only ASCII characters |
||
| 2090 | 22 | */ |
|
| 2091 | public function toAscii($strict = false) |
||
| 2097 | |||
| 2098 | 18 | /** |
|
| 2099 | 18 | * Returns a boolean representation of the given logical string value. |
|
| 2100 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
| 2101 | 18 | * 'off', and 'no' will return false. In all instances, case is ignored. |
|
| 2102 | 18 | * For other numeric strings, their sign will determine the return value. |
|
| 2103 | * In addition, blank strings consisting of only whitespace will return |
||
| 2104 | 18 | * false. For all other strings, the return value is a result of a |
|
| 2105 | * boolean cast. |
||
| 2106 | * |
||
| 2107 | * @return bool A boolean value for the string |
||
| 2108 | */ |
||
| 2109 | public function toBoolean() |
||
| 2133 | |||
| 2134 | /** |
||
| 2135 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
||
| 2136 | * |
||
| 2137 | * @return string |
||
| 2138 | */ |
||
| 2139 | 32 | public function toString() |
|
| 2143 | 32 | ||
| 2144 | /** |
||
| 2145 | 32 | * Converts each tab in the string to some number of spaces, as defined by |
|
| 2146 | 32 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
|
| 2147 | * |
||
| 2148 | 27 | * @param int $tabLength Number of spaces to replace each tab with |
|
| 2149 | 27 | * |
|
| 2150 | * @return static Object whose $str has had tabs switched to spaces |
||
| 2151 | 1 | */ |
|
| 2152 | View Code Duplication | public function toSpaces($tabLength = 4) |
|
| 2159 | |||
| 2160 | 6 | /** |
|
| 2161 | 32 | * Converts each occurrence of some consecutive number of spaces, as |
|
| 2162 | 32 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
|
| 2163 | 32 | * are converted to a tab. |
|
| 2164 | * |
||
| 2165 | 32 | * @param int $tabLength Number of spaces to replace with a tab |
|
| 2166 | * |
||
| 2167 | * @return static Object whose $str has had spaces switched to tabs |
||
| 2168 | */ |
||
| 2169 | View Code Duplication | public function toTabs($tabLength = 4) |
|
| 2176 | |||
| 2177 | 20 | /** |
|
| 2178 | 20 | * Converts the first character of each word in the string to uppercase. |
|
| 2179 | 20 | * |
|
| 2180 | * @return static Object with all characters of $str being title-cased |
||
| 2181 | 20 | */ |
|
| 2182 | 20 | public function toTitleCase() |
|
| 2189 | |||
| 2190 | 4 | /** |
|
| 2191 | * Converts all characters in the string to uppercase. An alias for PHP's |
||
| 2192 | 20 | * UTF8::strtoupper(). |
|
| 2193 | * |
||
| 2194 | 20 | * @return static Object with all characters of $str being uppercase |
|
| 2195 | */ |
||
| 2196 | 20 | public function toUpperCase() |
|
| 2202 | 20 | ||
| 2203 | /** |
||
| 2204 | 20 | * Returns a string with whitespace removed from the start of the string. |
|
| 2205 | 20 | * Supports the removal of unicode whitespace. Accepts an optional |
|
| 2206 | 20 | * string of characters to strip instead of the defaults. |
|
| 2207 | 20 | * |
|
| 2208 | * @param string $chars Optional string of characters to strip |
||
| 2209 | 20 | * |
|
| 2210 | * @return static Object with a trimmed $str |
||
| 2211 | 20 | */ |
|
| 2212 | 20 | View Code Duplication | public function trimLeft($chars = null) |
| 2222 | 37 | ||
| 2223 | /** |
||
| 2224 | 37 | * Returns a string with whitespace removed from the end of the string. |
|
| 2225 | 37 | * Supports the removal of unicode whitespace. Accepts an optional |
|
| 2226 | * string of characters to strip instead of the defaults. |
||
| 2227 | 37 | * |
|
| 2228 | * @param string $chars Optional string of characters to strip |
||
| 2229 | 37 | * |
|
| 2230 | * @return static Object with a trimmed $str |
||
| 2231 | */ |
||
| 2232 | View Code Duplication | public function trimRight($chars = null) |
|
| 2242 | 4 | ||
| 2243 | /** |
||
| 2244 | 4 | * Truncates the string to a given length. If $substring is provided, and |
|
| 2245 | * truncating occurs, the string is further truncated so that the substring |
||
| 2246 | * may be appended without exceeding the desired length. |
||
| 2247 | * |
||
| 2248 | * @param int $length Desired length of the truncated string |
||
| 2249 | * @param string $substring The substring to append if it can fit |
||
| 2250 | * |
||
| 2251 | * @return static Object with the resulting $str after truncating |
||
| 2252 | */ |
||
| 2253 | View Code Duplication | public function truncate($length, $substring = '') |
|
| 2269 | |||
| 2270 | /** |
||
| 2271 | * Returns a lowercase and trimmed string separated by underscores. |
||
| 2272 | * Underscores are inserted before uppercase characters (with the exception |
||
| 2273 | * of the first character of the string), and in place of spaces as well as |
||
| 2274 | * dashes. |
||
| 2275 | 1 | * |
|
| 2276 | * @return static Object with an underscored $str |
||
| 2277 | 1 | */ |
|
| 2278 | 1 | public function underscored() |
|
| 2282 | 1 | ||
| 2283 | 1 | /** |
|
| 2284 | 1 | * Returns an UpperCamelCase version of the supplied string. It trims |
|
| 2285 | 1 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
|
| 2286 | 1 | * and underscores, and removes spaces, dashes, underscores. |
|
| 2287 | 1 | * |
|
| 2288 | 1 | * @return static Object with $str in UpperCamelCase |
|
| 2289 | 1 | */ |
|
| 2290 | public function upperCamelize() |
||
| 2294 | |||
| 2295 | /** |
||
| 2296 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
| 2297 | * capitalizes letters following digits, spaces, dashes and underscores, |
||
| 2298 | * and removes spaces, dashes, as well as underscores. |
||
| 2299 | * |
||
| 2300 | 1 | * @return static Object with $str in camelCase |
|
| 2301 | */ |
||
| 2302 | 1 | public function camelize() |
|
| 2330 | 1 | ||
| 2331 | /** |
||
| 2332 | * Convert a string to e.g.: "snake_case" |
||
| 2333 | 1 | * |
|
| 2334 | 1 | * @return static Object with $str in snake_case |
|
| 2335 | 1 | */ |
|
| 2336 | 1 | public function snakeize() |
|
| 2379 | 39 | ||
| 2380 | 39 | /** |
|
| 2381 | * Converts the first character of the string to lower case. |
||
| 2382 | 39 | * |
|
| 2383 | * @return static Object with the first character of $str being lower case |
||
| 2384 | */ |
||
| 2385 | View Code Duplication | public function lowerCaseFirst() |
|
| 2394 | 7 | ||
| 2395 | 7 | /** |
|
| 2396 | 7 | * Shorten the string after $length, but also after the next word. |
|
| 2397 | * |
||
| 2398 | 7 | * @param int $length |
|
| 2399 | * @param string $strAddOn |
||
| 2400 | * |
||
| 2401 | * @return static |
||
| 2402 | */ |
||
| 2403 | public function shortenAfterWord($length, $strAddOn = '...') |
||
| 2409 | 39 | ||
| 2410 | /** |
||
| 2411 | * Line-Wrap the string after $limit, but also after the next word. |
||
| 2412 | 39 | * |
|
| 2413 | 39 | * @param int $limit |
|
| 2414 | * |
||
| 2415 | * @return static |
||
| 2416 | */ |
||
| 2417 | 39 | public function lineWrapAfterWord($limit) |
|
| 2429 | 39 | ||
| 2430 | 39 | /** |
|
| 2431 | 39 | * Gets the substring after the first occurrence of a separator. |
|
| 2432 | 39 | * If no match is found returns new empty Stringy object. |
|
| 2433 | 39 | * |
|
| 2434 | 39 | * @param string $separator |
|
| 2435 | 39 | * |
|
| 2436 | 39 | * @return static |
|
| 2437 | 39 | */ |
|
| 2438 | 39 | View Code Duplication | public function afterFirst($separator) |
| 2462 | 13 | ||
| 2463 | 13 | /** |
|
| 2464 | 13 | * Gets the substring after the first occurrence of a separator. |
|
| 2465 | 2 | * If no match is found returns new empty Stringy object. |
|
| 2466 | 2 | * |
|
| 2467 | 13 | * @param string $separator |
|
| 2468 | 13 | * |
|
| 2469 | * @return static |
||
| 2470 | 13 | */ |
|
| 2471 | 13 | View Code Duplication | public function afterFirstIgnoreCase($separator) |
| 2495 | |||
| 2496 | /** |
||
| 2497 | * Gets the substring after the last occurrence of a separator. |
||
| 2498 | * If no match is found returns new empty Stringy object. |
||
| 2499 | * |
||
| 2500 | * @param string $separator |
||
| 2501 | * |
||
| 2502 | * @return static |
||
| 2503 | */ |
||
| 2504 | View Code Duplication | public function afterLastIgnoreCase($separator) |
|
| 2529 | |||
| 2530 | /** |
||
| 2531 | * Gets the substring after the last occurrence of a separator. |
||
| 2532 | * If no match is found returns new empty Stringy object. |
||
| 2533 | * |
||
| 2534 | * @param string $separator |
||
| 2535 | * |
||
| 2536 | * @return static |
||
| 2537 | */ |
||
| 2538 | View Code Duplication | public function afterLast($separator) |
|
| 2563 | |||
| 2564 | /** |
||
| 2565 | * Gets the substring before the first occurrence of a separator. |
||
| 2566 | * If no match is found returns new empty Stringy object. |
||
| 2567 | * |
||
| 2568 | * @param string $separator |
||
| 2569 | * |
||
| 2570 | * @return static |
||
| 2571 | */ |
||
| 2572 | View Code Duplication | public function beforeFirst($separator) |
|
| 2597 | |||
| 2598 | /** |
||
| 2599 | * Gets the substring before the first occurrence of a separator. |
||
| 2600 | * If no match is found returns new empty Stringy object. |
||
| 2601 | * |
||
| 2602 | * @param string $separator |
||
| 2603 | * |
||
| 2604 | * @return static |
||
| 2605 | */ |
||
| 2606 | View Code Duplication | public function beforeFirstIgnoreCase($separator) |
|
| 2631 | |||
| 2632 | /** |
||
| 2633 | * Gets the substring before the last occurrence of a separator. |
||
| 2634 | * If no match is found returns new empty Stringy object. |
||
| 2635 | * |
||
| 2636 | * @param string $separator |
||
| 2637 | * |
||
| 2638 | * @return static |
||
| 2639 | */ |
||
| 2640 | View Code Duplication | public function beforeLast($separator) |
|
| 2665 | |||
| 2666 | /** |
||
| 2667 | * Gets the substring before the last occurrence of a separator. |
||
| 2668 | * If no match is found returns new empty Stringy object. |
||
| 2669 | * |
||
| 2670 | * @param string $separator |
||
| 2671 | * |
||
| 2672 | * @return static |
||
| 2673 | */ |
||
| 2674 | View Code Duplication | public function beforeLastIgnoreCase($separator) |
|
| 2699 | |||
| 2700 | /** |
||
| 2701 | * Returns the string with the first letter of each word capitalized, |
||
| 2702 | * except for when the word is a name which shouldn't be capitalized. |
||
| 2703 | * |
||
| 2704 | * @return static Object with $str capitalized |
||
| 2705 | */ |
||
| 2706 | public function capitalizePersonalName() |
||
| 2714 | |||
| 2715 | /** |
||
| 2716 | * @param string $word |
||
| 2717 | * |
||
| 2718 | * @return string |
||
| 2719 | */ |
||
| 2720 | protected function capitalizeWord($word) |
||
| 2730 | |||
| 2731 | /** |
||
| 2732 | * Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius"). |
||
| 2733 | * |
||
| 2734 | * @param string $names |
||
| 2735 | * @param string $delimiter |
||
| 2736 | * |
||
| 2737 | * @return string |
||
| 2738 | */ |
||
| 2739 | protected function capitalizePersonalNameByDelimiter($names, $delimiter) |
||
| 2815 | } |
||
| 2816 |
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: