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 | 1117 | 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 | 186 | 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): Stringy |
|
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): Stringy |
|
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): Stringy |
|
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'): Stringy |
|
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 | 1107 | public static function create($str = '', string $encoding = null): Stringy |
|
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): Stringy |
|
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): Stringy |
|
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 | 259 | 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(): Stringy |
|
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 | 153 | View Code Duplication | public function trim(string $chars = null): Stringy |
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 | 224 | public function regexReplace(string $pattern, string $replacement, string $options = '', string $delimiter = '/'): Stringy |
|
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(): Stringy |
|
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): Stringy |
|
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): Stringy |
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): Stringy |
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): Stringy |
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): Stringy |
|
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): Stringy |
|
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): Stringy |
|
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(): Stringy |
|
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 | 27 | View Code Duplication | public function upperCaseFirst(): Stringy |
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): Stringy |
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): Stringy |
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): Stringy |
|
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): Stringy |
|
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): Stringy |
|
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) |
|
1305 | |||
1306 | /** |
||
1307 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1308 | * when called. This maintains the immutability of Stringy objects. |
||
1309 | * |
||
1310 | * @param int $offset <p>The index of the character.</p> |
||
1311 | * |
||
1312 | * @throws \Exception <p>When called.</p> |
||
1313 | */ |
||
1314 | 1 | public function offsetUnset($offset) |
|
1319 | |||
1320 | /** |
||
1321 | * Pads the string to a given length with $padStr. If length is less than |
||
1322 | * or equal to the length of the string, no padding takes places. The |
||
1323 | * default string used for padding is a space, and the default type (one of |
||
1324 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
||
1325 | * if $padType isn't one of those 3 values. |
||
1326 | * |
||
1327 | * @param int $length <p>Desired string length after padding.</p> |
||
1328 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1329 | * @param string $padType [optional] <p>One of 'left', 'right', 'both'. Default: 'right'</p> |
||
1330 | * |
||
1331 | * @return static <p>Object with a padded $str.</p> |
||
1332 | * |
||
1333 | * @throws \InvalidArgumentException <p>If $padType isn't one of 'right', 'left' or 'both'.</p> |
||
1334 | */ |
||
1335 | 13 | public function pad(int $length, string $padStr = ' ', string $padType = 'right'): Stringy |
|
1352 | |||
1353 | /** |
||
1354 | * Returns a new string of a given length such that the beginning of the |
||
1355 | * string is padded. Alias for pad() with a $padType of 'left'. |
||
1356 | * |
||
1357 | * @param int $length <p>Desired string length after padding.</p> |
||
1358 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1359 | * |
||
1360 | * @return static <p>String with left padding.</p> |
||
1361 | */ |
||
1362 | 10 | public function padLeft(int $length, string $padStr = ' '): Stringy |
|
1366 | |||
1367 | /** |
||
1368 | * Adds the specified amount of left and right padding to the given string. |
||
1369 | * The default character used is a space. |
||
1370 | * |
||
1371 | * @param int $left [optional] <p>Length of left padding. Default: 0</p> |
||
1372 | * @param int $right [optional] <p>Length of right padding. Default: 0</p> |
||
1373 | * @param string $padStr [optional] <p>String used to pad. Default: ' '</p> |
||
1374 | * |
||
1375 | * @return static <p>String with padding applied.</p> |
||
1376 | */ |
||
1377 | 37 | protected function applyPadding(int $left = 0, int $right = 0, string $padStr = ' '): Stringy |
|
1414 | |||
1415 | /** |
||
1416 | * Returns a new string of a given length such that the end of the string |
||
1417 | * is padded. Alias for pad() with a $padType of 'right'. |
||
1418 | * |
||
1419 | * @param int $length <p>Desired string length after padding.</p> |
||
1420 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1421 | * |
||
1422 | * @return static <p>String with right padding.</p> |
||
1423 | */ |
||
1424 | 13 | public function padRight(int $length, string $padStr = ' '): Stringy |
|
1428 | |||
1429 | /** |
||
1430 | * Returns a new string of a given length such that both sides of the |
||
1431 | * string are padded. Alias for pad() with a $padType of 'both'. |
||
1432 | * |
||
1433 | * @param int $length <p>Desired string length after padding.</p> |
||
1434 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1435 | * |
||
1436 | * @return static <p>String with padding applied.</p> |
||
1437 | */ |
||
1438 | 14 | public function padBoth(int $length, string $padStr = ' '): Stringy |
|
1444 | |||
1445 | /** |
||
1446 | * Returns a new string starting with $string. |
||
1447 | * |
||
1448 | * @param string $string <p>The string to append.</p> |
||
1449 | * |
||
1450 | * @return static <p>Object with appended $string.</p> |
||
1451 | */ |
||
1452 | 2 | public function prepend(string $string): Stringy |
|
1456 | |||
1457 | /** |
||
1458 | * Returns a new string with the prefix $substring removed, if present. |
||
1459 | * |
||
1460 | * @param string $substring <p>The prefix to remove.</p> |
||
1461 | * |
||
1462 | * @return static <p>Object having a $str without the prefix $substring.</p> |
||
1463 | */ |
||
1464 | 12 | View Code Duplication | public function removeLeft(string $substring): Stringy |
1476 | |||
1477 | /** |
||
1478 | * Returns a new string with the suffix $substring removed, if present. |
||
1479 | * |
||
1480 | * @param string $substring <p>The suffix to remove.</p> |
||
1481 | * |
||
1482 | * @return static <p>Object having a $str without the suffix $substring.</p> |
||
1483 | */ |
||
1484 | 12 | View Code Duplication | public function removeRight(string $substring): Stringy |
1496 | |||
1497 | /** |
||
1498 | * Returns a repeated string given a multiplier. |
||
1499 | * |
||
1500 | * @param int $multiplier <p>The number of times to repeat the string.</p> |
||
1501 | * |
||
1502 | * @return static <p>Object with a repeated str.</p> |
||
1503 | */ |
||
1504 | 7 | public function repeat(int $multiplier): Stringy |
|
1510 | |||
1511 | /** |
||
1512 | * Replaces all occurrences of $search in $str by $replacement. |
||
1513 | * |
||
1514 | * @param string $search <p>The needle to search for.</p> |
||
1515 | * @param string $replacement <p>The string to replace with.</p> |
||
1516 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
1517 | * |
||
1518 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1519 | */ |
||
1520 | 29 | View Code Duplication | public function replace(string $search, string $replacement, bool $caseSensitive = true): Stringy |
1530 | |||
1531 | /** |
||
1532 | * Replaces all occurrences of $search in $str by $replacement. |
||
1533 | * |
||
1534 | * @param array $search <p>The elements to search for.</p> |
||
1535 | * @param string|array $replacement <p>The string to replace with.</p> |
||
1536 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
1537 | * |
||
1538 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1539 | */ |
||
1540 | 30 | View Code Duplication | public function replaceAll(array $search, $replacement, bool $caseSensitive = true): Stringy |
1550 | |||
1551 | /** |
||
1552 | * Replaces all occurrences of $search from the beginning of string with $replacement. |
||
1553 | * |
||
1554 | * @param string $search <p>The string to search for.</p> |
||
1555 | * @param string $replacement <p>The replacement.</p> |
||
1556 | * |
||
1557 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1558 | */ |
||
1559 | 16 | public function replaceBeginning(string $search, string $replacement): Stringy |
|
1565 | |||
1566 | /** |
||
1567 | * Replaces all occurrences of $search from the ending of string with $replacement. |
||
1568 | * |
||
1569 | * @param string $search <p>The string to search for.</p> |
||
1570 | * @param string $replacement <p>The replacement.</p> |
||
1571 | * |
||
1572 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1573 | */ |
||
1574 | 16 | public function replaceEnding(string $search, string $replacement): Stringy |
|
1580 | |||
1581 | /** |
||
1582 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". |
||
1583 | * If no match is found returns new empty Stringy object. |
||
1584 | * |
||
1585 | * @param string $needle <p>The string to look for.</p> |
||
1586 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1587 | * |
||
1588 | * @return static |
||
1589 | */ |
||
1590 | 2 | View Code Duplication | public function substringOf(string $needle, bool $beforeNeedle = false): Stringy |
1602 | |||
1603 | /** |
||
1604 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". |
||
1605 | * If no match is found returns new empty Stringy object. |
||
1606 | * |
||
1607 | * @param string $needle <p>The string to look for.</p> |
||
1608 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1609 | * |
||
1610 | * @return static |
||
1611 | */ |
||
1612 | 2 | View Code Duplication | public function substringOfIgnoreCase(string $needle, bool $beforeNeedle = false): Stringy |
1624 | |||
1625 | /** |
||
1626 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". |
||
1627 | * If no match is found returns new empty Stringy object. |
||
1628 | * |
||
1629 | * @param string $needle <p>The string to look for.</p> |
||
1630 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1631 | * |
||
1632 | * @return static |
||
1633 | */ |
||
1634 | 2 | View Code Duplication | public function lastSubstringOf(string $needle, bool $beforeNeedle = false): Stringy |
1646 | |||
1647 | /** |
||
1648 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". |
||
1649 | * If no match is found returns new empty Stringy object. |
||
1650 | * |
||
1651 | * @param string $needle <p>The string to look for.</p> |
||
1652 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1653 | * |
||
1654 | * @return static |
||
1655 | */ |
||
1656 | 1 | View Code Duplication | public function lastSubstringOfIgnoreCase(string $needle, bool $beforeNeedle = false): Stringy |
1668 | |||
1669 | /** |
||
1670 | * Returns a reversed string. A multibyte version of strrev(). |
||
1671 | * |
||
1672 | * @return static <p>Object with a reversed $str.</p> |
||
1673 | */ |
||
1674 | 5 | public function reverse(): Stringy |
|
1680 | |||
1681 | /** |
||
1682 | * Truncates the string to a given length, while ensuring that it does not |
||
1683 | * split words. If $substring is provided, and truncating occurs, the |
||
1684 | * string is further truncated so that the substring may be appended without |
||
1685 | * exceeding the desired length. |
||
1686 | * |
||
1687 | * @param int $length <p>Desired length of the truncated string.</p> |
||
1688 | * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p> |
||
1689 | * |
||
1690 | * @return static <p>Object with the resulting $str after truncating.</p> |
||
1691 | */ |
||
1692 | 23 | public function safeTruncate(int $length, string $substring = ''): Stringy |
|
1721 | |||
1722 | /** |
||
1723 | * A multibyte string shuffle function. It returns a string with its |
||
1724 | * characters in random order. |
||
1725 | * |
||
1726 | * @return static <p>Object with a shuffled $str.</p> |
||
1727 | */ |
||
1728 | 3 | public function shuffle(): Stringy |
|
1734 | |||
1735 | /** |
||
1736 | * Converts the string into an URL slug. This includes replacing non-ASCII |
||
1737 | * characters with their closest ASCII equivalents, removing remaining |
||
1738 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
||
1739 | * $replacement. The replacement defaults to a single dash, and the string |
||
1740 | * is also converted to lowercase. |
||
1741 | * |
||
1742 | * @param string $replacement [optional] <p>The string used to replace whitespace. Default: '-'</p> |
||
1743 | * @param string $language [optional] <p>The language for the url. Default: 'de'</p> |
||
1744 | * @param bool $strToLower [optional] <p>string to lower. Default: true</p> |
||
1745 | * |
||
1746 | * @return static <p>Object whose $str has been converted to an URL slug.</p> |
||
1747 | */ |
||
1748 | 15 | public function slugify(string $replacement = '-', string $language = 'de', bool $strToLower = true): Stringy |
|
1754 | |||
1755 | /** |
||
1756 | * Remove css media-queries. |
||
1757 | * |
||
1758 | * @return static |
||
1759 | */ |
||
1760 | 1 | public function stripeCssMediaQueries(): Stringy |
|
1766 | |||
1767 | /** |
||
1768 | * Strip all whitespace characters. This includes tabs and newline characters, |
||
1769 | * as well as multibyte whitespace such as the thin space and ideographic space. |
||
1770 | * |
||
1771 | * @return static |
||
1772 | */ |
||
1773 | 12 | public function stripWhitespace(): Stringy |
|
1777 | |||
1778 | /** |
||
1779 | * Remove empty html-tag. |
||
1780 | * |
||
1781 | * e.g.: <tag></tag> |
||
1782 | * |
||
1783 | * @return static |
||
1784 | */ |
||
1785 | 1 | public function stripeEmptyHtmlTags(): Stringy |
|
1791 | |||
1792 | /** |
||
1793 | * Converts the string into an valid UTF-8 string. |
||
1794 | * |
||
1795 | * @return static |
||
1796 | */ |
||
1797 | 1 | public function utf8ify(): Stringy |
|
1801 | |||
1802 | /** |
||
1803 | * Create a escape html version of the string via "UTF8::htmlspecialchars()". |
||
1804 | * |
||
1805 | * @return static |
||
1806 | */ |
||
1807 | 6 | public function escape(): Stringy |
|
1817 | |||
1818 | /** |
||
1819 | * Create an extract from a sentence, so if the search-string was found, it try to centered in the output. |
||
1820 | * |
||
1821 | * @param string $search |
||
1822 | * @param int|null $length [optional] <p>Default: null === text->length / 2</p> |
||
1823 | * @param string $replacerForSkippedText [optional] <p>Default: …</p> |
||
1824 | * |
||
1825 | * @return static |
||
1826 | */ |
||
1827 | 1 | public function extractText(string $search = '', int $length = null, string $replacerForSkippedText = '…'): Stringy |
|
1952 | |||
1953 | |||
1954 | /** |
||
1955 | * Try to remove all XSS-attacks from the string. |
||
1956 | * |
||
1957 | * @return static |
||
1958 | */ |
||
1959 | 6 | public function removeXss(): Stringy |
|
1971 | |||
1972 | /** |
||
1973 | * Remove all breaks [<br> | \r\n | \r | \n | ...] from the string. |
||
1974 | * |
||
1975 | * @param string $replacement [optional] <p>Default is a empty string.</p> |
||
1976 | * |
||
1977 | * @return static |
||
1978 | */ |
||
1979 | 6 | public function removeHtmlBreak(string $replacement = ''): Stringy |
|
1985 | |||
1986 | /** |
||
1987 | * Remove html via "strip_tags()" from the string. |
||
1988 | * |
||
1989 | * @param string $allowableTags [optional] <p>You can use the optional second parameter to specify tags which should |
||
1990 | * not be stripped. Default: null |
||
1991 | * </p> |
||
1992 | * |
||
1993 | * @return static |
||
1994 | */ |
||
1995 | 6 | public function removeHtml(string $allowableTags = null): Stringy |
|
2001 | |||
2002 | /** |
||
2003 | * Returns the substring beginning at $start, and up to, but not including |
||
2004 | * the index specified by $end. If $end is omitted, the function extracts |
||
2005 | * the remaining string. If $end is negative, it is computed from the end |
||
2006 | * of the string. |
||
2007 | * |
||
2008 | * @param int $start <p>Initial index from which to begin extraction.</p> |
||
2009 | * @param int $end [optional] <p>Index at which to end extraction. Default: null</p> |
||
2010 | * |
||
2011 | * @return static <p>Object with its $str being the extracted substring.</p> |
||
2012 | */ |
||
2013 | 18 | public function slice(int $start, int $end = null): Stringy |
|
2029 | |||
2030 | /** |
||
2031 | * Splits the string with the provided regular expression, returning an |
||
2032 | * array of Stringy objects. An optional integer $limit will truncate the |
||
2033 | * results. |
||
2034 | * |
||
2035 | * @param string $pattern <p>The regex with which to split the string.</p> |
||
2036 | * @param int $limit [optional] <p>Maximum number of results to return. Default: -1 === no limit</p> |
||
2037 | * |
||
2038 | * @return static[] <p>An array of Stringy objects.</p> |
||
2039 | */ |
||
2040 | 16 | public function split(string $pattern, int $limit = -1): array |
|
2074 | |||
2075 | /** |
||
2076 | * Surrounds $str with the given substring. |
||
2077 | * |
||
2078 | * @param string $substring <p>The substring to add to both sides.</P> |
||
2079 | * |
||
2080 | * @return static <p>Object whose $str had the substring both prepended and appended.</p> |
||
2081 | */ |
||
2082 | 5 | public function surround(string $substring): Stringy |
|
2088 | |||
2089 | /** |
||
2090 | * Returns a case swapped version of the string. |
||
2091 | * |
||
2092 | * @return static <p>Object whose $str has each character's case swapped.</P> |
||
2093 | */ |
||
2094 | 5 | public function swapCase(): Stringy |
|
2102 | |||
2103 | /** |
||
2104 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
||
2105 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
||
2106 | * equivalents. |
||
2107 | * |
||
2108 | * @return static <p>Object whose $str has those characters removed.</p> |
||
2109 | */ |
||
2110 | 4 | public function tidy(): Stringy |
|
2116 | |||
2117 | /** |
||
2118 | * Returns a trimmed string with the first letter of each word capitalized. |
||
2119 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
2120 | * capitalized. |
||
2121 | * |
||
2122 | * @param array|null $ignore [optional] <p>An array of words not to capitalize or null. Default: null</p> |
||
2123 | * |
||
2124 | * @return static <p>Object with a titleized $str.</p> |
||
2125 | */ |
||
2126 | 5 | public function titleize(array $ignore = null): Stringy |
|
2147 | |||
2148 | /** |
||
2149 | * Converts all characters in the string to lowercase. |
||
2150 | * |
||
2151 | * @return static <p>Object with all characters of $str being lowercase.</p> |
||
2152 | */ |
||
2153 | 27 | public function toLowerCase(): Stringy |
|
2159 | |||
2160 | /** |
||
2161 | * Returns true if the string is base64 encoded, false otherwise. |
||
2162 | * |
||
2163 | * @return bool <p>Whether or not $str is base64 encoded.</p> |
||
2164 | */ |
||
2165 | 7 | public function isBase64(): bool |
|
2169 | |||
2170 | /** |
||
2171 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
||
2172 | * replaced with their closest ASCII counterparts, and the rest are removed |
||
2173 | * unless instructed otherwise. |
||
2174 | * |
||
2175 | * @param bool $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance | |
||
2176 | * Default: false</p> |
||
2177 | * |
||
2178 | * @return static <p>Object whose $str contains only ASCII characters.</p> |
||
2179 | */ |
||
2180 | 16 | public function toAscii(bool $strict = false): Stringy |
|
2186 | |||
2187 | /** |
||
2188 | * Returns a boolean representation of the given logical string value. |
||
2189 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
2190 | * 'off', and 'no' will return false. In all instances, case is ignored. |
||
2191 | * For other numeric strings, their sign will determine the return value. |
||
2192 | * In addition, blank strings consisting of only whitespace will return |
||
2193 | * false. For all other strings, the return value is a result of a |
||
2194 | * boolean cast. |
||
2195 | * |
||
2196 | * @return bool <p>A boolean value for the string.</p> |
||
2197 | */ |
||
2198 | 15 | public function toBoolean(): bool |
|
2222 | |||
2223 | /** |
||
2224 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
||
2225 | * |
||
2226 | * @return string |
||
2227 | */ |
||
2228 | 1041 | public function toString(): string |
|
2232 | |||
2233 | /** |
||
2234 | * Converts each tab in the string to some number of spaces, as defined by |
||
2235 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
||
2236 | * |
||
2237 | * @param int $tabLength [optional] <p>Number of spaces to replace each tab with. Default: 4</p> |
||
2238 | * |
||
2239 | * @return static <p>Object whose $str has had tabs switched to spaces.</p> |
||
2240 | */ |
||
2241 | 6 | View Code Duplication | public function toSpaces(int $tabLength = 4): Stringy |
2248 | |||
2249 | /** |
||
2250 | * Converts each occurrence of some consecutive number of spaces, as |
||
2251 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
||
2252 | * are converted to a tab. |
||
2253 | * |
||
2254 | * @param int $tabLength [optional] <p>Number of spaces to replace with a tab. Default: 4</p> |
||
2255 | * |
||
2256 | * @return static <p>Object whose $str has had spaces switched to tabs.</p> |
||
2257 | */ |
||
2258 | 5 | View Code Duplication | public function toTabs(int $tabLength = 4): Stringy |
2265 | |||
2266 | /** |
||
2267 | * Converts the first character of each word in the string to uppercase. |
||
2268 | * |
||
2269 | * @return static Object with all characters of $str being title-cased |
||
2270 | */ |
||
2271 | 5 | public function toTitleCase(): Stringy |
|
2278 | |||
2279 | /** |
||
2280 | * Converts all characters in the string to uppercase. |
||
2281 | * |
||
2282 | * @return static Object with all characters of $str being uppercase |
||
2283 | */ |
||
2284 | 5 | public function toUpperCase(): Stringy |
|
2290 | |||
2291 | /** |
||
2292 | * Returns a string with whitespace removed from the start of the string. |
||
2293 | * Supports the removal of unicode whitespace. Accepts an optional |
||
2294 | * string of characters to strip instead of the defaults. |
||
2295 | * |
||
2296 | * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p> |
||
2297 | * |
||
2298 | * @return static <p>Object with a trimmed $str.</p> |
||
2299 | */ |
||
2300 | 13 | View Code Duplication | public function trimLeft(string $chars = null): Stringy |
2310 | |||
2311 | /** |
||
2312 | * Returns a string with whitespace removed from the end of the string. |
||
2313 | * Supports the removal of unicode whitespace. Accepts an optional |
||
2314 | * string of characters to strip instead of the defaults. |
||
2315 | * |
||
2316 | * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p> |
||
2317 | * |
||
2318 | * @return static <p>Object with a trimmed $str.</p> |
||
2319 | */ |
||
2320 | 13 | View Code Duplication | public function trimRight(string $chars = null): Stringy |
2330 | |||
2331 | /** |
||
2332 | * Truncates the string to a given length. If $substring is provided, and |
||
2333 | * truncating occurs, the string is further truncated so that the substring |
||
2334 | * may be appended without exceeding the desired length. |
||
2335 | * |
||
2336 | * @param int $length <p>Desired length of the truncated string.</p> |
||
2337 | * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p> |
||
2338 | * |
||
2339 | * @return static <p>Object with the resulting $str after truncating.</p> |
||
2340 | */ |
||
2341 | 22 | View Code Duplication | public function truncate(int $length, string $substring = ''): Stringy |
2357 | |||
2358 | /** |
||
2359 | * Returns a lowercase and trimmed string separated by underscores. |
||
2360 | * Underscores are inserted before uppercase characters (with the exception |
||
2361 | * of the first character of the string), and in place of spaces as well as |
||
2362 | * dashes. |
||
2363 | * |
||
2364 | * @return static <p>Object with an underscored $str.</p> |
||
2365 | */ |
||
2366 | 16 | public function underscored(): Stringy |
|
2370 | |||
2371 | /** |
||
2372 | * Returns an UpperCamelCase version of the supplied string. It trims |
||
2373 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
||
2374 | * and underscores, and removes spaces, dashes, underscores. |
||
2375 | * |
||
2376 | * @return static <p>Object with $str in UpperCamelCase.</p> |
||
2377 | */ |
||
2378 | 13 | public function upperCamelize(): Stringy |
|
2382 | |||
2383 | /** |
||
2384 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
2385 | * capitalizes letters following digits, spaces, dashes and underscores, |
||
2386 | * and removes spaces, dashes, as well as underscores. |
||
2387 | * |
||
2388 | * @return static <p>Object with $str in camelCase.</p> |
||
2389 | */ |
||
2390 | 32 | public function camelize(): Stringy |
|
2418 | |||
2419 | /** |
||
2420 | * Convert a string to e.g.: "snake_case" |
||
2421 | * |
||
2422 | * @return static <p>Object with $str in snake_case.</p> |
||
2423 | */ |
||
2424 | 20 | public function snakeize(): Stringy |
|
2467 | |||
2468 | /** |
||
2469 | * Converts the first character of the string to lower case. |
||
2470 | * |
||
2471 | * @return static <p>Object with the first character of $str being lower case.</p> |
||
2472 | */ |
||
2473 | 37 | View Code Duplication | public function lowerCaseFirst(): Stringy |
2482 | |||
2483 | /** |
||
2484 | * Shorten the string after $length, but also after the next word. |
||
2485 | * |
||
2486 | * @param int $length |
||
2487 | * @param string $strAddOn [optional] <p>Default: '…'</p> |
||
2488 | * |
||
2489 | * @return static |
||
2490 | */ |
||
2491 | 4 | public function shortenAfterWord(int $length, string $strAddOn = '…'): Stringy |
|
2497 | |||
2498 | /** |
||
2499 | * Line-Wrap the string after $limit, but also after the next word. |
||
2500 | * |
||
2501 | * @param int $limit |
||
2502 | * |
||
2503 | * @return static |
||
2504 | */ |
||
2505 | 1 | public function lineWrapAfterWord(int $limit): Stringy |
|
2517 | |||
2518 | /** |
||
2519 | * Gets the substring after the first occurrence of a separator. |
||
2520 | * If no match is found returns new empty Stringy object. |
||
2521 | * |
||
2522 | * @param string $separator |
||
2523 | * |
||
2524 | * @return static |
||
2525 | */ |
||
2526 | 2 | View Code Duplication | public function afterFirst(string $separator): Stringy |
2550 | |||
2551 | /** |
||
2552 | * Gets the substring after the first occurrence of a separator. |
||
2553 | * If no match is found returns new empty Stringy object. |
||
2554 | * |
||
2555 | * @param string $separator |
||
2556 | * |
||
2557 | * @return static |
||
2558 | */ |
||
2559 | 1 | View Code Duplication | public function afterFirstIgnoreCase(string $separator): Stringy |
2583 | |||
2584 | /** |
||
2585 | * Gets the substring after the last occurrence of a separator. |
||
2586 | * If no match is found returns new empty Stringy object. |
||
2587 | * |
||
2588 | * @param string $separator |
||
2589 | * |
||
2590 | * @return static |
||
2591 | */ |
||
2592 | 1 | View Code Duplication | public function afterLastIgnoreCase(string $separator): Stringy |
2617 | |||
2618 | /** |
||
2619 | * Gets the substring after the last occurrence of a separator. |
||
2620 | * If no match is found returns new empty Stringy object. |
||
2621 | * |
||
2622 | * @param string $separator |
||
2623 | * |
||
2624 | * @return static |
||
2625 | */ |
||
2626 | 1 | View Code Duplication | public function afterLast(string $separator): Stringy |
2651 | |||
2652 | /** |
||
2653 | * Gets the substring before the first occurrence of a separator. |
||
2654 | * If no match is found returns new empty Stringy object. |
||
2655 | * |
||
2656 | * @param string $separator |
||
2657 | * |
||
2658 | * @return static |
||
2659 | */ |
||
2660 | 1 | View Code Duplication | public function beforeFirst(string $separator): Stringy |
2685 | |||
2686 | /** |
||
2687 | * Gets the substring before the first occurrence of a separator. |
||
2688 | * If no match is found returns new empty Stringy object. |
||
2689 | * |
||
2690 | * @param string $separator |
||
2691 | * |
||
2692 | * @return static |
||
2693 | */ |
||
2694 | 1 | View Code Duplication | public function beforeFirstIgnoreCase(string $separator): Stringy |
2719 | |||
2720 | /** |
||
2721 | * Gets the substring before the last occurrence of a separator. |
||
2722 | * If no match is found returns new empty Stringy object. |
||
2723 | * |
||
2724 | * @param string $separator |
||
2725 | * |
||
2726 | * @return static |
||
2727 | */ |
||
2728 | 1 | View Code Duplication | public function beforeLast(string $separator): Stringy |
2753 | |||
2754 | /** |
||
2755 | * Gets the substring before the last occurrence of a separator. |
||
2756 | * If no match is found returns new empty Stringy object. |
||
2757 | * |
||
2758 | * @param string $separator |
||
2759 | * |
||
2760 | * @return static |
||
2761 | */ |
||
2762 | 1 | View Code Duplication | public function beforeLastIgnoreCase(string $separator): Stringy |
2787 | |||
2788 | /** |
||
2789 | * Returns the string with the first letter of each word capitalized, |
||
2790 | * except for when the word is a name which shouldn't be capitalized. |
||
2791 | * |
||
2792 | * @return static <p>Object with $str capitalized.</p> |
||
2793 | */ |
||
2794 | 39 | public function capitalizePersonalName(): Stringy |
|
2802 | |||
2803 | /** |
||
2804 | * @param string $word |
||
2805 | * |
||
2806 | * @return static <p>Object with $str capitalized.</p> |
||
2807 | */ |
||
2808 | 7 | protected function capitalizeWord(string $word): Stringy |
|
2818 | |||
2819 | /** |
||
2820 | * Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius"). |
||
2821 | * |
||
2822 | * @param string $names |
||
2823 | * @param string $delimiter |
||
2824 | * |
||
2825 | * @return static |
||
2826 | */ |
||
2827 | 39 | protected function capitalizePersonalNameByDelimiter(string $names, string $delimiter): Stringy |
|
2903 | } |
||
2904 |
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: