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 | 1060 | public function __construct($str = '', $encoding = null) |
|
80 | |||
81 | /** |
||
82 | * Returns the value in $str. |
||
83 | * |
||
84 | * @return string The current value of the $str property |
||
85 | */ |
||
86 | 150 | public function __toString() |
|
90 | |||
91 | /** |
||
92 | * Returns a new string with $string appended. |
||
93 | * |
||
94 | * @param string $string The string to append |
||
95 | * |
||
96 | * @return Stringy Object with appended $string |
||
97 | */ |
||
98 | 5 | public function append($string) |
|
102 | |||
103 | /** |
||
104 | * Append an password (limited to chars that are good readable). |
||
105 | * |
||
106 | * @param int $length length of the random string |
||
107 | * |
||
108 | * @return Stringy Object with appended password |
||
109 | */ |
||
110 | 1 | public function appendPassword($length) |
|
116 | |||
117 | /** |
||
118 | * Append an unique identifier. |
||
119 | * |
||
120 | * @param string|int $extraPrefix |
||
121 | * |
||
122 | * @return Stringy Object with appended unique identifier as md5-hash |
||
123 | */ |
||
124 | 1 | public function appendUniqueIdentifier($extraPrefix = '') |
|
134 | |||
135 | /** |
||
136 | * Append an random string. |
||
137 | * |
||
138 | * @param int $length length of the random string |
||
139 | * @param string $possibleChars characters string for the random selection |
||
140 | * |
||
141 | * @return Stringy Object with appended random string |
||
142 | */ |
||
143 | 2 | public function appendRandomString($length, $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') |
|
164 | |||
165 | /** |
||
166 | * Creates a Stringy object and assigns both str and encoding properties |
||
167 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
168 | * $encoding is not specified, it defaults to mb_internal_encoding(). It |
||
169 | * then returns the initialized object. Throws an InvalidArgumentException |
||
170 | * if the first argument is an array or object without a __toString method. |
||
171 | * |
||
172 | * @param mixed $str Value to modify, after being cast to string |
||
173 | * @param string $encoding The character encoding |
||
174 | * |
||
175 | * @return Stringy A Stringy object |
||
176 | * @throws \InvalidArgumentException if an array or object without a |
||
177 | * __toString method is passed as the first argument |
||
178 | */ |
||
179 | 1050 | public static function create($str = '', $encoding = null) |
|
183 | |||
184 | /** |
||
185 | * Returns the substring between $start and $end, if found, or an empty |
||
186 | * string. An optional offset may be supplied from which to begin the |
||
187 | * search for the start string. |
||
188 | * |
||
189 | * @param string $start Delimiter marking the start of the substring |
||
190 | * @param string $end Delimiter marking the end of the substring |
||
191 | * @param int $offset Index from which to begin the search |
||
192 | * |
||
193 | * @return Stringy Object whose $str is a substring between $start and $end |
||
194 | */ |
||
195 | 16 | public function between($start, $end, $offset = 0) |
|
210 | |||
211 | /** |
||
212 | * Returns the index of the first occurrence of $needle in the string, |
||
213 | * and false if not found. Accepts an optional offset from which to begin |
||
214 | * the search. |
||
215 | * |
||
216 | * @param string $needle Substring to look for |
||
217 | * @param int $offset Offset from which to search |
||
218 | * |
||
219 | * @return int|bool The occurrence's index if found, otherwise false |
||
220 | */ |
||
221 | 28 | public function indexOf($needle, $offset = 0) |
|
225 | |||
226 | /** |
||
227 | * Returns the substring beginning at $start with the specified $length. |
||
228 | * It differs from the UTF8::substr() function in that providing a $length of |
||
229 | * null will return the rest of the string, rather than an empty string. |
||
230 | * |
||
231 | * @param int $start Position of the first character to use |
||
232 | * @param int $length Maximum number of characters used |
||
233 | * |
||
234 | * @return Stringy Object with its $str being the substring |
||
235 | */ |
||
236 | 64 | public function substr($start, $length = null) |
|
246 | |||
247 | /** |
||
248 | * Returns the length of the string. |
||
249 | * |
||
250 | * @return int The number of characters in $str given the encoding |
||
251 | */ |
||
252 | 247 | public function length() |
|
256 | |||
257 | /** |
||
258 | * Trims the string and replaces consecutive whitespace characters with a |
||
259 | * single space. This includes tabs and newline characters, as well as |
||
260 | * multibyte whitespace such as the thin space and ideographic space. |
||
261 | * |
||
262 | * @return Stringy Object with a trimmed $str and condensed whitespace |
||
263 | */ |
||
264 | 52 | public function collapseWhitespace() |
|
268 | |||
269 | /** |
||
270 | * Returns a string with whitespace removed from the start and end of the |
||
271 | * string. Supports the removal of unicode whitespace. Accepts an optional |
||
272 | * string of characters to strip instead of the defaults. |
||
273 | * |
||
274 | * @param string $chars Optional string of characters to strip |
||
275 | * |
||
276 | * @return Stringy Object with a trimmed $str |
||
277 | */ |
||
278 | 153 | View Code Duplication | public function trim($chars = null) |
288 | |||
289 | /** |
||
290 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
291 | * |
||
292 | * @param string $pattern The regular expression pattern |
||
293 | * @param string $replacement The string to replace with |
||
294 | * @param string $options Matching conditions to be used |
||
295 | * |
||
296 | * @return Stringy Object with the result2ing $str after the replacements |
||
297 | */ |
||
298 | 223 | public function regexReplace($pattern, $replacement, $options = '') |
|
312 | |||
313 | /** |
||
314 | * Returns true if the string contains all $needles, false otherwise. By |
||
315 | * default the comparison is case-sensitive, but can be made insensitive by |
||
316 | * setting $caseSensitive to false. |
||
317 | * |
||
318 | * @param array $needles SubStrings to look for |
||
319 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
320 | * |
||
321 | * @return bool Whether or not $str contains $needle |
||
322 | */ |
||
323 | 43 | View Code Duplication | public function containsAll($needles, $caseSensitive = true) |
338 | |||
339 | /** |
||
340 | * Returns true if the string contains $needle, false otherwise. By default |
||
341 | * the comparison is case-sensitive, but can be made insensitive by setting |
||
342 | * $caseSensitive to false. |
||
343 | * |
||
344 | * @param string $needle Substring to look for |
||
345 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
346 | * |
||
347 | * @return bool Whether or not $str contains $needle |
||
348 | */ |
||
349 | 105 | public function contains($needle, $caseSensitive = true) |
|
359 | |||
360 | /** |
||
361 | * Returns true if the string contains any $needles, false otherwise. By |
||
362 | * default the comparison is case-sensitive, but can be made insensitive by |
||
363 | * setting $caseSensitive to false. |
||
364 | * |
||
365 | * @param array $needles SubStrings to look for |
||
366 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
367 | * |
||
368 | * @return bool Whether or not $str contains $needle |
||
369 | */ |
||
370 | 43 | View Code Duplication | public function containsAny($needles, $caseSensitive = true) |
385 | |||
386 | /** |
||
387 | * Returns the length of the string, implementing the countable interface. |
||
388 | * |
||
389 | * @return int The number of characters in the string, given the encoding |
||
390 | */ |
||
391 | 1 | public function count() |
|
395 | |||
396 | /** |
||
397 | * Returns the number of occurrences of $substring in the given string. |
||
398 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
399 | * by setting $caseSensitive to false. |
||
400 | * |
||
401 | * @param string $substring The substring to search for |
||
402 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
403 | * |
||
404 | * @return int The number of $substring occurrences |
||
405 | */ |
||
406 | 15 | public function countSubstr($substring, $caseSensitive = true) |
|
417 | |||
418 | /** |
||
419 | * Returns a lowercase and trimmed string separated by dashes. Dashes are |
||
420 | * inserted before uppercase characters (with the exception of the first |
||
421 | * character of the string), and in place of spaces as well as underscores. |
||
422 | * |
||
423 | * @return Stringy Object with a dasherized $str |
||
424 | */ |
||
425 | 19 | public function dasherize() |
|
429 | |||
430 | /** |
||
431 | * Returns a lowercase and trimmed string separated by the given delimiter. |
||
432 | * Delimiters are inserted before uppercase characters (with the exception |
||
433 | * of the first character of the string), and in place of spaces, dashes, |
||
434 | * and underscores. Alpha delimiters are not converted to lowercase. |
||
435 | * |
||
436 | * @param string $delimiter Sequence used to separate parts of the string |
||
437 | * |
||
438 | * @return Stringy Object with a delimited $str |
||
439 | */ |
||
440 | 49 | public function delimit($delimiter) |
|
452 | |||
453 | /** |
||
454 | * Ensures that the string begins with $substring. If it doesn't, it's |
||
455 | * prepended. |
||
456 | * |
||
457 | * @param string $substring The substring to add if not present |
||
458 | * |
||
459 | * @return Stringy Object with its $str prefixed by the $substring |
||
460 | */ |
||
461 | 10 | View Code Duplication | public function ensureLeft($substring) |
471 | |||
472 | /** |
||
473 | * Returns true if the string begins with $substring, false otherwise. By |
||
474 | * default, the comparison is case-sensitive, but can be made insensitive |
||
475 | * by setting $caseSensitive to false. |
||
476 | * |
||
477 | * @param string $substring The substring to look for |
||
478 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
479 | * |
||
480 | * @return bool Whether or not $str starts with $substring |
||
481 | */ |
||
482 | 33 | public function startsWith($substring, $caseSensitive = true) |
|
493 | |||
494 | /** |
||
495 | * Ensures that the string ends with $substring. If it doesn't, it's |
||
496 | * appended. |
||
497 | * |
||
498 | * @param string $substring The substring to add if not present |
||
499 | * |
||
500 | * @return Stringy Object with its $str suffixed by the $substring |
||
501 | */ |
||
502 | 10 | View Code Duplication | public function ensureRight($substring) |
512 | |||
513 | /** |
||
514 | * Returns true if the string ends with $substring, false otherwise. By |
||
515 | * default, the comparison is case-sensitive, but can be made insensitive |
||
516 | * by setting $caseSensitive to false. |
||
517 | * |
||
518 | * @param string $substring The substring to look for |
||
519 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
520 | * |
||
521 | * @return bool Whether or not $str ends with $substring |
||
522 | */ |
||
523 | 33 | public function endsWith($substring, $caseSensitive = true) |
|
542 | |||
543 | /** |
||
544 | * Returns the first $n characters of the string. |
||
545 | * |
||
546 | * @param int $n Number of characters to retrieve from the start |
||
547 | * |
||
548 | * @return Stringy Object with its $str being the first $n chars |
||
549 | */ |
||
550 | 12 | View Code Duplication | public function first($n) |
562 | |||
563 | /** |
||
564 | * Returns the encoding used by the Stringy object. |
||
565 | * |
||
566 | * @return string The current value of the $encoding property |
||
567 | */ |
||
568 | 3 | public function getEncoding() |
|
572 | |||
573 | /** |
||
574 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
575 | * interface. The ArrayIterator's constructor is passed an array of chars |
||
576 | * in the multibyte string. This enables the use of foreach with instances |
||
577 | * of Stringy\Stringy. |
||
578 | * |
||
579 | * @return \ArrayIterator An iterator for the characters in the string |
||
580 | */ |
||
581 | 1 | public function getIterator() |
|
585 | |||
586 | /** |
||
587 | * Returns an array consisting of the characters in the string. |
||
588 | * |
||
589 | * @return array An array of string chars |
||
590 | */ |
||
591 | 4 | public function chars() |
|
603 | |||
604 | /** |
||
605 | * Returns the character at $index, with indexes starting at 0. |
||
606 | * |
||
607 | * @param int $index Position of the character |
||
608 | * |
||
609 | * @return Stringy The character at $index |
||
610 | */ |
||
611 | 11 | public function at($index) |
|
615 | |||
616 | /** |
||
617 | * Returns true if the string contains a lower case char, false |
||
618 | * otherwise. |
||
619 | * |
||
620 | * @return bool Whether or not the string contains a lower case character. |
||
621 | */ |
||
622 | 12 | public function hasLowerCase() |
|
626 | |||
627 | /** |
||
628 | * Returns true if $str matches the supplied pattern, false otherwise. |
||
629 | * |
||
630 | * @param string $pattern Regex pattern to match against |
||
631 | * |
||
632 | * @return bool Whether or not $str matches the pattern |
||
633 | */ |
||
634 | 91 | private function matchesPattern($pattern) |
|
642 | |||
643 | /** |
||
644 | * Returns true if the string contains an upper case char, false |
||
645 | * otherwise. |
||
646 | * |
||
647 | * @return bool Whether or not the string contains an upper case character. |
||
648 | */ |
||
649 | 12 | public function hasUpperCase() |
|
653 | |||
654 | /** |
||
655 | * Convert all HTML entities to their applicable characters. |
||
656 | * |
||
657 | * @param int|null $flags Optional flags |
||
658 | * |
||
659 | * @return Stringy Object with the resulting $str after being html decoded. |
||
660 | */ |
||
661 | 5 | public function htmlDecode($flags = ENT_COMPAT) |
|
667 | |||
668 | /** |
||
669 | * Convert all applicable characters to HTML entities. |
||
670 | * |
||
671 | * @param int|null $flags Optional flags |
||
672 | * |
||
673 | * @return Stringy Object with the resulting $str after being html encoded. |
||
674 | */ |
||
675 | 5 | public function htmlEncode($flags = ENT_COMPAT) |
|
681 | |||
682 | /** |
||
683 | * Capitalizes the first word of the string, replaces underscores with |
||
684 | * spaces, and strips '_id'. |
||
685 | * |
||
686 | * @return Stringy Object with a humanized $str |
||
687 | */ |
||
688 | 3 | public function humanize() |
|
694 | |||
695 | /** |
||
696 | * Converts the first character of the supplied string to upper case. |
||
697 | * |
||
698 | * @return Stringy Object with the first character of $str being upper case |
||
699 | */ |
||
700 | 27 | View Code Duplication | public function upperCaseFirst() |
714 | |||
715 | /** |
||
716 | * Returns the index of the last occurrence of $needle in the string, |
||
717 | * and false if not found. Accepts an optional offset from which to begin |
||
718 | * the search. Offsets may be negative to count from the last character |
||
719 | * in the string. |
||
720 | * |
||
721 | * @param string $needle Substring to look for |
||
722 | * @param int $offset Offset from which to search |
||
723 | * |
||
724 | * @return int|bool The last occurrence's index if found, otherwise false |
||
725 | */ |
||
726 | 12 | public function indexOfLast($needle, $offset = 0) |
|
730 | |||
731 | /** |
||
732 | * Inserts $substring into the string at the $index provided. |
||
733 | * |
||
734 | * @param string $substring String to be inserted |
||
735 | * @param int $index The index at which to insert the substring |
||
736 | * |
||
737 | * @return Stringy Object with the resulting $str after the insertion |
||
738 | */ |
||
739 | 8 | View Code Duplication | public function insert($substring, $index) |
753 | |||
754 | /** |
||
755 | * Returns true if the string contains only alphabetic chars, false |
||
756 | * otherwise. |
||
757 | * |
||
758 | * @return bool Whether or not $str contains only alphabetic chars |
||
759 | */ |
||
760 | 10 | public function isAlpha() |
|
764 | |||
765 | /** |
||
766 | * Determine whether the string is considered to be empty. |
||
767 | * |
||
768 | * A variable is considered empty if it does not exist or if its value equals FALSE. |
||
769 | * empty() does not generate a warning if the variable does not exist. |
||
770 | * |
||
771 | * @return bool |
||
772 | */ |
||
773 | public function isEmpty() |
||
777 | |||
778 | /** |
||
779 | * Returns true if the string contains only alphabetic and numeric chars, |
||
780 | * false otherwise. |
||
781 | * |
||
782 | * @return bool Whether or not $str contains only alphanumeric chars |
||
783 | */ |
||
784 | 13 | public function isAlphanumeric() |
|
788 | |||
789 | /** |
||
790 | * Returns true if the string contains only whitespace chars, false |
||
791 | * otherwise. |
||
792 | * |
||
793 | * @return bool Whether or not $str contains only whitespace characters |
||
794 | */ |
||
795 | 15 | public function isBlank() |
|
799 | |||
800 | /** |
||
801 | * Returns true if the string contains only hexadecimal chars, false |
||
802 | * otherwise. |
||
803 | * |
||
804 | * @return bool Whether or not $str contains only hexadecimal chars |
||
805 | */ |
||
806 | 13 | public function isHexadecimal() |
|
810 | |||
811 | /** |
||
812 | * Returns true if the string contains HTML-Tags, false otherwise. |
||
813 | * |
||
814 | * @return bool Whether or not $str contains HTML-Tags |
||
815 | */ |
||
816 | 1 | public function isHtml() |
|
820 | |||
821 | /** |
||
822 | * Returns true if the string contains a valid E-Mail address, false otherwise. |
||
823 | * |
||
824 | * @param bool $useExampleDomainCheck |
||
825 | * @param bool $useTypoInDomainCheck |
||
826 | * @param bool $useTemporaryDomainCheck |
||
827 | * @param bool $useDnsCheck |
||
828 | * |
||
829 | * @return bool Whether or not $str contains a valid E-Mail address |
||
830 | */ |
||
831 | 1 | public function isEmail($useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false) |
|
835 | |||
836 | /** |
||
837 | * Returns true if the string is JSON, false otherwise. Unlike json_decode |
||
838 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
||
839 | * in that an empty string is not considered valid JSON. |
||
840 | * |
||
841 | * @return bool Whether or not $str is JSON |
||
842 | */ |
||
843 | 20 | public function isJson() |
|
857 | |||
858 | /** |
||
859 | * Returns true if the string contains only lower case chars, false |
||
860 | * otherwise. |
||
861 | * |
||
862 | * @return bool Whether or not $str contains only lower case characters |
||
863 | */ |
||
864 | 8 | public function isLowerCase() |
|
872 | |||
873 | /** |
||
874 | * Returns true if the string is serialized, false otherwise. |
||
875 | * |
||
876 | * @return bool Whether or not $str is serialized |
||
877 | */ |
||
878 | 7 | public function isSerialized() |
|
895 | |||
896 | /** |
||
897 | * Returns true if the string contains only lower case chars, false |
||
898 | * otherwise. |
||
899 | * |
||
900 | * @return bool Whether or not $str contains only lower case characters |
||
901 | */ |
||
902 | 8 | public function isUpperCase() |
|
906 | |||
907 | /** |
||
908 | * Returns the last $n characters of the string. |
||
909 | * |
||
910 | * @param int $n Number of characters to retrieve from the end |
||
911 | * |
||
912 | * @return Stringy Object with its $str being the last $n chars |
||
913 | */ |
||
914 | 12 | View Code Duplication | public function last($n) |
926 | |||
927 | /** |
||
928 | * Splits on newlines and carriage returns, returning an array of Stringy |
||
929 | * objects corresponding to the lines in the string. |
||
930 | * |
||
931 | * @return Stringy[] An array of Stringy objects |
||
932 | */ |
||
933 | 15 | public function lines() |
|
944 | |||
945 | /** |
||
946 | * Returns the longest common prefix between the string and $otherStr. |
||
947 | * |
||
948 | * @param string $otherStr Second string for comparison |
||
949 | * |
||
950 | * @return Stringy Object with its $str being the longest common prefix |
||
951 | */ |
||
952 | 10 | public function longestCommonPrefix($otherStr) |
|
970 | |||
971 | /** |
||
972 | * Returns the longest common suffix between the string and $otherStr. |
||
973 | * |
||
974 | * @param string $otherStr Second string for comparison |
||
975 | * |
||
976 | * @return Stringy Object with its $str being the longest common suffix |
||
977 | */ |
||
978 | 10 | public function longestCommonSuffix($otherStr) |
|
996 | |||
997 | /** |
||
998 | * Returns the longest common substring between the string and $otherStr. |
||
999 | * In the case of ties, it returns that which occurs first. |
||
1000 | * |
||
1001 | * @param string $otherStr Second string for comparison |
||
1002 | * |
||
1003 | * @return Stringy Object with its $str being the longest common substring |
||
1004 | */ |
||
1005 | 10 | public function longestCommonSubstring($otherStr) |
|
1049 | |||
1050 | /** |
||
1051 | * Returns whether or not a character exists at an index. Offsets may be |
||
1052 | * negative to count from the last character in the string. Implements |
||
1053 | * part of the ArrayAccess interface. |
||
1054 | * |
||
1055 | * @param mixed $offset The index to check |
||
1056 | * |
||
1057 | * @return boolean Whether or not the index exists |
||
1058 | */ |
||
1059 | 6 | public function offsetExists($offset) |
|
1071 | |||
1072 | /** |
||
1073 | * Returns the character at the given index. Offsets may be negative to |
||
1074 | * count from the last character in the string. Implements part of the |
||
1075 | * ArrayAccess interface, and throws an OutOfBoundsException if the index |
||
1076 | * does not exist. |
||
1077 | * |
||
1078 | * @param mixed $offset The index from which to retrieve the char |
||
1079 | * |
||
1080 | * @return string The character at the specified index |
||
1081 | * @throws \OutOfBoundsException If the positive or negative offset does |
||
1082 | * not exist |
||
1083 | */ |
||
1084 | 2 | public function offsetGet($offset) |
|
1100 | |||
1101 | /** |
||
1102 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1103 | * when called. This maintains the immutability of Stringy objects. |
||
1104 | * |
||
1105 | * @param mixed $offset The index of the character |
||
1106 | * @param mixed $value Value to set |
||
1107 | * |
||
1108 | * @throws \Exception When called |
||
1109 | */ |
||
1110 | 1 | public function offsetSet($offset, $value) |
|
1115 | |||
1116 | /** |
||
1117 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1118 | * when called. This maintains the immutability of Stringy objects. |
||
1119 | * |
||
1120 | * @param mixed $offset The index of the character |
||
1121 | * |
||
1122 | * @throws \Exception When called |
||
1123 | */ |
||
1124 | 1 | public function offsetUnset($offset) |
|
1129 | |||
1130 | /** |
||
1131 | * Pads the string to a given length with $padStr. If length is less than |
||
1132 | * or equal to the length of the string, no padding takes places. The |
||
1133 | * default string used for padding is a space, and the default type (one of |
||
1134 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
||
1135 | * if $padType isn't one of those 3 values. |
||
1136 | * |
||
1137 | * @param int $length Desired string length after padding |
||
1138 | * @param string $padStr String used to pad, defaults to space |
||
1139 | * @param string $padType One of 'left', 'right', 'both' |
||
1140 | * |
||
1141 | * @return Stringy Object with a padded $str |
||
1142 | * @throws \InvalidArgumentException If $padType isn't one of 'right', 'left' or 'both' |
||
1143 | */ |
||
1144 | 13 | public function pad($length, $padStr = ' ', $padType = 'right') |
|
1161 | |||
1162 | /** |
||
1163 | * Returns a new string of a given length such that the beginning of the |
||
1164 | * string is padded. Alias for pad() with a $padType of 'left'. |
||
1165 | * |
||
1166 | * @param int $length Desired string length after padding |
||
1167 | * @param string $padStr String used to pad, defaults to space |
||
1168 | * |
||
1169 | * @return Stringy String with left padding |
||
1170 | */ |
||
1171 | 10 | public function padLeft($length, $padStr = ' ') |
|
1175 | |||
1176 | /** |
||
1177 | * Adds the specified amount of left and right padding to the given string. |
||
1178 | * The default character used is a space. |
||
1179 | * |
||
1180 | * @param int $left Length of left padding |
||
1181 | * @param int $right Length of right padding |
||
1182 | * @param string $padStr String used to pad |
||
1183 | * |
||
1184 | * @return Stringy String with padding applied |
||
1185 | */ |
||
1186 | 37 | private function applyPadding($left = 0, $right = 0, $padStr = ' ') |
|
1223 | |||
1224 | /** |
||
1225 | * Returns a new string of a given length such that the end of the string |
||
1226 | * is padded. Alias for pad() with a $padType of 'right'. |
||
1227 | * |
||
1228 | * @param int $length Desired string length after padding |
||
1229 | * @param string $padStr String used to pad, defaults to space |
||
1230 | * |
||
1231 | * @return Stringy String with right padding |
||
1232 | */ |
||
1233 | 13 | public function padRight($length, $padStr = ' ') |
|
1237 | |||
1238 | /** |
||
1239 | * Returns a new string of a given length such that both sides of the |
||
1240 | * string are padded. Alias for pad() with a $padType of 'both'. |
||
1241 | * |
||
1242 | * @param int $length Desired string length after padding |
||
1243 | * @param string $padStr String used to pad, defaults to space |
||
1244 | * |
||
1245 | * @return Stringy String with padding applied |
||
1246 | */ |
||
1247 | 14 | public function padBoth($length, $padStr = ' ') |
|
1253 | |||
1254 | /** |
||
1255 | * Returns a new string starting with $string. |
||
1256 | * |
||
1257 | * @param string $string The string to append |
||
1258 | * |
||
1259 | * @return Stringy Object with appended $string |
||
1260 | */ |
||
1261 | 2 | public function prepend($string) |
|
1265 | |||
1266 | /** |
||
1267 | * Returns a new string with the prefix $substring removed, if present. |
||
1268 | * |
||
1269 | * @param string $substring The prefix to remove |
||
1270 | * |
||
1271 | * @return Stringy Object having a $str without the prefix $substring |
||
1272 | */ |
||
1273 | 12 | View Code Duplication | public function removeLeft($substring) |
1285 | |||
1286 | /** |
||
1287 | * Returns a new string with the suffix $substring removed, if present. |
||
1288 | * |
||
1289 | * @param string $substring The suffix to remove |
||
1290 | * |
||
1291 | * @return Stringy Object having a $str without the suffix $substring |
||
1292 | */ |
||
1293 | 12 | View Code Duplication | public function removeRight($substring) |
1305 | |||
1306 | /** |
||
1307 | * Returns a repeated string given a multiplier. |
||
1308 | * |
||
1309 | * @param int $multiplier The number of times to repeat the string |
||
1310 | * |
||
1311 | * @return Stringy Object with a repeated str |
||
1312 | */ |
||
1313 | 7 | public function repeat($multiplier) |
|
1319 | |||
1320 | /** |
||
1321 | * Replaces all occurrences of $search in $str by $replacement. |
||
1322 | * |
||
1323 | * @param string $search The needle to search for |
||
1324 | * @param string $replacement The string to replace with |
||
1325 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
1326 | * |
||
1327 | * @return Stringy Object with the resulting $str after the replacements |
||
1328 | */ |
||
1329 | 28 | View Code Duplication | public function replace($search, $replacement, $caseSensitive = true) |
1339 | |||
1340 | /** |
||
1341 | * Replaces all occurrences of $search in $str by $replacement. |
||
1342 | * |
||
1343 | * @param array $search The elements to search for |
||
1344 | * @param string|array $replacement The string to replace with |
||
1345 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
1346 | * |
||
1347 | * @return Stringy Object with the resulting $str after the replacements |
||
1348 | */ |
||
1349 | 30 | View Code Duplication | public function replaceAll(array $search, $replacement, $caseSensitive = true) |
1359 | |||
1360 | /** |
||
1361 | * Replaces all occurrences of $search from the beginning of string with $replacement |
||
1362 | * |
||
1363 | * @param string $search |
||
1364 | * @param string $replacement |
||
1365 | * |
||
1366 | * @return Stringy Object with the resulting $str after the replacements |
||
1367 | */ |
||
1368 | 16 | public function replaceBeginning($search, $replacement) |
|
1374 | |||
1375 | /** |
||
1376 | * Replaces all occurrences of $search from the ending of string with $replacement |
||
1377 | * |
||
1378 | * @param string $search |
||
1379 | * @param string $replacement |
||
1380 | * |
||
1381 | * @return Stringy Object with the resulting $str after the replacements |
||
1382 | */ |
||
1383 | 16 | public function replaceEnding($search, $replacement) |
|
1389 | |||
1390 | /** |
||
1391 | * Returns a reversed string. A multibyte version of strrev(). |
||
1392 | * |
||
1393 | * @return Stringy Object with a reversed $str |
||
1394 | */ |
||
1395 | 5 | public function reverse() |
|
1401 | |||
1402 | /** |
||
1403 | * Truncates the string to a given length, while ensuring that it does not |
||
1404 | * split words. If $substring is provided, and truncating occurs, the |
||
1405 | * string is further truncated so that the substring may be appended without |
||
1406 | * exceeding the desired length. |
||
1407 | * |
||
1408 | * @param int $length Desired length of the truncated string |
||
1409 | * @param string $substring The substring to append if it can fit |
||
1410 | * |
||
1411 | * @return Stringy Object with the resulting $str after truncating |
||
1412 | */ |
||
1413 | 22 | public function safeTruncate($length, $substring = '') |
|
1438 | |||
1439 | /** |
||
1440 | * A multibyte string shuffle function. It returns a string with its |
||
1441 | * characters in random order. |
||
1442 | * |
||
1443 | * @return Stringy Object with a shuffled $str |
||
1444 | */ |
||
1445 | 3 | public function shuffle() |
|
1451 | |||
1452 | /** |
||
1453 | * Converts the string into an URL slug. This includes replacing non-ASCII |
||
1454 | * characters with their closest ASCII equivalents, removing remaining |
||
1455 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
||
1456 | * $replacement. The replacement defaults to a single dash, and the string |
||
1457 | * is also converted to lowercase. |
||
1458 | * |
||
1459 | * @param string $replacement The string used to replace whitespace |
||
1460 | * @param string $language The language for the url |
||
1461 | * @param bool $strToLower string to lower |
||
1462 | * |
||
1463 | * @return Stringy Object whose $str has been converted to an URL slug |
||
1464 | */ |
||
1465 | 15 | public function slugify($replacement = '-', $language = 'de', $strToLower = true) |
|
1471 | |||
1472 | /** |
||
1473 | * Remove css media-queries. |
||
1474 | * |
||
1475 | * @return Stringy |
||
1476 | */ |
||
1477 | 1 | public function stripeCssMediaQueries() |
|
1483 | |||
1484 | /** |
||
1485 | * Remove empty html-tag. |
||
1486 | * |
||
1487 | * e.g.: <tag></tag> |
||
1488 | * |
||
1489 | * @return Stringy |
||
1490 | */ |
||
1491 | 1 | public function stripeEmptyHtmlTags() |
|
1497 | |||
1498 | /** |
||
1499 | * Converts the string into an valid UTF-8 string. |
||
1500 | * |
||
1501 | * @return Stringy |
||
1502 | */ |
||
1503 | 1 | public function utf8ify() |
|
1507 | |||
1508 | /** |
||
1509 | * escape html |
||
1510 | * |
||
1511 | * @return Stringy |
||
1512 | */ |
||
1513 | 6 | public function escape() |
|
1523 | |||
1524 | /** |
||
1525 | * Create an extract from a text, so if the search-string was found, it will be centered in the output. |
||
1526 | * |
||
1527 | * @param string $search |
||
1528 | * @param int|null $length |
||
1529 | * @param string $ellipsis |
||
1530 | * |
||
1531 | * @return Stringy |
||
1532 | */ |
||
1533 | 1 | public function extractText($search = '', $length = null, $ellipsis = '...') |
|
1650 | |||
1651 | |||
1652 | /** |
||
1653 | * remove xss from html |
||
1654 | * |
||
1655 | * @return Stringy |
||
1656 | */ |
||
1657 | 6 | public function removeXss() |
|
1669 | |||
1670 | /** |
||
1671 | * remove html-break [br | \r\n | \r | \n | ...] |
||
1672 | * |
||
1673 | * @param string $replacement |
||
1674 | * |
||
1675 | * @return Stringy |
||
1676 | */ |
||
1677 | 6 | public function removeHtmlBreak($replacement = '') |
|
1683 | |||
1684 | /** |
||
1685 | * remove html |
||
1686 | * |
||
1687 | * @param $allowableTags |
||
1688 | * |
||
1689 | * @return Stringy |
||
1690 | */ |
||
1691 | 6 | public function removeHtml($allowableTags = null) |
|
1697 | |||
1698 | /** |
||
1699 | * Returns the substring beginning at $start, and up to, but not including |
||
1700 | * the index specified by $end. If $end is omitted, the function extracts |
||
1701 | * the remaining string. If $end is negative, it is computed from the end |
||
1702 | * of the string. |
||
1703 | * |
||
1704 | * @param int $start Initial index from which to begin extraction |
||
1705 | * @param int $end Optional index at which to end extraction |
||
1706 | * |
||
1707 | * @return Stringy Object with its $str being the extracted substring |
||
1708 | */ |
||
1709 | 18 | public function slice($start, $end = null) |
|
1725 | |||
1726 | /** |
||
1727 | * Splits the string with the provided regular expression, returning an |
||
1728 | * array of Stringy objects. An optional integer $limit will truncate the |
||
1729 | * results. |
||
1730 | * |
||
1731 | * @param string $pattern The regex with which to split the string |
||
1732 | * @param int $limit Optional maximum number of results to return |
||
1733 | * |
||
1734 | * @return Stringy[] An array of Stringy objects |
||
1735 | */ |
||
1736 | 19 | public function split($pattern, $limit = null) |
|
1770 | |||
1771 | /** |
||
1772 | * Surrounds $str with the given substring. |
||
1773 | * |
||
1774 | * @param string $substring The substring to add to both sides |
||
1775 | * |
||
1776 | * @return Stringy Object whose $str had the substring both prepended and |
||
1777 | * appended |
||
1778 | */ |
||
1779 | 5 | public function surround($substring) |
|
1785 | |||
1786 | /** |
||
1787 | * Returns a case swapped version of the string. |
||
1788 | * |
||
1789 | * @return Stringy Object whose $str has each character's case swapped |
||
1790 | */ |
||
1791 | 5 | public function swapCase() |
|
1799 | |||
1800 | /** |
||
1801 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
||
1802 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
||
1803 | * equivalents. |
||
1804 | * |
||
1805 | * @return Stringy Object whose $str has those characters removed |
||
1806 | */ |
||
1807 | 4 | public function tidy() |
|
1813 | |||
1814 | /** |
||
1815 | * Returns a trimmed string with the first letter of each word capitalized. |
||
1816 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
1817 | * capitalized. |
||
1818 | * |
||
1819 | * @param array $ignore An array of words not to capitalize |
||
1820 | * |
||
1821 | * @return Stringy Object with a titleized $str |
||
1822 | */ |
||
1823 | 5 | public function titleize($ignore = null) |
|
1844 | |||
1845 | /** |
||
1846 | * Converts all characters in the string to lowercase. An alias for PHP's |
||
1847 | * UTF8::strtolower(). |
||
1848 | * |
||
1849 | * @return Stringy Object with all characters of $str being lowercase |
||
1850 | */ |
||
1851 | 27 | public function toLowerCase() |
|
1857 | |||
1858 | /** |
||
1859 | * Returns true if the string is base64 encoded, false otherwise. |
||
1860 | * |
||
1861 | * @return bool Whether or not $str is base64 encoded |
||
1862 | */ |
||
1863 | 7 | public function isBase64() |
|
1867 | |||
1868 | /** |
||
1869 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
||
1870 | * replaced with their closest ASCII counterparts, and the rest are removed |
||
1871 | * unless instructed otherwise. |
||
1872 | * |
||
1873 | * @param $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance</p> |
||
1874 | * |
||
1875 | * @return Stringy Object whose $str contains only ASCII characters |
||
1876 | */ |
||
1877 | 16 | public function toAscii($strict = false) |
|
1883 | |||
1884 | /** |
||
1885 | * Returns a boolean representation of the given logical string value. |
||
1886 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
1887 | * 'off', and 'no' will return false. In all instances, case is ignored. |
||
1888 | * For other numeric strings, their sign will determine the return value. |
||
1889 | * In addition, blank strings consisting of only whitespace will return |
||
1890 | * false. For all other strings, the return value is a result of a |
||
1891 | * boolean cast. |
||
1892 | * |
||
1893 | * @return bool A boolean value for the string |
||
1894 | */ |
||
1895 | 15 | public function toBoolean() |
|
1917 | |||
1918 | /** |
||
1919 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
||
1920 | * |
||
1921 | * @return string |
||
1922 | */ |
||
1923 | 967 | public function toString() |
|
1927 | |||
1928 | /** |
||
1929 | * Converts each tab in the string to some number of spaces, as defined by |
||
1930 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
||
1931 | * |
||
1932 | * @param int $tabLength Number of spaces to replace each tab with |
||
1933 | * |
||
1934 | * @return Stringy Object whose $str has had tabs switched to spaces |
||
1935 | */ |
||
1936 | 6 | View Code Duplication | public function toSpaces($tabLength = 4) |
1943 | |||
1944 | /** |
||
1945 | * Converts each occurrence of some consecutive number of spaces, as |
||
1946 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
||
1947 | * are converted to a tab. |
||
1948 | * |
||
1949 | * @param int $tabLength Number of spaces to replace with a tab |
||
1950 | * |
||
1951 | * @return Stringy Object whose $str has had spaces switched to tabs |
||
1952 | */ |
||
1953 | 5 | View Code Duplication | public function toTabs($tabLength = 4) |
1960 | |||
1961 | /** |
||
1962 | * Converts the first character of each word in the string to uppercase. |
||
1963 | * |
||
1964 | * @return Stringy Object with all characters of $str being title-cased |
||
1965 | */ |
||
1966 | 5 | public function toTitleCase() |
|
1973 | |||
1974 | /** |
||
1975 | * Converts all characters in the string to uppercase. An alias for PHP's |
||
1976 | * UTF8::strtoupper(). |
||
1977 | * |
||
1978 | * @return Stringy Object with all characters of $str being uppercase |
||
1979 | */ |
||
1980 | 5 | public function toUpperCase() |
|
1986 | |||
1987 | /** |
||
1988 | * Returns a string with whitespace removed from the start of the string. |
||
1989 | * Supports the removal of unicode whitespace. Accepts an optional |
||
1990 | * string of characters to strip instead of the defaults. |
||
1991 | * |
||
1992 | * @param string $chars Optional string of characters to strip |
||
1993 | * |
||
1994 | * @return Stringy Object with a trimmed $str |
||
1995 | */ |
||
1996 | 13 | View Code Duplication | public function trimLeft($chars = null) |
2006 | |||
2007 | /** |
||
2008 | * Returns a string with whitespace removed from the end of the string. |
||
2009 | * Supports the removal of unicode whitespace. Accepts an optional |
||
2010 | * string of characters to strip instead of the defaults. |
||
2011 | * |
||
2012 | * @param string $chars Optional string of characters to strip |
||
2013 | * |
||
2014 | * @return Stringy Object with a trimmed $str |
||
2015 | */ |
||
2016 | 13 | View Code Duplication | public function trimRight($chars = null) |
2026 | |||
2027 | /** |
||
2028 | * Truncates the string to a given length. If $substring is provided, and |
||
2029 | * truncating occurs, the string is further truncated so that the substring |
||
2030 | * may be appended without exceeding the desired length. |
||
2031 | * |
||
2032 | * @param int $length Desired length of the truncated string |
||
2033 | * @param string $substring The substring to append if it can fit |
||
2034 | * |
||
2035 | * @return Stringy Object with the resulting $str after truncating |
||
2036 | */ |
||
2037 | 22 | View Code Duplication | public function truncate($length, $substring = '') |
2053 | |||
2054 | /** |
||
2055 | * Returns a lowercase and trimmed string separated by underscores. |
||
2056 | * Underscores are inserted before uppercase characters (with the exception |
||
2057 | * of the first character of the string), and in place of spaces as well as |
||
2058 | * dashes. |
||
2059 | * |
||
2060 | * @return Stringy Object with an underscored $str |
||
2061 | */ |
||
2062 | 16 | public function underscored() |
|
2066 | |||
2067 | /** |
||
2068 | * Returns an UpperCamelCase version of the supplied string. It trims |
||
2069 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
||
2070 | * and underscores, and removes spaces, dashes, underscores. |
||
2071 | * |
||
2072 | * @return Stringy Object with $str in UpperCamelCase |
||
2073 | */ |
||
2074 | 13 | public function upperCamelize() |
|
2078 | |||
2079 | /** |
||
2080 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
2081 | * capitalizes letters following digits, spaces, dashes and underscores, |
||
2082 | * and removes spaces, dashes, as well as underscores. |
||
2083 | * |
||
2084 | * @return Stringy Object with $str in camelCase |
||
2085 | */ |
||
2086 | 32 | public function camelize() |
|
2114 | |||
2115 | /** |
||
2116 | * Convert a string to e.g.: "snake_case" |
||
2117 | * |
||
2118 | * @return Stringy Object with $str in snake_case |
||
2119 | */ |
||
2120 | 20 | public function snakeize() |
|
2163 | |||
2164 | /** |
||
2165 | * Converts the first character of the string to lower case. |
||
2166 | * |
||
2167 | * @return Stringy Object with the first character of $str being lower case |
||
2168 | */ |
||
2169 | 37 | View Code Duplication | public function lowerCaseFirst() |
2178 | |||
2179 | /** |
||
2180 | * Shorten the string after $length, but also after the next word. |
||
2181 | * |
||
2182 | * @param int $length |
||
2183 | * @param string $strAddOn |
||
2184 | * |
||
2185 | * @return Stringy |
||
2186 | */ |
||
2187 | 4 | public function shortenAfterWord($length, $strAddOn = '...') |
|
2193 | |||
2194 | /** |
||
2195 | * Line-Wrap the string after $limit, but also after the next word. |
||
2196 | * |
||
2197 | * @param int $limit |
||
2198 | * |
||
2199 | * @return Stringy |
||
2200 | */ |
||
2201 | 1 | public function lineWrapAfterWord($limit) |
|
2213 | |||
2214 | /** |
||
2215 | * Gets the substring after the first occurrence of a separator. |
||
2216 | * If no match is found returns new empty Stringy object. |
||
2217 | * |
||
2218 | * @param string $separator |
||
2219 | * |
||
2220 | * @return Stringy |
||
2221 | */ |
||
2222 | 1 | View Code Duplication | public function afterFirst($separator) |
2238 | |||
2239 | /** |
||
2240 | * Gets the substring after the last occurrence of a separator. |
||
2241 | * If no match is found returns new empty Stringy object. |
||
2242 | * |
||
2243 | * @param string $separator |
||
2244 | * |
||
2245 | * @return Stringy |
||
2246 | */ |
||
2247 | 1 | public function afterLast($separator) |
|
2264 | |||
2265 | /** |
||
2266 | * Gets the substring before the first occurrence of a separator. |
||
2267 | * If no match is found returns new empty Stringy object. |
||
2268 | * |
||
2269 | * @param string $separator |
||
2270 | * |
||
2271 | * @return Stringy |
||
2272 | */ |
||
2273 | 1 | View Code Duplication | public function beforeFirst($separator) |
2290 | |||
2291 | /** |
||
2292 | * Gets the substring before the last occurrence of a separator. |
||
2293 | * If no match is found returns new empty Stringy object. |
||
2294 | * |
||
2295 | * @param string $separator |
||
2296 | * |
||
2297 | * @return Stringy |
||
2298 | */ |
||
2299 | 1 | View Code Duplication | public function beforeLast($separator) |
2316 | |||
2317 | /** |
||
2318 | * Returns the string with the first letter of each word capitalized, |
||
2319 | * except for when the word is a name which shouldn't be capitalized. |
||
2320 | * |
||
2321 | * @return Stringy Object with $str capitalized |
||
2322 | */ |
||
2323 | 39 | public function capitalizePersonalName() |
|
2331 | |||
2332 | /** |
||
2333 | * @param string $word |
||
2334 | * |
||
2335 | * @return string |
||
2336 | */ |
||
2337 | 7 | private function capitalizeWord($word) |
|
2347 | |||
2348 | /** |
||
2349 | * Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius"). |
||
2350 | * |
||
2351 | * @param string $names |
||
2352 | * @param string $delimiter |
||
2353 | * |
||
2354 | * @return string |
||
2355 | */ |
||
2356 | 39 | private function capitalizePersonalNameByDelimiter($names, $delimiter) |
|
2432 | } |
||
2433 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: