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 |
||
14 | class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess |
||
15 | { |
||
16 | /** |
||
17 | * An instance's string. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $str; |
||
22 | |||
23 | /** |
||
24 | * The string's encoding, which should be one of the mbstring module's |
||
25 | * supported encodings. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $encoding; |
||
30 | |||
31 | /** |
||
32 | * Initializes a Stringy object and assigns both str and encoding properties |
||
33 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
34 | * $encoding is not specified, it defaults to mb_internal_encoding(). Throws |
||
35 | * an InvalidArgumentException if the first argument is an array or object |
||
36 | * without a __toString method. |
||
37 | * |
||
38 | * @param mixed $str Value to modify, after being cast to string |
||
39 | * @param string $encoding The character encoding |
||
40 | * |
||
41 | * @throws \InvalidArgumentException if an array or object without a |
||
42 | * __toString method is passed as the first argument |
||
43 | */ |
||
44 | 933 | public function __construct($str = '', $encoding = null) |
|
79 | |||
80 | /** |
||
81 | * Returns the value in $str. |
||
82 | * |
||
83 | * @return string The current value of the $str property |
||
84 | */ |
||
85 | 892 | public function __toString() |
|
89 | |||
90 | /** |
||
91 | * Returns a new string with $string appended. |
||
92 | * |
||
93 | * @param string $string The string to append |
||
94 | * |
||
95 | * @return Stringy Object with appended $string |
||
96 | */ |
||
97 | 2 | public function append($string) |
|
101 | |||
102 | /** |
||
103 | * Creates a Stringy object and assigns both str and encoding properties |
||
104 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
105 | * $encoding is not specified, it defaults to mb_internal_encoding(). It |
||
106 | * then returns the initialized object. Throws an InvalidArgumentException |
||
107 | * if the first argument is an array or object without a __toString method. |
||
108 | * |
||
109 | * @param mixed $str Value to modify, after being cast to string |
||
110 | * @param string $encoding The character encoding |
||
111 | * |
||
112 | * @return Stringy A Stringy object |
||
113 | * @throws \InvalidArgumentException if an array or object without a |
||
114 | * __toString method is passed as the first argument |
||
115 | */ |
||
116 | 923 | public static function create($str = '', $encoding = null) |
|
120 | |||
121 | /** |
||
122 | * Returns the substring between $start and $end, if found, or an empty |
||
123 | * string. An optional offset may be supplied from which to begin the |
||
124 | * search for the start string. |
||
125 | * |
||
126 | * @param string $start Delimiter marking the start of the substring |
||
127 | * @param string $end Delimiter marketing the end of the substring |
||
128 | * @param int $offset Index from which to begin the search |
||
129 | * |
||
130 | * @return Stringy Object whose $str has been converted to an URL slug |
||
131 | */ |
||
132 | 16 | public function between($start, $end, $offset = 0) |
|
147 | |||
148 | /** |
||
149 | * Returns the index of the first occurrence of $needle in the string, |
||
150 | * and false if not found. Accepts an optional offset from which to begin |
||
151 | * the search. |
||
152 | * |
||
153 | * @param string $needle Substring to look for |
||
154 | * @param int $offset Offset from which to search |
||
155 | * |
||
156 | * @return int|bool The occurrence's index if found, otherwise false |
||
157 | */ |
||
158 | 26 | public function indexOf($needle, $offset = 0) |
|
162 | |||
163 | /** |
||
164 | * Returns the substring beginning at $start with the specified $length. |
||
165 | * It differs from the UTF8::substr() function in that providing a $length of |
||
166 | * null will return the rest of the string, rather than an empty string. |
||
167 | * |
||
168 | * @param int $start Position of the first character to use |
||
169 | * @param int $length Maximum number of characters used |
||
170 | * |
||
171 | * @return Stringy Object with its $str being the substring |
||
172 | */ |
||
173 | 66 | public function substr($start, $length = null) |
|
183 | |||
184 | /** |
||
185 | * Returns the length of the string. |
||
186 | * |
||
187 | * @return int The number of characters in $str given the encoding |
||
188 | */ |
||
189 | 247 | public function length() |
|
193 | |||
194 | /** |
||
195 | * Trims the string and replaces consecutive whitespace characters with a |
||
196 | * single space. This includes tabs and newline characters, as well as |
||
197 | * multibyte whitespace such as the thin space and ideographic space. |
||
198 | * |
||
199 | * @return Stringy Object with a trimmed $str and condensed whitespace |
||
200 | */ |
||
201 | 13 | public function collapseWhitespace() |
|
205 | |||
206 | /** |
||
207 | * Returns a string with whitespace removed from the start and end of the |
||
208 | * string. Supports the removal of unicode whitespace. Accepts an optional |
||
209 | * string of characters to strip instead of the defaults. |
||
210 | * |
||
211 | * @param string $chars Optional string of characters to strip |
||
212 | * |
||
213 | * @return Stringy Object with a trimmed $str |
||
214 | */ |
||
215 | 114 | View Code Duplication | public function trim($chars = null) |
225 | |||
226 | /** |
||
227 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
228 | * |
||
229 | * @param string $pattern The regular expression pattern |
||
230 | * @param string $replacement The string to replace with |
||
231 | * @param string $options Matching conditions to be used |
||
232 | * |
||
233 | * @return Stringy Object with the result2ing $str after the replacements |
||
234 | */ |
||
235 | 199 | public function regexReplace($pattern, $replacement, $options = '') |
|
249 | |||
250 | /** |
||
251 | * Returns true if the string contains all $needles, false otherwise. By |
||
252 | * default the comparison is case-sensitive, but can be made insensitive by |
||
253 | * setting $caseSensitive to false. |
||
254 | * |
||
255 | * @param array $needles SubStrings to look for |
||
256 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
257 | * |
||
258 | * @return bool Whether or not $str contains $needle |
||
259 | */ |
||
260 | 43 | View Code Duplication | public function containsAll($needles, $caseSensitive = true) |
275 | |||
276 | /** |
||
277 | * Returns true if the string contains $needle, false otherwise. By default |
||
278 | * the comparison is case-sensitive, but can be made insensitive by setting |
||
279 | * $caseSensitive to false. |
||
280 | * |
||
281 | * @param string $needle Substring to look for |
||
282 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
283 | * |
||
284 | * @return bool Whether or not $str contains $needle |
||
285 | */ |
||
286 | 105 | public function contains($needle, $caseSensitive = true) |
|
296 | |||
297 | /** |
||
298 | * Returns true if the string contains any $needles, false otherwise. By |
||
299 | * default the comparison is case-sensitive, but can be made insensitive by |
||
300 | * setting $caseSensitive to false. |
||
301 | * |
||
302 | * @param array $needles SubStrings to look for |
||
303 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
304 | * |
||
305 | * @return bool Whether or not $str contains $needle |
||
306 | */ |
||
307 | 43 | View Code Duplication | public function containsAny($needles, $caseSensitive = true) |
322 | |||
323 | /** |
||
324 | * Returns the length of the string, implementing the countable interface. |
||
325 | * |
||
326 | * @return int The number of characters in the string, given the encoding |
||
327 | */ |
||
328 | 1 | public function count() |
|
332 | |||
333 | /** |
||
334 | * Returns the number of occurrences of $substring in the given string. |
||
335 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
336 | * by setting $caseSensitive to false. |
||
337 | * |
||
338 | * @param string $substring The substring to search for |
||
339 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
340 | * |
||
341 | * @return int The number of $substring occurrences |
||
342 | */ |
||
343 | 15 | public function countSubstr($substring, $caseSensitive = true) |
|
354 | |||
355 | /** |
||
356 | * Returns a lowercase and trimmed string separated by dashes. Dashes are |
||
357 | * inserted before uppercase characters (with the exception of the first |
||
358 | * character of the string), and in place of spaces as well as underscores. |
||
359 | * |
||
360 | * @return Stringy Object with a dasherized $str |
||
361 | */ |
||
362 | 19 | public function dasherize() |
|
366 | |||
367 | /** |
||
368 | * Returns a lowercase and trimmed string separated by the given delimiter. |
||
369 | * Delimiters are inserted before uppercase characters (with the exception |
||
370 | * of the first character of the string), and in place of spaces, dashes, |
||
371 | * and underscores. Alpha delimiters are not converted to lowercase. |
||
372 | * |
||
373 | * @param string $delimiter Sequence used to separate parts of the string |
||
374 | * |
||
375 | * @return Stringy Object with a delimited $str |
||
376 | */ |
||
377 | 49 | public function delimit($delimiter) |
|
389 | |||
390 | /** |
||
391 | * Ensures that the string begins with $substring. If it doesn't, it's |
||
392 | * prepended. |
||
393 | * |
||
394 | * @param string $substring The substring to add if not present |
||
395 | * |
||
396 | * @return Stringy Object with its $str prefixed by the $substring |
||
397 | */ |
||
398 | 10 | View Code Duplication | public function ensureLeft($substring) |
408 | |||
409 | /** |
||
410 | * Returns true if the string begins with $substring, false otherwise. By |
||
411 | * default, the comparison is case-sensitive, but can be made insensitive |
||
412 | * by setting $caseSensitive to false. |
||
413 | * |
||
414 | * @param string $substring The substring to look for |
||
415 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
416 | * |
||
417 | * @return bool Whether or not $str starts with $substring |
||
418 | */ |
||
419 | 33 | public function startsWith($substring, $caseSensitive = true) |
|
420 | { |
||
421 | 33 | $substringLength = UTF8::strlen($substring, $this->encoding); |
|
422 | 33 | $startOfStr = UTF8::substr( |
|
423 | 33 | $this->str, 0, $substringLength, |
|
424 | 33 | $this->encoding |
|
425 | ); |
||
426 | |||
427 | 33 | View Code Duplication | if (!$caseSensitive) { |
428 | 4 | $substring = UTF8::strtolower($substring, $this->encoding); |
|
429 | 4 | $startOfStr = UTF8::strtolower($startOfStr, $this->encoding); |
|
430 | } |
||
431 | |||
432 | 33 | return (string) $substring === $startOfStr; |
|
433 | } |
||
434 | |||
435 | /** |
||
436 | * Ensures that the string ends with $substring. If it doesn't, it's |
||
437 | * appended. |
||
438 | * |
||
439 | * @param string $substring The substring to add if not present |
||
440 | * |
||
441 | * @return Stringy Object with its $str suffixed by the $substring |
||
442 | */ |
||
443 | 10 | View Code Duplication | public function ensureRight($substring) |
453 | |||
454 | /** |
||
455 | * Returns true if the string ends with $substring, false otherwise. By |
||
456 | * default, the comparison is case-sensitive, but can be made insensitive |
||
457 | * by setting $caseSensitive to false. |
||
458 | * |
||
459 | * @param string $substring The substring to look for |
||
460 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
461 | * |
||
462 | * @return bool Whether or not $str ends with $substring |
||
463 | */ |
||
464 | 33 | public function endsWith($substring, $caseSensitive = true) |
|
483 | |||
484 | /** |
||
485 | * Returns the first $n characters of the string. |
||
486 | * |
||
487 | * @param int $n Number of characters to retrieve from the start |
||
488 | * |
||
489 | * @return Stringy Object with its $str being the first $n chars |
||
490 | */ |
||
491 | 12 | View Code Duplication | public function first($n) |
503 | |||
504 | /** |
||
505 | * Returns the encoding used by the Stringy object. |
||
506 | * |
||
507 | * @return string The current value of the $encoding property |
||
508 | */ |
||
509 | 3 | public function getEncoding() |
|
513 | |||
514 | /** |
||
515 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
516 | * interface. The ArrayIterator's constructor is passed an array of chars |
||
517 | * in the multibyte string. This enables the use of foreach with instances |
||
518 | * of Stringy\Stringy. |
||
519 | * |
||
520 | * @return \ArrayIterator An iterator for the characters in the string |
||
521 | */ |
||
522 | 1 | public function getIterator() |
|
526 | |||
527 | /** |
||
528 | * Returns an array consisting of the characters in the string. |
||
529 | * |
||
530 | * @return array An array of string chars |
||
531 | */ |
||
532 | 4 | public function chars() |
|
544 | |||
545 | /** |
||
546 | * Returns the character at $index, with indexes starting at 0. |
||
547 | * |
||
548 | * @param int $index Position of the character |
||
549 | * |
||
550 | * @return Stringy The character at $index |
||
551 | */ |
||
552 | 11 | public function at($index) |
|
556 | |||
557 | /** |
||
558 | * Returns true if the string contains a lower case char, false |
||
559 | * otherwise. |
||
560 | * |
||
561 | * @return bool Whether or not the string contains a lower case character. |
||
562 | */ |
||
563 | 12 | public function hasLowerCase() |
|
567 | |||
568 | /** |
||
569 | * Returns true if $str matches the supplied pattern, false otherwise. |
||
570 | * |
||
571 | * @param string $pattern Regex pattern to match against |
||
572 | * |
||
573 | * @return bool Whether or not $str matches the pattern |
||
574 | */ |
||
575 | 91 | private function matchesPattern($pattern) |
|
583 | |||
584 | /** |
||
585 | * Returns true if the string contains an upper case char, false |
||
586 | * otherwise. |
||
587 | * |
||
588 | * @return bool Whether or not the string contains an upper case character. |
||
589 | */ |
||
590 | 12 | public function hasUpperCase() |
|
594 | |||
595 | /** |
||
596 | * Convert all HTML entities to their applicable characters. |
||
597 | * |
||
598 | * @param int|null $flags Optional flags |
||
599 | * |
||
600 | * @return Stringy Object with the resulting $str after being html decoded. |
||
601 | */ |
||
602 | 5 | public function htmlDecode($flags = ENT_COMPAT) |
|
608 | |||
609 | /** |
||
610 | * Convert all applicable characters to HTML entities. |
||
611 | * |
||
612 | * @param int|null $flags Optional flags |
||
613 | * |
||
614 | * @return Stringy Object with the resulting $str after being html encoded. |
||
615 | */ |
||
616 | 5 | public function htmlEncode($flags = ENT_COMPAT) |
|
622 | |||
623 | /** |
||
624 | * Capitalizes the first word of the string, replaces underscores with |
||
625 | * spaces, and strips '_id'. |
||
626 | * |
||
627 | * @return Stringy Object with a humanized $str |
||
628 | */ |
||
629 | 3 | public function humanize() |
|
635 | |||
636 | /** |
||
637 | * Converts the first character of the supplied string to upper case. |
||
638 | * |
||
639 | * @return Stringy Object with the first character of $str being upper case |
||
640 | */ |
||
641 | 27 | View Code Duplication | public function upperCaseFirst() |
655 | |||
656 | /** |
||
657 | * Returns the index of the last occurrence of $needle in the string, |
||
658 | * and false if not found. Accepts an optional offset from which to begin |
||
659 | * the search. Offsets may be negative to count from the last character |
||
660 | * in the string. |
||
661 | * |
||
662 | * @param string $needle Substring to look for |
||
663 | * @param int $offset Offset from which to search |
||
664 | * |
||
665 | * @return int|bool The last occurrence's index if found, otherwise false |
||
666 | */ |
||
667 | 10 | public function indexOfLast($needle, $offset = 0) |
|
671 | |||
672 | /** |
||
673 | * Inserts $substring into the string at the $index provided. |
||
674 | * |
||
675 | * @param string $substring String to be inserted |
||
676 | * @param int $index The index at which to insert the substring |
||
677 | * |
||
678 | * @return Stringy Object with the resulting $str after the insertion |
||
679 | */ |
||
680 | 8 | View Code Duplication | public function insert($substring, $index) |
697 | |||
698 | /** |
||
699 | * Returns true if the string contains only alphabetic chars, false |
||
700 | * otherwise. |
||
701 | * |
||
702 | * @return bool Whether or not $str contains only alphabetic chars |
||
703 | */ |
||
704 | 10 | public function isAlpha() |
|
708 | |||
709 | /** |
||
710 | * Returns true if the string contains only alphabetic and numeric chars, |
||
711 | * false otherwise. |
||
712 | * |
||
713 | * @return bool Whether or not $str contains only alphanumeric chars |
||
714 | */ |
||
715 | 13 | public function isAlphanumeric() |
|
719 | |||
720 | /** |
||
721 | * Returns true if the string contains only whitespace chars, false |
||
722 | * otherwise. |
||
723 | * |
||
724 | * @return bool Whether or not $str contains only whitespace characters |
||
725 | */ |
||
726 | 15 | public function isBlank() |
|
730 | |||
731 | /** |
||
732 | * Returns true if the string contains only hexadecimal chars, false |
||
733 | * otherwise. |
||
734 | * |
||
735 | * @return bool Whether or not $str contains only hexadecimal chars |
||
736 | */ |
||
737 | 13 | public function isHexadecimal() |
|
741 | |||
742 | /** |
||
743 | * Returns true if the string is JSON, false otherwise. Unlike json_decode |
||
744 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
||
745 | * in that an empty string is not considered valid JSON. |
||
746 | * |
||
747 | * @return bool Whether or not $str is JSON |
||
748 | */ |
||
749 | 20 | public function isJson() |
|
763 | |||
764 | /** |
||
765 | * Returns true if the string contains only lower case chars, false |
||
766 | * otherwise. |
||
767 | * |
||
768 | * @return bool Whether or not $str contains only lower case characters |
||
769 | */ |
||
770 | 8 | public function isLowerCase() |
|
778 | |||
779 | /** |
||
780 | * Returns true if the string is serialized, false otherwise. |
||
781 | * |
||
782 | * @return bool Whether or not $str is serialized |
||
783 | */ |
||
784 | 7 | public function isSerialized() |
|
801 | |||
802 | /** |
||
803 | * Returns true if the string contains only lower case chars, false |
||
804 | * otherwise. |
||
805 | * |
||
806 | * @return bool Whether or not $str contains only lower case characters |
||
807 | */ |
||
808 | 8 | public function isUpperCase() |
|
812 | |||
813 | /** |
||
814 | * Returns the last $n characters of the string. |
||
815 | * |
||
816 | * @param int $n Number of characters to retrieve from the end |
||
817 | * |
||
818 | * @return Stringy Object with its $str being the last $n chars |
||
819 | */ |
||
820 | 12 | View Code Duplication | public function last($n) |
832 | |||
833 | /** |
||
834 | * Splits on newlines and carriage returns, returning an array of Stringy |
||
835 | * objects corresponding to the lines in the string. |
||
836 | * |
||
837 | * @return Stringy[] An array of Stringy objects |
||
838 | */ |
||
839 | 15 | public function lines() |
|
849 | |||
850 | /** |
||
851 | * Returns the longest common prefix between the string and $otherStr. |
||
852 | * |
||
853 | * @param string $otherStr Second string for comparison |
||
854 | * |
||
855 | * @return Stringy Object with its $str being the longest common prefix |
||
856 | */ |
||
857 | 10 | public function longestCommonPrefix($otherStr) |
|
875 | |||
876 | /** |
||
877 | * Returns the longest common suffix between the string and $otherStr. |
||
878 | * |
||
879 | * @param string $otherStr Second string for comparison |
||
880 | * |
||
881 | * @return Stringy Object with its $str being the longest common suffix |
||
882 | */ |
||
883 | 10 | public function longestCommonSuffix($otherStr) |
|
901 | |||
902 | /** |
||
903 | * Returns the longest common substring between the string and $otherStr. |
||
904 | * In the case of ties, it returns that which occurs first. |
||
905 | * |
||
906 | * @param string $otherStr Second string for comparison |
||
907 | * |
||
908 | * @return Stringy Object with its $str being the longest common substring |
||
909 | */ |
||
910 | 10 | public function longestCommonSubstring($otherStr) |
|
954 | |||
955 | /** |
||
956 | * Returns whether or not a character exists at an index. Offsets may be |
||
957 | * negative to count from the last character in the string. Implements |
||
958 | * part of the ArrayAccess interface. |
||
959 | * |
||
960 | * @param mixed $offset The index to check |
||
961 | * |
||
962 | * @return boolean Whether or not the index exists |
||
963 | */ |
||
964 | 6 | public function offsetExists($offset) |
|
976 | |||
977 | /** |
||
978 | * Returns the character at the given index. Offsets may be negative to |
||
979 | * count from the last character in the string. Implements part of the |
||
980 | * ArrayAccess interface, and throws an OutOfBoundsException if the index |
||
981 | * does not exist. |
||
982 | * |
||
983 | * @param mixed $offset The index from which to retrieve the char |
||
984 | * |
||
985 | * @return string The character at the specified index |
||
986 | * @throws \OutOfBoundsException If the positive or negative offset does |
||
987 | * not exist |
||
988 | */ |
||
989 | 2 | public function offsetGet($offset) |
|
1005 | |||
1006 | /** |
||
1007 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1008 | * when called. This maintains the immutability of Stringy objects. |
||
1009 | * |
||
1010 | * @param mixed $offset The index of the character |
||
1011 | * @param mixed $value Value to set |
||
1012 | * |
||
1013 | * @throws \Exception When called |
||
1014 | */ |
||
1015 | 1 | public function offsetSet($offset, $value) |
|
1020 | |||
1021 | /** |
||
1022 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1023 | * when called. This maintains the immutability of Stringy objects. |
||
1024 | * |
||
1025 | * @param mixed $offset The index of the character |
||
1026 | * |
||
1027 | * @throws \Exception When called |
||
1028 | */ |
||
1029 | 1 | public function offsetUnset($offset) |
|
1034 | |||
1035 | /** |
||
1036 | * Pads the string to a given length with $padStr. If length is less than |
||
1037 | * or equal to the length of the string, no padding takes places. The |
||
1038 | * default string used for padding is a space, and the default type (one of |
||
1039 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
||
1040 | * if $padType isn't one of those 3 values. |
||
1041 | * |
||
1042 | * @param int $length Desired string length after padding |
||
1043 | * @param string $padStr String used to pad, defaults to space |
||
1044 | * @param string $padType One of 'left', 'right', 'both' |
||
1045 | * |
||
1046 | * @return Stringy Object with a padded $str |
||
1047 | * @throws \InvalidArgumentException If $padType isn't one of 'right', 'left' or 'both' |
||
1048 | */ |
||
1049 | 13 | public function pad($length, $padStr = ' ', $padType = 'right') |
|
1050 | { |
||
1051 | 13 | if (!in_array($padType, array('left', 'right', 'both'), true)) { |
|
1052 | 1 | throw new \InvalidArgumentException( |
|
1053 | 1 | 'Pad expects $padType ' . "to be one of 'left', 'right' or 'both'" |
|
1054 | ); |
||
1055 | } |
||
1056 | |||
1057 | switch ($padType) { |
||
1058 | 12 | case 'left': |
|
1059 | 3 | return $this->padLeft($length, $padStr); |
|
1060 | 3 | case 'right': |
|
1061 | 6 | return $this->padRight($length, $padStr); |
|
1062 | default: |
||
1063 | 3 | return $this->padBoth($length, $padStr); |
|
1064 | } |
||
1065 | } |
||
1066 | |||
1067 | /** |
||
1068 | * Returns a new string of a given length such that the beginning of the |
||
1069 | * string is padded. Alias for pad() with a $padType of 'left'. |
||
1070 | * |
||
1071 | * @param int $length Desired string length after padding |
||
1072 | * @param string $padStr String used to pad, defaults to space |
||
1073 | * |
||
1074 | * @return Stringy String with left padding |
||
1075 | */ |
||
1076 | 10 | public function padLeft($length, $padStr = ' ') |
|
1077 | { |
||
1078 | 10 | return $this->applyPadding($length - $this->length(), 0, $padStr); |
|
1079 | } |
||
1080 | |||
1081 | /** |
||
1082 | * Adds the specified amount of left and right padding to the given string. |
||
1083 | * The default character used is a space. |
||
1084 | * |
||
1085 | * @param int $left Length of left padding |
||
1086 | * @param int $right Length of right padding |
||
1087 | * @param string $padStr String used to pad |
||
1088 | * |
||
1089 | * @return Stringy String with padding applied |
||
1090 | */ |
||
1091 | 37 | private function applyPadding($left = 0, $right = 0, $padStr = ' ') |
|
1128 | |||
1129 | /** |
||
1130 | * Returns a new string of a given length such that the end of the string |
||
1131 | * is padded. Alias for pad() with a $padType of 'right'. |
||
1132 | * |
||
1133 | * @param int $length Desired string length after padding |
||
1134 | * @param string $padStr String used to pad, defaults to space |
||
1135 | * |
||
1136 | * @return Stringy String with right padding |
||
1137 | */ |
||
1138 | 13 | public function padRight($length, $padStr = ' ') |
|
1139 | { |
||
1140 | 13 | return $this->applyPadding(0, $length - $this->length(), $padStr); |
|
1141 | } |
||
1142 | |||
1143 | /** |
||
1144 | * Returns a new string of a given length such that both sides of the |
||
1145 | * string are padded. Alias for pad() with a $padType of 'both'. |
||
1146 | * |
||
1147 | * @param int $length Desired string length after padding |
||
1148 | * @param string $padStr String used to pad, defaults to space |
||
1149 | * |
||
1150 | * @return Stringy String with padding applied |
||
1151 | */ |
||
1152 | 14 | public function padBoth($length, $padStr = ' ') |
|
1153 | { |
||
1154 | 14 | $padding = $length - $this->length(); |
|
1155 | |||
1156 | 14 | return $this->applyPadding(floor($padding / 2), ceil($padding / 2), $padStr); |
|
1157 | } |
||
1158 | |||
1159 | /** |
||
1160 | * Returns a new string starting with $string. |
||
1161 | * |
||
1162 | * @param string $string The string to append |
||
1163 | * |
||
1164 | * @return Stringy Object with appended $string |
||
1165 | */ |
||
1166 | 2 | public function prepend($string) |
|
1170 | |||
1171 | /** |
||
1172 | * Returns a new string with the prefix $substring removed, if present. |
||
1173 | * |
||
1174 | * @param string $substring The prefix to remove |
||
1175 | * |
||
1176 | * @return Stringy Object having a $str without the prefix $substring |
||
1177 | */ |
||
1178 | 12 | View Code Duplication | public function removeLeft($substring) |
1190 | |||
1191 | /** |
||
1192 | * Returns a new string with the suffix $substring removed, if present. |
||
1193 | * |
||
1194 | * @param string $substring The suffix to remove |
||
1195 | * |
||
1196 | * @return Stringy Object having a $str without the suffix $substring |
||
1197 | */ |
||
1198 | 12 | View Code Duplication | public function removeRight($substring) |
1210 | |||
1211 | /** |
||
1212 | * Returns a repeated string given a multiplier. |
||
1213 | * |
||
1214 | * @param int $multiplier The number of times to repeat the string |
||
1215 | * |
||
1216 | * @return Stringy Object with a repeated str |
||
1217 | */ |
||
1218 | 7 | public function repeat($multiplier) |
|
1224 | |||
1225 | /** |
||
1226 | * Replaces all occurrences of $search in $str by $replacement. |
||
1227 | * |
||
1228 | * @param string $search The needle to search for |
||
1229 | * @param string $replacement The string to replace with |
||
1230 | * |
||
1231 | * @return Stringy Object with the resulting $str after the replacements |
||
1232 | */ |
||
1233 | 16 | public function replace($search, $replacement) |
|
1237 | |||
1238 | /** |
||
1239 | * Replaces all occurrences of $search from the beginning of string with $replacement |
||
1240 | * |
||
1241 | * @param string $search |
||
1242 | * @param string $replacement |
||
1243 | * |
||
1244 | * @return Stringy Object with the resulting $str after the replacements |
||
1245 | */ |
||
1246 | 16 | public function replaceBeginning($search, $replacement) |
|
1252 | |||
1253 | /** |
||
1254 | * Replaces all occurrences of $search from the ending of string with $replacement |
||
1255 | * |
||
1256 | * @param string $search |
||
1257 | * @param string $replacement |
||
1258 | * |
||
1259 | * @return Stringy Object with the resulting $str after the replacements |
||
1260 | */ |
||
1261 | 16 | public function replaceEnding($search, $replacement) |
|
1267 | |||
1268 | /** |
||
1269 | * Returns a reversed string. A multibyte version of strrev(). |
||
1270 | * |
||
1271 | * @return Stringy Object with a reversed $str |
||
1272 | */ |
||
1273 | 5 | public function reverse() |
|
1279 | |||
1280 | /** |
||
1281 | * Truncates the string to a given length, while ensuring that it does not |
||
1282 | * split words. If $substring is provided, and truncating occurs, the |
||
1283 | * string is further truncated so that the substring may be appended without |
||
1284 | * exceeding the desired length. |
||
1285 | * |
||
1286 | * @param int $length Desired length of the truncated string |
||
1287 | * @param string $substring The substring to append if it can fit |
||
1288 | * |
||
1289 | * @return Stringy Object with the resulting $str after truncating |
||
1290 | */ |
||
1291 | 22 | public function safeTruncate($length, $substring = '') |
|
1316 | |||
1317 | /** |
||
1318 | * A multibyte string shuffle function. It returns a string with its |
||
1319 | * characters in random order. |
||
1320 | * |
||
1321 | * @return Stringy Object with a shuffled $str |
||
1322 | */ |
||
1323 | 3 | public function shuffle() |
|
1329 | |||
1330 | /** |
||
1331 | * Converts the string into an URL slug. This includes replacing non-ASCII |
||
1332 | * characters with their closest ASCII equivalents, removing remaining |
||
1333 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
||
1334 | * $replacement. The replacement defaults to a single dash, and the string |
||
1335 | * is also converted to lowercase. |
||
1336 | * |
||
1337 | * @param string $replacement The string used to replace whitespace |
||
1338 | * @param string $language The language for the url |
||
1339 | * @param bool $strToLower string to lower |
||
1340 | * |
||
1341 | * @return Stringy Object whose $str has been converted to an URL slug |
||
1342 | */ |
||
1343 | 15 | public function slugify($replacement = '-', $language = 'de', $strToLower = true) |
|
1349 | |||
1350 | /** |
||
1351 | * escape html |
||
1352 | * |
||
1353 | * @return Stringy |
||
1354 | */ |
||
1355 | 6 | public function escape() |
|
1365 | |||
1366 | /** |
||
1367 | * remove xss from html |
||
1368 | * |
||
1369 | * @return Stringy |
||
1370 | */ |
||
1371 | 6 | public function removeXss() |
|
1383 | |||
1384 | /** |
||
1385 | * remove html |
||
1386 | * |
||
1387 | * @param $allowableTags |
||
1388 | * |
||
1389 | * @return Stringy |
||
1390 | */ |
||
1391 | 6 | public function removeHtml($allowableTags = null) |
|
1397 | |||
1398 | /** |
||
1399 | * Returns the substring beginning at $start, and up to, but not including |
||
1400 | * the index specified by $end. If $end is omitted, the function extracts |
||
1401 | * the remaining string. If $end is negative, it is computed from the end |
||
1402 | * of the string. |
||
1403 | * |
||
1404 | * @param int $start Initial index from which to begin extraction |
||
1405 | * @param int $end Optional index at which to end extraction |
||
1406 | * |
||
1407 | * @return Stringy Object with its $str being the extracted substring |
||
1408 | */ |
||
1409 | 18 | public function slice($start, $end = null) |
|
1425 | |||
1426 | /** |
||
1427 | * Splits the string with the provided regular expression, returning an |
||
1428 | * array of Stringy objects. An optional integer $limit will truncate the |
||
1429 | * results. |
||
1430 | * |
||
1431 | * @param string $pattern The regex with which to split the string |
||
1432 | * @param int $limit Optional maximum number of results to return |
||
1433 | * |
||
1434 | * @return Stringy[] An array of Stringy objects |
||
1435 | */ |
||
1436 | 19 | public function split($pattern, $limit = null) |
|
1469 | |||
1470 | /** |
||
1471 | * Surrounds $str with the given substring. |
||
1472 | * |
||
1473 | * @param string $substring The substring to add to both sides |
||
1474 | * |
||
1475 | * @return Stringy Object whose $str had the substring both prepended and |
||
1476 | * appended |
||
1477 | */ |
||
1478 | 5 | public function surround($substring) |
|
1484 | |||
1485 | /** |
||
1486 | * Returns a case swapped version of the string. |
||
1487 | * |
||
1488 | * @return Stringy Object whose $str has each character's case swapped |
||
1489 | */ |
||
1490 | 5 | public function swapCase() |
|
1511 | |||
1512 | /** |
||
1513 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
||
1514 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
||
1515 | * equivalents. |
||
1516 | * |
||
1517 | * @return Stringy Object whose $str has those characters removed |
||
1518 | */ |
||
1519 | 4 | public function tidy() |
|
1525 | |||
1526 | /** |
||
1527 | * Returns a trimmed string with the first letter of each word capitalized. |
||
1528 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
1529 | * capitalized. |
||
1530 | * |
||
1531 | * @param array $ignore An array of words not to capitalize |
||
1532 | * |
||
1533 | * @return Stringy Object with a titleized $str |
||
1534 | */ |
||
1535 | 5 | public function titleize($ignore = null) |
|
1556 | |||
1557 | /** |
||
1558 | * Converts all characters in the string to lowercase. An alias for PHP's |
||
1559 | * UTF8::strtolower(). |
||
1560 | * |
||
1561 | * @return Stringy Object with all characters of $str being lowercase |
||
1562 | */ |
||
1563 | 27 | public function toLowerCase() |
|
1569 | |||
1570 | /** |
||
1571 | * Returns true if the string is base64 encoded, false otherwise. |
||
1572 | * |
||
1573 | * @return bool Whether or not $str is base64 encoded |
||
1574 | */ |
||
1575 | 7 | public function isBase64() |
|
1587 | |||
1588 | /** |
||
1589 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
||
1590 | * replaced with their closest ASCII counterparts, and the rest are removed |
||
1591 | * unless instructed otherwise. |
||
1592 | * |
||
1593 | * @return Stringy Object whose $str contains only ASCII characters |
||
1594 | */ |
||
1595 | 16 | public function toAscii() |
|
1601 | |||
1602 | /** |
||
1603 | * Returns a boolean representation of the given logical string value. |
||
1604 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
1605 | * 'off', and 'no' will return false. In all instances, case is ignored. |
||
1606 | * For other numeric strings, their sign will determine the return value. |
||
1607 | * In addition, blank strings consisting of only whitespace will return |
||
1608 | * false. For all other strings, the return value is a result of a |
||
1609 | * boolean cast. |
||
1610 | * |
||
1611 | * @return bool A boolean value for the string |
||
1612 | */ |
||
1613 | 15 | public function toBoolean() |
|
1635 | |||
1636 | /** |
||
1637 | * Converts each tab in the string to some number of spaces, as defined by |
||
1638 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
||
1639 | * |
||
1640 | * @param int $tabLength Number of spaces to replace each tab with |
||
1641 | * |
||
1642 | * @return Stringy Object whose $str has had tabs switched to spaces |
||
1643 | */ |
||
1644 | 6 | View Code Duplication | public function toSpaces($tabLength = 4) |
1651 | |||
1652 | /** |
||
1653 | * Converts each occurrence of some consecutive number of spaces, as |
||
1654 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
||
1655 | * are converted to a tab. |
||
1656 | * |
||
1657 | * @param int $tabLength Number of spaces to replace with a tab |
||
1658 | * |
||
1659 | * @return Stringy Object whose $str has had spaces switched to tabs |
||
1660 | */ |
||
1661 | 5 | View Code Duplication | public function toTabs($tabLength = 4) |
1668 | |||
1669 | /** |
||
1670 | * Converts the first character of each word in the string to uppercase. |
||
1671 | * |
||
1672 | * @return Stringy Object with all characters of $str being title-cased |
||
1673 | */ |
||
1674 | 5 | public function toTitleCase() |
|
1681 | |||
1682 | /** |
||
1683 | * Converts all characters in the string to uppercase. An alias for PHP's |
||
1684 | * UTF8::strtoupper(). |
||
1685 | * |
||
1686 | * @return Stringy Object with all characters of $str being uppercase |
||
1687 | */ |
||
1688 | 5 | public function toUpperCase() |
|
1694 | |||
1695 | /** |
||
1696 | * Returns a string with whitespace removed from the start of the string. |
||
1697 | * Supports the removal of unicode whitespace. Accepts an optional |
||
1698 | * string of characters to strip instead of the defaults. |
||
1699 | * |
||
1700 | * @param string $chars Optional string of characters to strip |
||
1701 | * |
||
1702 | * @return Stringy Object with a trimmed $str |
||
1703 | */ |
||
1704 | 13 | View Code Duplication | public function trimLeft($chars = null) |
1714 | |||
1715 | /** |
||
1716 | * Returns a string with whitespace removed from the end of the string. |
||
1717 | * Supports the removal of unicode whitespace. Accepts an optional |
||
1718 | * string of characters to strip instead of the defaults. |
||
1719 | * |
||
1720 | * @param string $chars Optional string of characters to strip |
||
1721 | * |
||
1722 | * @return Stringy Object with a trimmed $str |
||
1723 | */ |
||
1724 | 13 | View Code Duplication | public function trimRight($chars = null) |
1734 | |||
1735 | /** |
||
1736 | * Truncates the string to a given length. If $substring is provided, and |
||
1737 | * truncating occurs, the string is further truncated so that the substring |
||
1738 | * may be appended without exceeding the desired length. |
||
1739 | * |
||
1740 | * @param int $length Desired length of the truncated string |
||
1741 | * @param string $substring The substring to append if it can fit |
||
1742 | * |
||
1743 | * @return Stringy Object with the resulting $str after truncating |
||
1744 | */ |
||
1745 | 22 | View Code Duplication | public function truncate($length, $substring = '') |
1761 | |||
1762 | /** |
||
1763 | * Returns a lowercase and trimmed string separated by underscores. |
||
1764 | * Underscores are inserted before uppercase characters (with the exception |
||
1765 | * of the first character of the string), and in place of spaces as well as |
||
1766 | * dashes. |
||
1767 | * |
||
1768 | * @return Stringy Object with an underscored $str |
||
1769 | */ |
||
1770 | 16 | public function underscored() |
|
1774 | |||
1775 | /** |
||
1776 | * Returns an UpperCamelCase version of the supplied string. It trims |
||
1777 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
||
1778 | * and underscores, and removes spaces, dashes, underscores. |
||
1779 | * |
||
1780 | * @return Stringy Object with $str in UpperCamelCase |
||
1781 | */ |
||
1782 | 13 | public function upperCamelize() |
|
1786 | |||
1787 | /** |
||
1788 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
1789 | * capitalizes letters following digits, spaces, dashes and underscores, |
||
1790 | * and removes spaces, dashes, as well as underscores. |
||
1791 | * |
||
1792 | * @return Stringy Object with $str in camelCase |
||
1793 | */ |
||
1794 | 32 | public function camelize() |
|
1822 | |||
1823 | /** |
||
1824 | * Converts the first character of the string to lower case. |
||
1825 | * |
||
1826 | * @return Stringy Object with the first character of $str being lower case |
||
1827 | */ |
||
1828 | 37 | View Code Duplication | public function lowerCaseFirst() |
1840 | } |
||
1841 |
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: