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 | ||
| 17 | class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess | ||
| 18 | { | ||
| 19 | /** | ||
| 20 | * An instance's string. | ||
| 21 | * | ||
| 22 | * @var string | ||
| 23 | */ | ||
| 24 | protected $str; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * The string's encoding, which should be one of the mbstring module's | ||
| 28 | * supported encodings. | ||
| 29 | * | ||
| 30 | * @var string | ||
| 31 | */ | ||
| 32 | protected $encoding; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * Initializes a Stringy object and assigns both str and encoding properties | ||
| 36 | * the supplied values. $str is cast to a string prior to assignment, and if | ||
| 37 | * $encoding is not specified, it defaults to mb_internal_encoding(). Throws | ||
| 38 | * an InvalidArgumentException if the first argument is an array or object | ||
| 39 | * without a __toString method. | ||
| 40 | * | ||
| 41 | * @param mixed $str [optional] <p>Value to modify, after being cast to string. Default: ''</p> | ||
| 42 | * @param string $encoding [optional] <p>The character encoding.</p> | ||
| 43 | * | ||
| 44 | * @throws \InvalidArgumentException <p>if an array or object without a | ||
| 45 | * __toString method is passed as the first argument</p> | ||
| 46 | */ | ||
| 47 | 1152 | public function __construct($str = '', string $encoding = null) | |
| 76 | |||
| 77 | /** | ||
| 78 | * Returns the value in $str. | ||
| 79 | * | ||
| 80 | * @return string <p>The current value of the $str property.</p> | ||
| 81 | */ | ||
| 82 | 221 | public function __toString() | |
| 86 | |||
| 87 | /** | ||
| 88 | * Returns a new string with $string appended. | ||
| 89 | * | ||
| 90 | * @param string $string <p>The string to append.</p> | ||
| 91 | * | ||
| 92 | * @return static <p>Object with appended $string.</p> | ||
| 93 | */ | ||
| 94 | 5 | public function append(string $string): self | |
| 98 | |||
| 99 | /** | ||
| 100 | * Append an password (limited to chars that are good readable). | ||
| 101 | * | ||
| 102 | * @param int $length <p>Length of the random string.</p> | ||
| 103 | * | ||
| 104 | * @return static <p>Object with appended password.</p> | ||
| 105 | */ | ||
| 106 | 1 | public function appendPassword(int $length): self | |
| 112 | |||
| 113 | /** | ||
| 114 | * Append an unique identifier. | ||
| 115 | * | ||
| 116 | * @param string|int $entropyExtra [optional] <p>Extra entropy via a string or int value.</p> | ||
| 117 | * @param bool $md5 [optional] <p>Return the unique identifier as md5-hash? Default: true</p> | ||
| 118 | * | ||
| 119 | * @return static <p>Object with appended unique identifier as md5-hash.</p> | ||
| 120 | */ | ||
| 121 | 1 | public function appendUniqueIdentifier($entropyExtra = '', bool $md5 = true): self | |
| 137 | |||
| 138 | /** | ||
| 139 | * Append an random string. | ||
| 140 | * | ||
| 141 | * @param int $length <p>Length of the random string.</p> | ||
| 142 | * @param string $possibleChars [optional] <p>Characters string for the random selection.</p> | ||
| 143 | * | ||
| 144 | * @return static <p>Object with appended random string.</p> | ||
| 145 | */ | ||
| 146 | 2 | public function appendRandomString(int $length, string $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'): self | |
| 166 | |||
| 167 | /** | ||
| 168 | * Creates a Stringy object and assigns both str and encoding properties | ||
| 169 | * the supplied values. $str is cast to a string prior to assignment, and if | ||
| 170 | * $encoding is not specified, it defaults to mb_internal_encoding(). It | ||
| 171 | * then returns the initialized object. Throws an InvalidArgumentException | ||
| 172 | * if the first argument is an array or object without a __toString method. | ||
| 173 | * | ||
| 174 | * @param mixed $str [optional] <p>Value to modify, after being cast to string. Default: ''</p> | ||
| 175 | * @param string $encoding [optional] <p>The character encoding.</p> | ||
| 176 | * | ||
| 177 | * @return static <p>A Stringy object.</p> | ||
| 178 | * | ||
| 179 | * @throws \InvalidArgumentException <p>if an array or object without a | ||
| 180 | * __toString method is passed as the first argument</p> | ||
| 181 | */ | ||
| 182 | 1142 | public static function create($str = '', string $encoding = null): self | |
| 186 | |||
| 187 | /** | ||
| 188 | * Returns the substring between $start and $end, if found, or an empty | ||
| 189 | * string. An optional offset may be supplied from which to begin the | ||
| 190 | * search for the start string. | ||
| 191 | * | ||
| 192 | * @param string $start <p>Delimiter marking the start of the substring.</p> | ||
| 193 | * @param string $end <p>Delimiter marking the end of the substring.</p> | ||
| 194 | * @param int $offset [optional] <p>Index from which to begin the search. Default: 0</p> | ||
| 195 | * | ||
| 196 | * @return static <p>Object whose $str is a substring between $start and $end.</p> | ||
| 197 | */ | ||
| 198 | 16 | public function between(string $start, string $end, int $offset = 0): self | |
| 213 | |||
| 214 | /** | ||
| 215 | * Returns the index of the first occurrence of $needle in the string, | ||
| 216 | * and false if not found. Accepts an optional offset from which to begin | ||
| 217 | * the search. | ||
| 218 | * | ||
| 219 | * @param string $needle <p>Substring to look for.</p> | ||
| 220 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> | ||
| 221 | * | ||
| 222 | * @return int|false <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> | ||
| 223 | */ | ||
| 224 | 29 | public function indexOf(string $needle, int $offset = 0) | |
| 228 | |||
| 229 | /** | ||
| 230 | * Returns the index of the first occurrence of $needle in the string, | ||
| 231 | * and false if not found. Accepts an optional offset from which to begin | ||
| 232 | * the search. | ||
| 233 | * | ||
| 234 | * @param string $needle <p>Substring to look for.</p> | ||
| 235 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> | ||
| 236 | * | ||
| 237 | * @return int|false <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> | ||
| 238 | */ | ||
| 239 | 2 | public function indexOfIgnoreCase(string $needle, int $offset = 0) | |
| 243 | |||
| 244 | /** | ||
| 245 | * Returns the substring beginning at $start with the specified $length. | ||
| 246 | * It differs from the UTF8::substr() function in that providing a $length of | ||
| 247 | * null will return the rest of the string, rather than an empty string. | ||
| 248 | * | ||
| 249 | * @param int $start <p>Position of the first character to use.</p> | ||
| 250 | * @param int $length [optional] <p>Maximum number of characters used. Default: null</p> | ||
| 251 | * | ||
| 252 | * @return static <p>Object with its $str being the substring.</p> | ||
| 253 | */ | ||
| 254 | 65 | public function substr(int $start, int $length = null): self | |
| 264 | |||
| 265 | /** | ||
| 266 | * Returns the length of the string. | ||
| 267 | * | ||
| 268 | * @return int <p>The number of characters in $str given the encoding.</p> | ||
| 269 | */ | ||
| 270 | 293 | public function length(): int | |
| 274 | |||
| 275 | /** | ||
| 276 | * Trims the string and replaces consecutive whitespace characters with a | ||
| 277 | * single space. This includes tabs and newline characters, as well as | ||
| 278 | * multibyte whitespace such as the thin space and ideographic space. | ||
| 279 | * | ||
| 280 | * @return static <p>Object with a trimmed $str and condensed whitespace.</p> | ||
| 281 | */ | ||
| 282 | 52 | public function collapseWhitespace(): self | |
| 286 | |||
| 287 | /** | ||
| 288 | * Returns a string with whitespace removed from the start and end of the | ||
| 289 | * string. Supports the removal of unicode whitespace. Accepts an optional | ||
| 290 | * string of characters to strip instead of the defaults. | ||
| 291 | * | ||
| 292 | * @param string $chars [optional] <p>String of characters to strip. Default: null</p> | ||
| 293 | * | ||
| 294 | * @return static <p>Object with a trimmed $str.</p> | ||
| 295 | */ | ||
| 296 | 188 | View Code Duplication | public function trim(string $chars = null): self | 
| 306 | |||
| 307 | /** | ||
| 308 | * Replaces all occurrences of $pattern in $str by $replacement. | ||
| 309 | * | ||
| 310 | * @param string $pattern <p>The regular expression pattern.</p> | ||
| 311 | * @param string $replacement <p>The string to replace with.</p> | ||
| 312 | * @param string $options [optional] <p>Matching conditions to be used.</p> | ||
| 313 | * @param string $delimiter [optional] <p>Delimiter the the regex. Default: '/'</p> | ||
| 314 | * | ||
| 315 | * @return static <p>Object with the result2ing $str after the replacements.</p> | ||
| 316 | */ | ||
| 317 | 259 | public function regexReplace(string $pattern, string $replacement, string $options = '', string $delimiter = '/'): self | |
| 336 | |||
| 337 | /** | ||
| 338 | * Returns true if the string contains all $needles, false otherwise. By | ||
| 339 | * default the comparison is case-sensitive, but can be made insensitive by | ||
| 340 | * setting $caseSensitive to false. | ||
| 341 | * | ||
| 342 | * @param array $needles <p>SubStrings to look for.</p> | ||
| 343 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 344 | * | ||
| 345 | * @return bool <p>Whether or not $str contains $needle.</p> | ||
| 346 | */ | ||
| 347 | 43 | View Code Duplication | public function containsAll(array $needles, bool $caseSensitive = true): bool | 
| 362 | |||
| 363 | /** | ||
| 364 | * Returns true if the string contains $needle, false otherwise. By default | ||
| 365 | * the comparison is case-sensitive, but can be made insensitive by setting | ||
| 366 | * $caseSensitive to false. | ||
| 367 | * | ||
| 368 | * @param string $needle <p>Substring to look for.</p> | ||
| 369 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 370 | * | ||
| 371 | * @return bool <p>Whether or not $str contains $needle.</p> | ||
| 372 | */ | ||
| 373 | 105 | public function contains(string $needle, bool $caseSensitive = true): bool | |
| 383 | |||
| 384 | /** | ||
| 385 | * Returns true if the string contains any $needles, false otherwise. By | ||
| 386 | * default the comparison is case-sensitive, but can be made insensitive by | ||
| 387 | * setting $caseSensitive to false. | ||
| 388 | * | ||
| 389 | * @param array $needles <p>SubStrings to look for.</p> | ||
| 390 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 391 | * | ||
| 392 | * @return bool <p>Whether or not $str contains $needle.</p> | ||
| 393 | */ | ||
| 394 | 43 | View Code Duplication | public function containsAny(array $needles, bool $caseSensitive = true): bool | 
| 409 | |||
| 410 | /** | ||
| 411 | * Returns the length of the string, implementing the countable interface. | ||
| 412 | * | ||
| 413 | * @return int <p>The number of characters in the string, given the encoding.</p> | ||
| 414 | */ | ||
| 415 | 1 | public function count(): int | |
| 419 | |||
| 420 | /** | ||
| 421 | * Returns the number of occurrences of $substring in the given string. | ||
| 422 | * By default, the comparison is case-sensitive, but can be made insensitive | ||
| 423 | * by setting $caseSensitive to false. | ||
| 424 | * | ||
| 425 | * @param string $substring <p>The substring to search for.</p> | ||
| 426 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 427 | * | ||
| 428 | * @return int|false <p>This functions returns an integer or false if there isn't a string.</p> | ||
| 429 | */ | ||
| 430 | 15 | public function countSubstr(string $substring, bool $caseSensitive = true) | |
| 441 | |||
| 442 | /** | ||
| 443 | * Returns a lowercase and trimmed string separated by dashes. Dashes are | ||
| 444 | * inserted before uppercase characters (with the exception of the first | ||
| 445 | * character of the string), and in place of spaces as well as underscores. | ||
| 446 | * | ||
| 447 | * @return static <p>Object with a dasherized $str</p> | ||
| 448 | */ | ||
| 449 | 19 | public function dasherize(): self | |
| 453 | |||
| 454 | /** | ||
| 455 | * Returns a lowercase and trimmed string separated by the given delimiter. | ||
| 456 | * Delimiters are inserted before uppercase characters (with the exception | ||
| 457 | * of the first character of the string), and in place of spaces, dashes, | ||
| 458 | * and underscores. Alpha delimiters are not converted to lowercase. | ||
| 459 | * | ||
| 460 | * @param string $delimiter <p>Sequence used to separate parts of the string.</p> | ||
| 461 | * | ||
| 462 | * @return static <p>Object with a delimited $str.</p> | ||
| 463 | */ | ||
| 464 | 49 | public function delimit(string $delimiter): self | |
| 476 | |||
| 477 | /** | ||
| 478 | * Ensures that the string begins with $substring. If it doesn't, it's | ||
| 479 | * prepended. | ||
| 480 | * | ||
| 481 | * @param string $substring <p>The substring to add if not present.</p> | ||
| 482 | * | ||
| 483 | * @return static <p>Object with its $str prefixed by the $substring.</p> | ||
| 484 | */ | ||
| 485 | 10 | View Code Duplication | public function ensureLeft(string $substring): self | 
| 495 | |||
| 496 | /** | ||
| 497 | * Returns true if the string begins with $substring, false otherwise. By | ||
| 498 | * default, the comparison is case-sensitive, but can be made insensitive | ||
| 499 | * by setting $caseSensitive to false. | ||
| 500 | * | ||
| 501 | * @param string $substring <p>The substring to look for.</p> | ||
| 502 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 503 | * | ||
| 504 | * @return bool <p>Whether or not $str starts with $substring.</p> | ||
| 505 | */ | ||
| 506 | 45 | public function startsWith(string $substring, bool $caseSensitive = true): bool | |
| 517 | |||
| 518 | /** | ||
| 519 | * Returns true if the string begins with any of $substrings, false otherwise. | ||
| 520 | * By default the comparison is case-sensitive, but can be made insensitive by | ||
| 521 | * setting $caseSensitive to false. | ||
| 522 | * | ||
| 523 | * @param array $substrings <p>Substrings to look for.</p> | ||
| 524 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 525 | * | ||
| 526 | * @return bool <p>Whether or not $str starts with $substring.</p> | ||
| 527 | */ | ||
| 528 | 12 | View Code Duplication | public function startsWithAny(array $substrings, bool $caseSensitive = true): bool | 
| 542 | |||
| 543 | /** | ||
| 544 | * Ensures that the string ends with $substring. If it doesn't, it's appended. | ||
| 545 | * | ||
| 546 | * @param string $substring <p>The substring to add if not present.</p> | ||
| 547 | * | ||
| 548 | * @return static <p>Object with its $str suffixed by the $substring.</p> | ||
| 549 | */ | ||
| 550 | 10 | View Code Duplication | public function ensureRight(string $substring): self | 
| 560 | |||
| 561 | /** | ||
| 562 | * Returns true if the string ends with $substring, false otherwise. By | ||
| 563 | * default, the comparison is case-sensitive, but can be made insensitive | ||
| 564 | * by setting $caseSensitive to false. | ||
| 565 | * | ||
| 566 | * @param string $substring <p>The substring to look for.</p> | ||
| 567 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 568 | * | ||
| 569 | * @return bool <p>Whether or not $str ends with $substring.</p> | ||
| 570 | */ | ||
| 571 | 44 | public function endsWith(string $substring, bool $caseSensitive = true): bool | |
| 590 | |||
| 591 | /** | ||
| 592 | * Returns true if the string ends with any of $substrings, false otherwise. | ||
| 593 | * By default, the comparison is case-sensitive, but can be made insensitive | ||
| 594 | * by setting $caseSensitive to false. | ||
| 595 | * | ||
| 596 | * @param string[] $substrings <p>Substrings to look for.</p> | ||
| 597 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 598 | * | ||
| 599 | * @return bool <p>Whether or not $str ends with $substring.</p> | ||
| 600 | */ | ||
| 601 | 11 | View Code Duplication | public function endsWithAny(array $substrings, bool $caseSensitive = true): bool | 
| 615 | |||
| 616 | /** | ||
| 617 | * Returns the first $n characters of the string. | ||
| 618 | * | ||
| 619 | * @param int $n <p>Number of characters to retrieve from the start.</p> | ||
| 620 | * | ||
| 621 | * @return static <p>Object with its $str being the first $n chars.</p> | ||
| 622 | */ | ||
| 623 | 13 | View Code Duplication | public function first(int $n): self | 
| 635 | |||
| 636 | /** | ||
| 637 | * Returns the encoding used by the Stringy object. | ||
| 638 | * | ||
| 639 | * @return string <p>The current value of the $encoding property.</p> | ||
| 640 | */ | ||
| 641 | 3 | public function getEncoding(): string | |
| 645 | |||
| 646 | /** | ||
| 647 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate | ||
| 648 | * interface. The ArrayIterator's constructor is passed an array of chars | ||
| 649 | * in the multibyte string. This enables the use of foreach with instances | ||
| 650 | * of Stringy\Stringy. | ||
| 651 | * | ||
| 652 | * @return \ArrayIterator <p>An iterator for the characters in the string.</p> | ||
| 653 | */ | ||
| 654 | 1 | public function getIterator(): \ArrayIterator | |
| 658 | |||
| 659 | /** | ||
| 660 | * Returns an array consisting of the characters in the string. | ||
| 661 | * | ||
| 662 | * @return array <p>An array of string chars.</p> | ||
| 663 | */ | ||
| 664 | 4 | public function chars(): array | |
| 676 | |||
| 677 | /** | ||
| 678 | * Returns the character at $index, with indexes starting at 0. | ||
| 679 | * | ||
| 680 | * @param int $index <p>Position of the character.</p> | ||
| 681 | * | ||
| 682 | * @return static <p>The character at $index.</p> | ||
| 683 | */ | ||
| 684 | 11 | public function at(int $index): self | |
| 688 | |||
| 689 | /** | ||
| 690 | * Returns true if the string contains a lower case char, false otherwise. | ||
| 691 | * | ||
| 692 | * @return bool <p>Whether or not the string contains a lower case character.</p> | ||
| 693 | */ | ||
| 694 | 12 | public function hasLowerCase(): bool | |
| 698 | |||
| 699 | /** | ||
| 700 | * Returns true if $str matches the supplied pattern, false otherwise. | ||
| 701 | * | ||
| 702 | * @param string $pattern <p>Regex pattern to match against.</p> | ||
| 703 | * | ||
| 704 | * @return bool <p>Whether or not $str matches the pattern.</p> | ||
| 705 | */ | ||
| 706 | 103 | protected function matchesPattern(string $pattern): bool | |
| 714 | |||
| 715 | /** | ||
| 716 | * Returns true if the string contains an upper case char, false otherwise. | ||
| 717 | * | ||
| 718 | * @return bool <p>Whether or not the string contains an upper case character.</p> | ||
| 719 | */ | ||
| 720 | 12 | public function hasUpperCase(): bool | |
| 724 | |||
| 725 | /** | ||
| 726 | * Convert all HTML entities to their applicable characters. | ||
| 727 | * | ||
| 728 | * @param int $flags [optional] <p> | ||
| 729 | * A bitmask of one or more of the following flags, which specify how to handle quotes and | ||
| 730 | * which document type to use. The default is ENT_COMPAT. | ||
| 731 | * <table> | ||
| 732 | * Available <i>flags</i> constants | ||
| 733 | * <tr valign="top"> | ||
| 734 | * <td>Constant Name</td> | ||
| 735 | * <td>Description</td> | ||
| 736 | * </tr> | ||
| 737 | * <tr valign="top"> | ||
| 738 | * <td><b>ENT_COMPAT</b></td> | ||
| 739 | * <td>Will convert double-quotes and leave single-quotes alone.</td> | ||
| 740 | * </tr> | ||
| 741 | * <tr valign="top"> | ||
| 742 | * <td><b>ENT_QUOTES</b></td> | ||
| 743 | * <td>Will convert both double and single quotes.</td> | ||
| 744 | * </tr> | ||
| 745 | * <tr valign="top"> | ||
| 746 | * <td><b>ENT_NOQUOTES</b></td> | ||
| 747 | * <td>Will leave both double and single quotes unconverted.</td> | ||
| 748 | * </tr> | ||
| 749 | * <tr valign="top"> | ||
| 750 | * <td><b>ENT_HTML401</b></td> | ||
| 751 | * <td> | ||
| 752 | * Handle code as HTML 4.01. | ||
| 753 | * </td> | ||
| 754 | * </tr> | ||
| 755 | * <tr valign="top"> | ||
| 756 | * <td><b>ENT_XML1</b></td> | ||
| 757 | * <td> | ||
| 758 | * Handle code as XML 1. | ||
| 759 | * </td> | ||
| 760 | * </tr> | ||
| 761 | * <tr valign="top"> | ||
| 762 | * <td><b>ENT_XHTML</b></td> | ||
| 763 | * <td> | ||
| 764 | * Handle code as XHTML. | ||
| 765 | * </td> | ||
| 766 | * </tr> | ||
| 767 | * <tr valign="top"> | ||
| 768 | * <td><b>ENT_HTML5</b></td> | ||
| 769 | * <td> | ||
| 770 | * Handle code as HTML 5. | ||
| 771 | * </td> | ||
| 772 | * </tr> | ||
| 773 | * </table> | ||
| 774 | * </p> | ||
| 775 | * | ||
| 776 | * @return static <p>Object with the resulting $str after being html decoded.</p> | ||
| 777 | */ | ||
| 778 | 5 | public function htmlDecode(int $flags = ENT_COMPAT): self | |
| 784 | |||
| 785 | /** | ||
| 786 | * Convert all applicable characters to HTML entities. | ||
| 787 | * | ||
| 788 | * @param int $flags [optional] <p> | ||
| 789 | * A bitmask of one or more of the following flags, which specify how to handle quotes and | ||
| 790 | * which document type to use. The default is ENT_COMPAT. | ||
| 791 | * <table> | ||
| 792 | * Available <i>flags</i> constants | ||
| 793 | * <tr valign="top"> | ||
| 794 | * <td>Constant Name</td> | ||
| 795 | * <td>Description</td> | ||
| 796 | * </tr> | ||
| 797 | * <tr valign="top"> | ||
| 798 | * <td><b>ENT_COMPAT</b></td> | ||
| 799 | * <td>Will convert double-quotes and leave single-quotes alone.</td> | ||
| 800 | * </tr> | ||
| 801 | * <tr valign="top"> | ||
| 802 | * <td><b>ENT_QUOTES</b></td> | ||
| 803 | * <td>Will convert both double and single quotes.</td> | ||
| 804 | * </tr> | ||
| 805 | * <tr valign="top"> | ||
| 806 | * <td><b>ENT_NOQUOTES</b></td> | ||
| 807 | * <td>Will leave both double and single quotes unconverted.</td> | ||
| 808 | * </tr> | ||
| 809 | * <tr valign="top"> | ||
| 810 | * <td><b>ENT_HTML401</b></td> | ||
| 811 | * <td> | ||
| 812 | * Handle code as HTML 4.01. | ||
| 813 | * </td> | ||
| 814 | * </tr> | ||
| 815 | * <tr valign="top"> | ||
| 816 | * <td><b>ENT_XML1</b></td> | ||
| 817 | * <td> | ||
| 818 | * Handle code as XML 1. | ||
| 819 | * </td> | ||
| 820 | * </tr> | ||
| 821 | * <tr valign="top"> | ||
| 822 | * <td><b>ENT_XHTML</b></td> | ||
| 823 | * <td> | ||
| 824 | * Handle code as XHTML. | ||
| 825 | * </td> | ||
| 826 | * </tr> | ||
| 827 | * <tr valign="top"> | ||
| 828 | * <td><b>ENT_HTML5</b></td> | ||
| 829 | * <td> | ||
| 830 | * Handle code as HTML 5. | ||
| 831 | * </td> | ||
| 832 | * </tr> | ||
| 833 | * </table> | ||
| 834 | * </p> | ||
| 835 | * | ||
| 836 | * @return static <p>Object with the resulting $str after being html encoded.</p> | ||
| 837 | */ | ||
| 838 | 5 | public function htmlEncode(int $flags = ENT_COMPAT): self | |
| 844 | |||
| 845 | /** | ||
| 846 | * Capitalizes the first word of the string, replaces underscores with | ||
| 847 | * spaces, and strips '_id'. | ||
| 848 | * | ||
| 849 | * @return static <p>Object with a humanized $str.</p> | ||
| 850 | */ | ||
| 851 | 3 | public function humanize(): self | |
| 857 | |||
| 858 | /** | ||
| 859 | * Converts the first character of the supplied string to upper case. | ||
| 860 | * | ||
| 861 | * @return static <p>Object with the first character of $str being upper case.</p> | ||
| 862 | */ | ||
| 863 | 61 | View Code Duplication | public function upperCaseFirst(): self | 
| 877 | |||
| 878 | /** | ||
| 879 | * Returns the index of the last occurrence of $needle in the string, | ||
| 880 | * and false if not found. Accepts an optional offset from which to begin | ||
| 881 | * the search. Offsets may be negative to count from the last character | ||
| 882 | * in the string. | ||
| 883 | * | ||
| 884 | * @param string $needle <p>Substring to look for.</p> | ||
| 885 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> | ||
| 886 | * | ||
| 887 | * @return int|false <p>The last occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> | ||
| 888 | */ | ||
| 889 | 12 | public function indexOfLast(string $needle, int $offset = 0) | |
| 893 | |||
| 894 | /** | ||
| 895 | * Returns the index of the last occurrence of $needle in the string, | ||
| 896 | * and false if not found. Accepts an optional offset from which to begin | ||
| 897 | * the search. Offsets may be negative to count from the last character | ||
| 898 | * in the string. | ||
| 899 | * | ||
| 900 | * @param string $needle <p>Substring to look for.</p> | ||
| 901 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> | ||
| 902 | * | ||
| 903 | * @return int|false <p>The last occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> | ||
| 904 | */ | ||
| 905 | 2 | public function indexOfLastIgnoreCase(string $needle, int $offset = 0) | |
| 909 | |||
| 910 | /** | ||
| 911 | * Inserts $substring into the string at the $index provided. | ||
| 912 | * | ||
| 913 | * @param string $substring <p>String to be inserted.</p> | ||
| 914 | * @param int $index <p>The index at which to insert the substring.</p> | ||
| 915 | * | ||
| 916 | * @return static <p>Object with the resulting $str after the insertion.</p> | ||
| 917 | */ | ||
| 918 | 8 | View Code Duplication | public function insert(string $substring, int $index): self | 
| 932 | |||
| 933 | /** | ||
| 934 | * Returns true if the string contains the $pattern, otherwise false. | ||
| 935 | * | ||
| 936 |    * WARNING: Asterisks ("*") are translated into (".*") zero-or-more regular | ||
| 937 | * expression wildcards. | ||
| 938 | * | ||
| 939 | * @credit Originally from Laravel, thanks Taylor. | ||
| 940 | * | ||
| 941 | * @param string $pattern <p>The string or pattern to match against.</p> | ||
| 942 | * | ||
| 943 | * @return bool <p>Whether or not we match the provided pattern.</p> | ||
| 944 | */ | ||
| 945 | 13 | public function is(string $pattern): bool | |
| 956 | |||
| 957 | /** | ||
| 958 | * Returns true if the string contains only alphabetic chars, false otherwise. | ||
| 959 | * | ||
| 960 | * @return bool <p>Whether or not $str contains only alphabetic chars.</p> | ||
| 961 | */ | ||
| 962 | 10 | public function isAlpha(): bool | |
| 966 | |||
| 967 | /** | ||
| 968 | * Determine whether the string is considered to be empty. | ||
| 969 | * | ||
| 970 | * A variable is considered empty if it does not exist or if its value equals FALSE. | ||
| 971 | * empty() does not generate a warning if the variable does not exist. | ||
| 972 | * | ||
| 973 | * @return bool <p>Whether or not $str is empty().</p> | ||
| 974 | */ | ||
| 975 | public function isEmpty(): bool | ||
| 979 | |||
| 980 | /** | ||
| 981 | * Returns true if the string contains only alphabetic and numeric chars, false otherwise. | ||
| 982 | * | ||
| 983 | * @return bool <p>Whether or not $str contains only alphanumeric chars.</p> | ||
| 984 | */ | ||
| 985 | 13 | public function isAlphanumeric(): bool | |
| 989 | |||
| 990 | /** | ||
| 991 | * Returns true if the string contains only whitespace chars, false otherwise. | ||
| 992 | * | ||
| 993 | * @return bool <p>Whether or not $str contains only whitespace characters.</p> | ||
| 994 | */ | ||
| 995 | 15 | public function isBlank(): bool | |
| 999 | |||
| 1000 | /** | ||
| 1001 | * Returns true if the string contains only hexadecimal chars, false otherwise. | ||
| 1002 | * | ||
| 1003 | * @return bool <p>Whether or not $str contains only hexadecimal chars.</p> | ||
| 1004 | */ | ||
| 1005 | 13 | public function isHexadecimal(): bool | |
| 1009 | |||
| 1010 | /** | ||
| 1011 | * Returns true if the string contains HTML-Tags, false otherwise. | ||
| 1012 | * | ||
| 1013 | * @return bool <p>Whether or not $str contains HTML-Tags.</p> | ||
| 1014 | */ | ||
| 1015 | 1 | public function isHtml(): bool | |
| 1019 | |||
| 1020 | /** | ||
| 1021 | * Returns true if the string contains a valid E-Mail address, false otherwise. | ||
| 1022 | * | ||
| 1023 | * @param bool $useExampleDomainCheck [optional] <p>Default: false</p> | ||
| 1024 | * @param bool $useTypoInDomainCheck [optional] <p>Default: false</p> | ||
| 1025 | * @param bool $useTemporaryDomainCheck [optional] <p>Default: false</p> | ||
| 1026 | * @param bool $useDnsCheck [optional] <p>Default: false</p> | ||
| 1027 | * | ||
| 1028 | * @return bool <p>Whether or not $str contains a valid E-Mail address.</p> | ||
| 1029 | */ | ||
| 1030 | 1 | public function isEmail(bool $useExampleDomainCheck = false, bool $useTypoInDomainCheck = false, bool $useTemporaryDomainCheck = false, bool $useDnsCheck = false): bool | |
| 1034 | |||
| 1035 | /** | ||
| 1036 | * Returns true if the string is JSON, false otherwise. Unlike json_decode | ||
| 1037 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, | ||
| 1038 | * in that an empty string is not considered valid JSON. | ||
| 1039 | * | ||
| 1040 | * @return bool <p>Whether or not $str is JSON.</p> | ||
| 1041 | */ | ||
| 1042 | 20 | public function isJson(): bool | |
| 1052 | |||
| 1053 | /** | ||
| 1054 | * Returns true if the string contains only lower case chars, false otherwise. | ||
| 1055 | * | ||
| 1056 | * @return bool <p>Whether or not $str contains only lower case characters.</p> | ||
| 1057 | */ | ||
| 1058 | 8 | public function isLowerCase(): bool | |
| 1066 | |||
| 1067 | /** | ||
| 1068 | * Returns true if the string is serialized, false otherwise. | ||
| 1069 | * | ||
| 1070 | * @return bool <p>Whether or not $str is serialized.</p> | ||
| 1071 | */ | ||
| 1072 | 7 | public function isSerialized(): bool | |
| 1084 | |||
| 1085 | /** | ||
| 1086 | * Returns true if the string contains only lower case chars, false | ||
| 1087 | * otherwise. | ||
| 1088 | * | ||
| 1089 | * @return bool <p>Whether or not $str contains only lower case characters.</p> | ||
| 1090 | */ | ||
| 1091 | 8 | public function isUpperCase(): bool | |
| 1095 | |||
| 1096 | /** | ||
| 1097 | * Returns the last $n characters of the string. | ||
| 1098 | * | ||
| 1099 | * @param int $n <p>Number of characters to retrieve from the end.</p> | ||
| 1100 | * | ||
| 1101 | * @return static <p>Object with its $str being the last $n chars.</p> | ||
| 1102 | */ | ||
| 1103 | 12 | View Code Duplication | public function last(int $n): self | 
| 1115 | |||
| 1116 | /** | ||
| 1117 | * Splits on newlines and carriage returns, returning an array of Stringy | ||
| 1118 | * objects corresponding to the lines in the string. | ||
| 1119 | * | ||
| 1120 | * @return static[] <p>An array of Stringy objects.</p> | ||
| 1121 | */ | ||
| 1122 | 15 | public function lines(): array | |
| 1133 | |||
| 1134 | /** | ||
| 1135 | * Returns the longest common prefix between the string and $otherStr. | ||
| 1136 | * | ||
| 1137 | * @param string $otherStr <p>Second string for comparison.</p> | ||
| 1138 | * | ||
| 1139 | * @return static <p>Object with its $str being the longest common prefix.</p> | ||
| 1140 | */ | ||
| 1141 | 10 | public function longestCommonPrefix(string $otherStr): self | |
| 1159 | |||
| 1160 | /** | ||
| 1161 | * Returns the longest common suffix between the string and $otherStr. | ||
| 1162 | * | ||
| 1163 | * @param string $otherStr <p>Second string for comparison.</p> | ||
| 1164 | * | ||
| 1165 | * @return static <p>Object with its $str being the longest common suffix.</p> | ||
| 1166 | */ | ||
| 1167 | 10 | public function longestCommonSuffix(string $otherStr): self | |
| 1185 | |||
| 1186 | /** | ||
| 1187 | * Returns the longest common substring between the string and $otherStr. | ||
| 1188 | * In the case of ties, it returns that which occurs first. | ||
| 1189 | * | ||
| 1190 | * @param string $otherStr <p>Second string for comparison.</p> | ||
| 1191 | * | ||
| 1192 | * @return static <p>Object with its $str being the longest common substring.</p> | ||
| 1193 | */ | ||
| 1194 | 10 | public function longestCommonSubstring(string $otherStr): self | |
| 1239 | |||
| 1240 | /** | ||
| 1241 | * Returns whether or not a character exists at an index. Offsets may be | ||
| 1242 | * negative to count from the last character in the string. Implements | ||
| 1243 | * part of the ArrayAccess interface. | ||
| 1244 | * | ||
| 1245 | * @param int $offset <p>The index to check.</p> | ||
| 1246 | * | ||
| 1247 | * @return boolean <p>Whether or not the index exists.</p> | ||
| 1248 | */ | ||
| 1249 | 6 | public function offsetExists($offset): bool | |
| 1261 | |||
| 1262 | /** | ||
| 1263 | * Returns the character at the given index. Offsets may be negative to | ||
| 1264 | * count from the last character in the string. Implements part of the | ||
| 1265 | * ArrayAccess interface, and throws an OutOfBoundsException if the index | ||
| 1266 | * does not exist. | ||
| 1267 | * | ||
| 1268 | * @param int $offset <p>The <strong>index</strong> from which to retrieve the char.</p> | ||
| 1269 | * | ||
| 1270 | * @return string <p>The character at the specified index.</p> | ||
| 1271 | * | ||
| 1272 | * @throws \OutOfBoundsException <p>If the positive or negative offset does not exist.</p> | ||
| 1273 | */ | ||
| 1274 | 2 | public function offsetGet($offset): string | |
| 1290 | |||
| 1291 | /** | ||
| 1292 | * Implements part of the ArrayAccess interface, but throws an exception | ||
| 1293 | * when called. This maintains the immutability of Stringy objects. | ||
| 1294 | * | ||
| 1295 | * @param int $offset <p>The index of the character.</p> | ||
| 1296 | * @param mixed $value <p>Value to set.</p> | ||
| 1297 | * | ||
| 1298 | * @throws \Exception <p>When called.</p> | ||
| 1299 | */ | ||
| 1300 | 1 | public function offsetSet($offset, $value) | |
| 1306 | |||
| 1307 | /** | ||
| 1308 | * Implements part of the ArrayAccess interface, but throws an exception | ||
| 1309 | * when called. This maintains the immutability of Stringy objects. | ||
| 1310 | * | ||
| 1311 | * @param int $offset <p>The index of the character.</p> | ||
| 1312 | * | ||
| 1313 | * @throws \Exception <p>When called.</p> | ||
| 1314 | */ | ||
| 1315 | 1 | public function offsetUnset($offset) | |
| 1321 | |||
| 1322 | /** | ||
| 1323 | * Pads the string to a given length with $padStr. If length is less than | ||
| 1324 | * or equal to the length of the string, no padding takes places. The | ||
| 1325 | * default string used for padding is a space, and the default type (one of | ||
| 1326 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException | ||
| 1327 | * if $padType isn't one of those 3 values. | ||
| 1328 | * | ||
| 1329 | * @param int $length <p>Desired string length after padding.</p> | ||
| 1330 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> | ||
| 1331 | * @param string $padType [optional] <p>One of 'left', 'right', 'both'. Default: 'right'</p> | ||
| 1332 | * | ||
| 1333 | * @return static <p>Object with a padded $str.</p> | ||
| 1334 | * | ||
| 1335 | * @throws \InvalidArgumentException <p>If $padType isn't one of 'right', 'left' or 'both'.</p> | ||
| 1336 | */ | ||
| 1337 | 13 | public function pad(int $length, string $padStr = ' ', string $padType = 'right'): self | |
| 1354 | |||
| 1355 | /** | ||
| 1356 | * Returns a new string of a given length such that the beginning of the | ||
| 1357 | * string is padded. Alias for pad() with a $padType of 'left'. | ||
| 1358 | * | ||
| 1359 | * @param int $length <p>Desired string length after padding.</p> | ||
| 1360 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> | ||
| 1361 | * | ||
| 1362 | * @return static <p>String with left padding.</p> | ||
| 1363 | */ | ||
| 1364 | 10 | public function padLeft(int $length, string $padStr = ' '): self | |
| 1368 | |||
| 1369 | /** | ||
| 1370 | * Adds the specified amount of left and right padding to the given string. | ||
| 1371 | * The default character used is a space. | ||
| 1372 | * | ||
| 1373 | * @param int $left [optional] <p>Length of left padding. Default: 0</p> | ||
| 1374 | * @param int $right [optional] <p>Length of right padding. Default: 0</p> | ||
| 1375 | * @param string $padStr [optional] <p>String used to pad. Default: ' '</p> | ||
| 1376 | * | ||
| 1377 | * @return static <p>String with padding applied.</p> | ||
| 1378 | */ | ||
| 1379 | 37 | protected function applyPadding(int $left = 0, int $right = 0, string $padStr = ' '): self | |
| 1416 | |||
| 1417 | /** | ||
| 1418 | * Returns a new string of a given length such that the end of the string | ||
| 1419 | * is padded. Alias for pad() with a $padType of 'right'. | ||
| 1420 | * | ||
| 1421 | * @param int $length <p>Desired string length after padding.</p> | ||
| 1422 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> | ||
| 1423 | * | ||
| 1424 | * @return static <p>String with right padding.</p> | ||
| 1425 | */ | ||
| 1426 | 13 | public function padRight(int $length, string $padStr = ' '): self | |
| 1430 | |||
| 1431 | /** | ||
| 1432 | * Returns a new string of a given length such that both sides of the | ||
| 1433 | * string are padded. Alias for pad() with a $padType of 'both'. | ||
| 1434 | * | ||
| 1435 | * @param int $length <p>Desired string length after padding.</p> | ||
| 1436 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> | ||
| 1437 | * | ||
| 1438 | * @return static <p>String with padding applied.</p> | ||
| 1439 | */ | ||
| 1440 | 14 | public function padBoth(int $length, string $padStr = ' '): self | |
| 1446 | |||
| 1447 | /** | ||
| 1448 | * Returns a new string starting with $string. | ||
| 1449 | * | ||
| 1450 | * @param string $string <p>The string to append.</p> | ||
| 1451 | * | ||
| 1452 | * @return static <p>Object with appended $string.</p> | ||
| 1453 | */ | ||
| 1454 | 2 | public function prepend(string $string): self | |
| 1458 | |||
| 1459 | /** | ||
| 1460 | * Returns a new string with the prefix $substring removed, if present. | ||
| 1461 | * | ||
| 1462 | * @param string $substring <p>The prefix to remove.</p> | ||
| 1463 | * | ||
| 1464 | * @return static <p>Object having a $str without the prefix $substring.</p> | ||
| 1465 | */ | ||
| 1466 | 12 | View Code Duplication | public function removeLeft(string $substring): self | 
| 1478 | |||
| 1479 | /** | ||
| 1480 | * Returns a new string with the suffix $substring removed, if present. | ||
| 1481 | * | ||
| 1482 | * @param string $substring <p>The suffix to remove.</p> | ||
| 1483 | * | ||
| 1484 | * @return static <p>Object having a $str without the suffix $substring.</p> | ||
| 1485 | */ | ||
| 1486 | 12 | View Code Duplication | public function removeRight(string $substring): self | 
| 1498 | |||
| 1499 | /** | ||
| 1500 | * Returns a repeated string given a multiplier. | ||
| 1501 | * | ||
| 1502 | * @param int $multiplier <p>The number of times to repeat the string.</p> | ||
| 1503 | * | ||
| 1504 | * @return static <p>Object with a repeated str.</p> | ||
| 1505 | */ | ||
| 1506 | 7 | public function repeat(int $multiplier): self | |
| 1512 | |||
| 1513 | /** | ||
| 1514 | * Replaces all occurrences of $search in $str by $replacement. | ||
| 1515 | * | ||
| 1516 | * @param string $search <p>The needle to search for.</p> | ||
| 1517 | * @param string $replacement <p>The string to replace with.</p> | ||
| 1518 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 1519 | * | ||
| 1520 | * @return static <p>Object with the resulting $str after the replacements.</p> | ||
| 1521 | */ | ||
| 1522 | 29 | View Code Duplication | public function replace(string $search, string $replacement, bool $caseSensitive = true): self | 
| 1532 | |||
| 1533 | /** | ||
| 1534 | * Replaces all occurrences of $search in $str by $replacement. | ||
| 1535 | * | ||
| 1536 | * @param array $search <p>The elements to search for.</p> | ||
| 1537 | * @param string|array $replacement <p>The string to replace with.</p> | ||
| 1538 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> | ||
| 1539 | * | ||
| 1540 | * @return static <p>Object with the resulting $str after the replacements.</p> | ||
| 1541 | */ | ||
| 1542 | 30 | View Code Duplication | public function replaceAll(array $search, $replacement, bool $caseSensitive = true): self | 
| 1552 | |||
| 1553 | /** | ||
| 1554 | * Replaces all occurrences of $search from the beginning of string with $replacement. | ||
| 1555 | * | ||
| 1556 | * @param string $search <p>The string to search for.</p> | ||
| 1557 | * @param string $replacement <p>The replacement.</p> | ||
| 1558 | * | ||
| 1559 | * @return static <p>Object with the resulting $str after the replacements.</p> | ||
| 1560 | */ | ||
| 1561 | 16 | public function replaceBeginning(string $search, string $replacement): self | |
| 1567 | |||
| 1568 | /** | ||
| 1569 | * Replaces all occurrences of $search from the ending of string with $replacement. | ||
| 1570 | * | ||
| 1571 | * @param string $search <p>The string to search for.</p> | ||
| 1572 | * @param string $replacement <p>The replacement.</p> | ||
| 1573 | * | ||
| 1574 | * @return static <p>Object with the resulting $str after the replacements.</p> | ||
| 1575 | */ | ||
| 1576 | 16 | public function replaceEnding(string $search, string $replacement): self | |
| 1582 | |||
| 1583 | /** | ||
| 1584 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". | ||
| 1585 | * If no match is found returns new empty Stringy object. | ||
| 1586 | * | ||
| 1587 | * @param string $needle <p>The string to look for.</p> | ||
| 1588 | * @param bool $beforeNeedle [optional] <p>Default: false</p> | ||
| 1589 | * | ||
| 1590 | * @return static | ||
| 1591 | */ | ||
| 1592 | 2 | View Code Duplication | public function substringOf(string $needle, bool $beforeNeedle = false): self | 
| 1604 | |||
| 1605 | /** | ||
| 1606 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". | ||
| 1607 | * If no match is found returns new empty Stringy object. | ||
| 1608 | * | ||
| 1609 | * @param string $needle <p>The string to look for.</p> | ||
| 1610 | * @param bool $beforeNeedle [optional] <p>Default: false</p> | ||
| 1611 | * | ||
| 1612 | * @return static | ||
| 1613 | */ | ||
| 1614 | 2 | View Code Duplication | public function substringOfIgnoreCase(string $needle, bool $beforeNeedle = false): self | 
| 1626 | |||
| 1627 | /** | ||
| 1628 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". | ||
| 1629 | * If no match is found returns new empty Stringy object. | ||
| 1630 | * | ||
| 1631 | * @param string $needle <p>The string to look for.</p> | ||
| 1632 | * @param bool $beforeNeedle [optional] <p>Default: false</p> | ||
| 1633 | * | ||
| 1634 | * @return static | ||
| 1635 | */ | ||
| 1636 | 2 | View Code Duplication | public function lastSubstringOf(string $needle, bool $beforeNeedle = false): self | 
| 1648 | |||
| 1649 | /** | ||
| 1650 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". | ||
| 1651 | * If no match is found returns new empty Stringy object. | ||
| 1652 | * | ||
| 1653 | * @param string $needle <p>The string to look for.</p> | ||
| 1654 | * @param bool $beforeNeedle [optional] <p>Default: false</p> | ||
| 1655 | * | ||
| 1656 | * @return static | ||
| 1657 | */ | ||
| 1658 | 1 | View Code Duplication | public function lastSubstringOfIgnoreCase(string $needle, bool $beforeNeedle = false): self | 
| 1670 | |||
| 1671 | /** | ||
| 1672 | * Returns a reversed string. A multibyte version of strrev(). | ||
| 1673 | * | ||
| 1674 | * @return static <p>Object with a reversed $str.</p> | ||
| 1675 | */ | ||
| 1676 | 5 | public function reverse(): self | |
| 1682 | |||
| 1683 | /** | ||
| 1684 | * Truncates the string to a given length, while ensuring that it does not | ||
| 1685 | * split words. If $substring is provided, and truncating occurs, the | ||
| 1686 | * string is further truncated so that the substring may be appended without | ||
| 1687 | * exceeding the desired length. | ||
| 1688 | * | ||
| 1689 | * @param int $length <p>Desired length of the truncated string.</p> | ||
| 1690 | * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p> | ||
| 1691 | * | ||
| 1692 | * @return static <p>Object with the resulting $str after truncating.</p> | ||
| 1693 | */ | ||
| 1694 | 23 | public function safeTruncate(int $length, string $substring = ''): self | |
| 1723 | |||
| 1724 | /** | ||
| 1725 | * A multibyte string shuffle function. It returns a string with its | ||
| 1726 | * characters in random order. | ||
| 1727 | * | ||
| 1728 | * @return static <p>Object with a shuffled $str.</p> | ||
| 1729 | */ | ||
| 1730 | 3 | public function shuffle(): self | |
| 1736 | |||
| 1737 | /** | ||
| 1738 | * Converts the string into an URL slug. This includes replacing non-ASCII | ||
| 1739 | * characters with their closest ASCII equivalents, removing remaining | ||
| 1740 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with | ||
| 1741 | * $replacement. The replacement defaults to a single dash, and the string | ||
| 1742 | * is also converted to lowercase. | ||
| 1743 | * | ||
| 1744 | * @param string $replacement [optional] <p>The string used to replace whitespace. Default: '-'</p> | ||
| 1745 | * @param string $language [optional] <p>The language for the url. Default: 'de'</p> | ||
| 1746 | * @param bool $strToLower [optional] <p>string to lower. Default: true</p> | ||
| 1747 | * | ||
| 1748 | * @return static <p>Object whose $str has been converted to an URL slug.</p> | ||
| 1749 | */ | ||
| 1750 | 15 | public function slugify(string $replacement = '-', string $language = 'de', bool $strToLower = true): self | |
| 1756 | |||
| 1757 | /** | ||
| 1758 | * Remove css media-queries. | ||
| 1759 | * | ||
| 1760 | * @return static | ||
| 1761 | */ | ||
| 1762 | 1 | public function stripeCssMediaQueries(): self | |
| 1768 | |||
| 1769 | /** | ||
| 1770 | * Strip all whitespace characters. This includes tabs and newline characters, | ||
| 1771 | * as well as multibyte whitespace such as the thin space and ideographic space. | ||
| 1772 | * | ||
| 1773 | * @return static | ||
| 1774 | */ | ||
| 1775 | 12 | public function stripWhitespace(): self | |
| 1779 | |||
| 1780 | /** | ||
| 1781 | * Remove empty html-tag. | ||
| 1782 | * | ||
| 1783 | * e.g.: <tag></tag> | ||
| 1784 | * | ||
| 1785 | * @return static | ||
| 1786 | */ | ||
| 1787 | 1 | public function stripeEmptyHtmlTags(): self | |
| 1793 | |||
| 1794 | /** | ||
| 1795 | * Converts the string into an valid UTF-8 string. | ||
| 1796 | * | ||
| 1797 | * @return static | ||
| 1798 | */ | ||
| 1799 | 1 | public function utf8ify(): self | |
| 1803 | |||
| 1804 | /** | ||
| 1805 | * Create a escape html version of the string via "UTF8::htmlspecialchars()". | ||
| 1806 | * | ||
| 1807 | * @return static | ||
| 1808 | */ | ||
| 1809 | 6 | public function escape(): self | |
| 1819 | |||
| 1820 | /** | ||
| 1821 | * Create an extract from a sentence, so if the search-string was found, it try to centered in the output. | ||
| 1822 | * | ||
| 1823 | * @param string $search | ||
| 1824 | * @param int|null $length [optional] <p>Default: null === text->length / 2</p> | ||
| 1825 | * @param string $replacerForSkippedText [optional] <p>Default: …</p> | ||
| 1826 | * | ||
| 1827 | * @return static | ||
| 1828 | */ | ||
| 1829 | 1 | public function extractText(string $search = '', int $length = null, string $replacerForSkippedText = '…'): self | |
| 1954 | |||
| 1955 | |||
| 1956 | /** | ||
| 1957 | * Try to remove all XSS-attacks from the string. | ||
| 1958 | * | ||
| 1959 | * @return static | ||
| 1960 | */ | ||
| 1961 | 6 | public function removeXss(): self | |
| 1973 | |||
| 1974 | /** | ||
| 1975 | * Remove all breaks [<br> | \r\n | \r | \n | ...] from the string. | ||
| 1976 | * | ||
| 1977 | * @param string $replacement [optional] <p>Default is a empty string.</p> | ||
| 1978 | * | ||
| 1979 | * @return static | ||
| 1980 | */ | ||
| 1981 | 6 | public function removeHtmlBreak(string $replacement = ''): self | |
| 1987 | |||
| 1988 | /** | ||
| 1989 | * Remove html via "strip_tags()" from the string. | ||
| 1990 | * | ||
| 1991 | * @param string $allowableTags [optional] <p>You can use the optional second parameter to specify tags which should | ||
| 1992 | * not be stripped. Default: null | ||
| 1993 | * </p> | ||
| 1994 | * | ||
| 1995 | * @return static | ||
| 1996 | */ | ||
| 1997 | 6 | public function removeHtml(string $allowableTags = null): self | |
| 2003 | |||
| 2004 | /** | ||
| 2005 | * Returns the substring beginning at $start, and up to, but not including | ||
| 2006 | * the index specified by $end. If $end is omitted, the function extracts | ||
| 2007 | * the remaining string. If $end is negative, it is computed from the end | ||
| 2008 | * of the string. | ||
| 2009 | * | ||
| 2010 | * @param int $start <p>Initial index from which to begin extraction.</p> | ||
| 2011 | * @param int $end [optional] <p>Index at which to end extraction. Default: null</p> | ||
| 2012 | * | ||
| 2013 | * @return static <p>Object with its $str being the extracted substring.</p> | ||
| 2014 | */ | ||
| 2015 | 18 | public function slice(int $start, int $end = null): self | |
| 2031 | |||
| 2032 | /** | ||
| 2033 | * Splits the string with the provided regular expression, returning an | ||
| 2034 | * array of Stringy objects. An optional integer $limit will truncate the | ||
| 2035 | * results. | ||
| 2036 | * | ||
| 2037 | * @param string $pattern <p>The regex with which to split the string.</p> | ||
| 2038 | * @param int $limit [optional] <p>Maximum number of results to return. Default: -1 === no limit</p> | ||
| 2039 | * | ||
| 2040 | * @return static[] <p>An array of Stringy objects.</p> | ||
| 2041 | */ | ||
| 2042 | 16 | public function split(string $pattern, int $limit = -1): array | |
| 2076 | |||
| 2077 | /** | ||
| 2078 | * Surrounds $str with the given substring. | ||
| 2079 | * | ||
| 2080 | * @param string $substring <p>The substring to add to both sides.</P> | ||
| 2081 | * | ||
| 2082 | * @return static <p>Object whose $str had the substring both prepended and appended.</p> | ||
| 2083 | */ | ||
| 2084 | 5 | public function surround(string $substring): self | |
| 2090 | |||
| 2091 | /** | ||
| 2092 | * Returns a case swapped version of the string. | ||
| 2093 | * | ||
| 2094 | * @return static <p>Object whose $str has each character's case swapped.</P> | ||
| 2095 | */ | ||
| 2096 | 5 | public function swapCase(): self | |
| 2104 | |||
| 2105 | /** | ||
| 2106 | * Returns a string with smart quotes, ellipsis characters, and dashes from | ||
| 2107 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII | ||
| 2108 | * equivalents. | ||
| 2109 | * | ||
| 2110 | * @return static <p>Object whose $str has those characters removed.</p> | ||
| 2111 | */ | ||
| 2112 | 4 | public function tidy(): self | |
| 2118 | |||
| 2119 | /** | ||
| 2120 | * Returns a trimmed string in proper title case. | ||
| 2121 | * | ||
| 2122 | * Also accepts an array, $ignore, allowing you to list words not to be | ||
| 2123 | * capitalized. | ||
| 2124 | * | ||
| 2125 | * Adapted from John Gruber's script. | ||
| 2126 | * | ||
| 2127 | * @see https://gist.github.com/gruber/9f9e8650d68b13ce4d78 | ||
| 2128 | * | ||
| 2129 | * @param array $ignore <p>An array of words not to capitalize.</p> | ||
| 2130 | * | ||
| 2131 | * @return static <p>Object with a titleized $str</p> | ||
| 2132 | */ | ||
| 2133 | 35 | public function titleizeForHumans(array $ignore = []): Stringy | |
| 2260 | |||
| 2261 | /** | ||
| 2262 | * Returns a trimmed string with the first letter of each word capitalized. | ||
| 2263 | * Also accepts an array, $ignore, allowing you to list words not to be | ||
| 2264 | * capitalized. | ||
| 2265 | * | ||
| 2266 | * @param array|null $ignore [optional] <p>An array of words not to capitalize or null. Default: null</p> | ||
| 2267 | * | ||
| 2268 | * @return static <p>Object with a titleized $str.</p> | ||
| 2269 | */ | ||
| 2270 | 5 | public function titleize(array $ignore = null): self | |
| 2291 | |||
| 2292 | /** | ||
| 2293 | * Converts all characters in the string to lowercase. | ||
| 2294 | * | ||
| 2295 | * @return static <p>Object with all characters of $str being lowercase.</p> | ||
| 2296 | */ | ||
| 2297 | 54 | public function toLowerCase(): self | |
| 2303 | |||
| 2304 | /** | ||
| 2305 | * Returns true if the string is base64 encoded, false otherwise. | ||
| 2306 | * | ||
| 2307 | * @return bool <p>Whether or not $str is base64 encoded.</p> | ||
| 2308 | */ | ||
| 2309 | 7 | public function isBase64(): bool | |
| 2313 | |||
| 2314 | /** | ||
| 2315 | * Returns an ASCII version of the string. A set of non-ASCII characters are | ||
| 2316 | * replaced with their closest ASCII counterparts, and the rest are removed | ||
| 2317 | * unless instructed otherwise. | ||
| 2318 | * | ||
| 2319 | * @param bool $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance | | ||
| 2320 | * Default: false</p> | ||
| 2321 | * | ||
| 2322 | * @return static <p>Object whose $str contains only ASCII characters.</p> | ||
| 2323 | */ | ||
| 2324 | 16 | public function toAscii(bool $strict = false): self | |
| 2330 | |||
| 2331 | /** | ||
| 2332 | * Returns a boolean representation of the given logical string value. | ||
| 2333 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', | ||
| 2334 | * 'off', and 'no' will return false. In all instances, case is ignored. | ||
| 2335 | * For other numeric strings, their sign will determine the return value. | ||
| 2336 | * In addition, blank strings consisting of only whitespace will return | ||
| 2337 | * false. For all other strings, the return value is a result of a | ||
| 2338 | * boolean cast. | ||
| 2339 | * | ||
| 2340 | * @return bool <p>A boolean value for the string.</p> | ||
| 2341 | */ | ||
| 2342 | 15 | public function toBoolean(): bool | |
| 2366 | |||
| 2367 | /** | ||
| 2368 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. | ||
| 2369 | * | ||
| 2370 | * @return string | ||
| 2371 | */ | ||
| 2372 | 1076 | public function toString(): string | |
| 2376 | |||
| 2377 | /** | ||
| 2378 | * Converts each tab in the string to some number of spaces, as defined by | ||
| 2379 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. | ||
| 2380 | * | ||
| 2381 | * @param int $tabLength [optional] <p>Number of spaces to replace each tab with. Default: 4</p> | ||
| 2382 | * | ||
| 2383 | * @return static <p>Object whose $str has had tabs switched to spaces.</p> | ||
| 2384 | */ | ||
| 2385 | 6 | View Code Duplication | public function toSpaces(int $tabLength = 4): self | 
| 2392 | |||
| 2393 | /** | ||
| 2394 | * Converts each occurrence of some consecutive number of spaces, as | ||
| 2395 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces | ||
| 2396 | * are converted to a tab. | ||
| 2397 | * | ||
| 2398 | * @param int $tabLength [optional] <p>Number of spaces to replace with a tab. Default: 4</p> | ||
| 2399 | * | ||
| 2400 | * @return static <p>Object whose $str has had spaces switched to tabs.</p> | ||
| 2401 | */ | ||
| 2402 | 5 | View Code Duplication | public function toTabs(int $tabLength = 4): self | 
| 2409 | |||
| 2410 | /** | ||
| 2411 | * Converts the first character of each word in the string to uppercase. | ||
| 2412 | * | ||
| 2413 | * @return static Object with all characters of $str being title-cased | ||
| 2414 | */ | ||
| 2415 | 5 | public function toTitleCase(): self | |
| 2422 | |||
| 2423 | /** | ||
| 2424 | * Converts all characters in the string to uppercase. | ||
| 2425 | * | ||
| 2426 | * @return static Object with all characters of $str being uppercase | ||
| 2427 | */ | ||
| 2428 | 5 | public function toUpperCase(): self | |
| 2434 | |||
| 2435 | /** | ||
| 2436 | * Returns a string with whitespace removed from the start of the string. | ||
| 2437 | * Supports the removal of unicode whitespace. Accepts an optional | ||
| 2438 | * string of characters to strip instead of the defaults. | ||
| 2439 | * | ||
| 2440 | * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p> | ||
| 2441 | * | ||
| 2442 | * @return static <p>Object with a trimmed $str.</p> | ||
| 2443 | */ | ||
| 2444 | 13 | View Code Duplication | public function trimLeft(string $chars = null): self | 
| 2454 | |||
| 2455 | /** | ||
| 2456 | * Returns a string with whitespace removed from the end of the string. | ||
| 2457 | * Supports the removal of unicode whitespace. Accepts an optional | ||
| 2458 | * string of characters to strip instead of the defaults. | ||
| 2459 | * | ||
| 2460 | * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p> | ||
| 2461 | * | ||
| 2462 | * @return static <p>Object with a trimmed $str.</p> | ||
| 2463 | */ | ||
| 2464 | 13 | View Code Duplication | public function trimRight(string $chars = null): self | 
| 2474 | |||
| 2475 | /** | ||
| 2476 | * Truncates the string to a given length. If $substring is provided, and | ||
| 2477 | * truncating occurs, the string is further truncated so that the substring | ||
| 2478 | * may be appended without exceeding the desired length. | ||
| 2479 | * | ||
| 2480 | * @param int $length <p>Desired length of the truncated string.</p> | ||
| 2481 | * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p> | ||
| 2482 | * | ||
| 2483 | * @return static <p>Object with the resulting $str after truncating.</p> | ||
| 2484 | */ | ||
| 2485 | 22 | View Code Duplication | public function truncate(int $length, string $substring = ''): self | 
| 2501 | |||
| 2502 | /** | ||
| 2503 | * Returns a lowercase and trimmed string separated by underscores. | ||
| 2504 | * Underscores are inserted before uppercase characters (with the exception | ||
| 2505 | * of the first character of the string), and in place of spaces as well as | ||
| 2506 | * dashes. | ||
| 2507 | * | ||
| 2508 | * @return static <p>Object with an underscored $str.</p> | ||
| 2509 | */ | ||
| 2510 | 16 | public function underscored(): self | |
| 2514 | |||
| 2515 | /** | ||
| 2516 | * Returns an UpperCamelCase version of the supplied string. It trims | ||
| 2517 | * surrounding spaces, capitalizes letters following digits, spaces, dashes | ||
| 2518 | * and underscores, and removes spaces, dashes, underscores. | ||
| 2519 | * | ||
| 2520 | * @return static <p>Object with $str in UpperCamelCase.</p> | ||
| 2521 | */ | ||
| 2522 | 13 | public function upperCamelize(): self | |
| 2526 | |||
| 2527 | /** | ||
| 2528 | * Returns a camelCase version of the string. Trims surrounding spaces, | ||
| 2529 | * capitalizes letters following digits, spaces, dashes and underscores, | ||
| 2530 | * and removes spaces, dashes, as well as underscores. | ||
| 2531 | * | ||
| 2532 | * @return static <p>Object with $str in camelCase.</p> | ||
| 2533 | */ | ||
| 2534 | 32 | public function camelize(): self | |
| 2562 | |||
| 2563 | /** | ||
| 2564 | * Convert a string to e.g.: "snake_case" | ||
| 2565 | * | ||
| 2566 | * @return static <p>Object with $str in snake_case.</p> | ||
| 2567 | */ | ||
| 2568 | 20 | public function snakeize(): self | |
| 2611 | |||
| 2612 | /** | ||
| 2613 | * Converts the first character of the string to lower case. | ||
| 2614 | * | ||
| 2615 | * @return static <p>Object with the first character of $str being lower case.</p> | ||
| 2616 | */ | ||
| 2617 | 37 | View Code Duplication | public function lowerCaseFirst(): self | 
| 2626 | |||
| 2627 | /** | ||
| 2628 | * Shorten the string after $length, but also after the next word. | ||
| 2629 | * | ||
| 2630 | * @param int $length | ||
| 2631 | * @param string $strAddOn [optional] <p>Default: '…'</p> | ||
| 2632 | * | ||
| 2633 | * @return static | ||
| 2634 | */ | ||
| 2635 | 4 | public function shortenAfterWord(int $length, string $strAddOn = '…'): self | |
| 2641 | |||
| 2642 | /** | ||
| 2643 | * Line-Wrap the string after $limit, but also after the next word. | ||
| 2644 | * | ||
| 2645 | * @param int $limit | ||
| 2646 | * | ||
| 2647 | * @return static | ||
| 2648 | */ | ||
| 2649 | 1 | public function lineWrapAfterWord(int $limit): self | |
| 2661 | |||
| 2662 | /** | ||
| 2663 | * Gets the substring after the first occurrence of a separator. | ||
| 2664 | * If no match is found returns new empty Stringy object. | ||
| 2665 | * | ||
| 2666 | * @param string $separator | ||
| 2667 | * | ||
| 2668 | * @return static | ||
| 2669 | */ | ||
| 2670 | 2 | View Code Duplication | public function afterFirst(string $separator): self | 
| 2694 | |||
| 2695 | /** | ||
| 2696 | * Gets the substring after the first occurrence of a separator. | ||
| 2697 | * If no match is found returns new empty Stringy object. | ||
| 2698 | * | ||
| 2699 | * @param string $separator | ||
| 2700 | * | ||
| 2701 | * @return static | ||
| 2702 | */ | ||
| 2703 | 1 | View Code Duplication | public function afterFirstIgnoreCase(string $separator): self | 
| 2727 | |||
| 2728 | /** | ||
| 2729 | * Gets the substring after the last occurrence of a separator. | ||
| 2730 | * If no match is found returns new empty Stringy object. | ||
| 2731 | * | ||
| 2732 | * @param string $separator | ||
| 2733 | * | ||
| 2734 | * @return static | ||
| 2735 | */ | ||
| 2736 | 1 | View Code Duplication | public function afterLastIgnoreCase(string $separator): self | 
| 2761 | |||
| 2762 | /** | ||
| 2763 | * Gets the substring after the last occurrence of a separator. | ||
| 2764 | * If no match is found returns new empty Stringy object. | ||
| 2765 | * | ||
| 2766 | * @param string $separator | ||
| 2767 | * | ||
| 2768 | * @return static | ||
| 2769 | */ | ||
| 2770 | 1 | View Code Duplication | public function afterLast(string $separator): self | 
| 2795 | |||
| 2796 | /** | ||
| 2797 | * Gets the substring before the first occurrence of a separator. | ||
| 2798 | * If no match is found returns new empty Stringy object. | ||
| 2799 | * | ||
| 2800 | * @param string $separator | ||
| 2801 | * | ||
| 2802 | * @return static | ||
| 2803 | */ | ||
| 2804 | 1 | View Code Duplication | public function beforeFirst(string $separator): self | 
| 2829 | |||
| 2830 | /** | ||
| 2831 | * Gets the substring before the first occurrence of a separator. | ||
| 2832 | * If no match is found returns new empty Stringy object. | ||
| 2833 | * | ||
| 2834 | * @param string $separator | ||
| 2835 | * | ||
| 2836 | * @return static | ||
| 2837 | */ | ||
| 2838 | 1 | View Code Duplication | public function beforeFirstIgnoreCase(string $separator): self | 
| 2863 | |||
| 2864 | /** | ||
| 2865 | * Gets the substring before the last occurrence of a separator. | ||
| 2866 | * If no match is found returns new empty Stringy object. | ||
| 2867 | * | ||
| 2868 | * @param string $separator | ||
| 2869 | * | ||
| 2870 | * @return static | ||
| 2871 | */ | ||
| 2872 | 1 | View Code Duplication | public function beforeLast(string $separator): self | 
| 2897 | |||
| 2898 | /** | ||
| 2899 | * Gets the substring before the last occurrence of a separator. | ||
| 2900 | * If no match is found returns new empty Stringy object. | ||
| 2901 | * | ||
| 2902 | * @param string $separator | ||
| 2903 | * | ||
| 2904 | * @return static | ||
| 2905 | */ | ||
| 2906 | 1 | View Code Duplication | public function beforeLastIgnoreCase(string $separator): self | 
| 2931 | |||
| 2932 | /** | ||
| 2933 | * Returns the string with the first letter of each word capitalized, | ||
| 2934 | * except for when the word is a name which shouldn't be capitalized. | ||
| 2935 | * | ||
| 2936 | * @return static <p>Object with $str capitalized.</p> | ||
| 2937 | */ | ||
| 2938 | 39 | public function capitalizePersonalName(): self | |
| 2946 | |||
| 2947 | /** | ||
| 2948 | * @param string $word | ||
| 2949 | * | ||
| 2950 | * @return static <p>Object with $str capitalized.</p> | ||
| 2951 | */ | ||
| 2952 | 7 | protected function capitalizeWord(string $word): self | |
| 2962 | |||
| 2963 | /** | ||
| 2964 |    * Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius"). | ||
| 2965 | * | ||
| 2966 | * @param string $names | ||
| 2967 | * @param string $delimiter | ||
| 2968 | * | ||
| 2969 | * @return static | ||
| 2970 | */ | ||
| 2971 | 39 | protected function capitalizePersonalNameByDelimiter(string $names, string $delimiter): self | |
| 3047 | } | ||
| 3048 | 
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: