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. Fallback: 'UTF-8'</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 | 319 | public function __construct($str = '', string $encoding = null) |
|
73 | |||
74 | /** |
||
75 | * Returns the value in $str. |
||
76 | * |
||
77 | * @return string <p>The current value of the $str property.</p> |
||
78 | */ |
||
79 | 29 | public function __toString() |
|
83 | |||
84 | /** |
||
85 | * Gets the substring after the first occurrence of a separator. |
||
86 | * If no match is found returns new empty Stringy object. |
||
87 | * |
||
88 | * @param string $separator |
||
89 | * |
||
90 | * @return static |
||
91 | */ |
||
92 | public function afterFirst(string $separator): self |
||
102 | |||
103 | /** |
||
104 | * Gets the substring after the first occurrence of a separator. |
||
105 | * If no match is found returns new empty Stringy object. |
||
106 | * |
||
107 | * @param string $separator |
||
108 | * |
||
109 | * @return static |
||
110 | */ |
||
111 | public function afterFirstIgnoreCase(string $separator): self |
||
121 | |||
122 | /** |
||
123 | * Gets the substring after the last occurrence of a separator. |
||
124 | * If no match is found returns new empty Stringy object. |
||
125 | * |
||
126 | * @param string $separator |
||
127 | * |
||
128 | * @return static |
||
129 | */ |
||
130 | public function afterLast(string $separator): self |
||
140 | |||
141 | /** |
||
142 | * Gets the substring after the last occurrence of a separator. |
||
143 | * If no match is found returns new empty Stringy object. |
||
144 | * |
||
145 | * @param string $separator |
||
146 | * |
||
147 | * @return static |
||
148 | */ |
||
149 | public function afterLastIgnoreCase(string $separator): self |
||
159 | |||
160 | /** |
||
161 | * Returns a new string with $string appended. |
||
162 | * |
||
163 | * @param string $string <p>The string to append.</p> |
||
164 | * |
||
165 | * @return static <p>Object with appended $string.</p> |
||
166 | */ |
||
167 | 2 | public function append(string $string): self |
|
171 | |||
172 | /** |
||
173 | * Append an password (limited to chars that are good readable). |
||
174 | * |
||
175 | * @param int $length <p>Length of the random string.</p> |
||
176 | * |
||
177 | * @return static <p>Object with appended password.</p> |
||
178 | */ |
||
179 | public function appendPassword(int $length): self |
||
185 | |||
186 | /** |
||
187 | * Append an random string. |
||
188 | * |
||
189 | * @param int $length <p>Length of the random string.</p> |
||
190 | * @param string $possibleChars [optional] <p>Characters string for the random selection.</p> |
||
191 | * |
||
192 | * @return static <p>Object with appended random string.</p> |
||
193 | */ |
||
194 | public function appendRandomString(int $length, string $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'): self |
||
200 | |||
201 | /** |
||
202 | * Append an unique identifier. |
||
203 | * |
||
204 | * @param string|int $entropyExtra [optional] <p>Extra entropy via a string or int value.</p> |
||
205 | * @param bool $md5 [optional] <p>Return the unique identifier as md5-hash? Default: true</p> |
||
206 | * |
||
207 | * @return static <p>Object with appended unique identifier as md5-hash.</p> |
||
208 | */ |
||
209 | public function appendUniqueIdentifier($entropyExtra = '', bool $md5 = true): self |
||
215 | |||
216 | /** |
||
217 | * Returns the character at $index, with indexes starting at 0. |
||
218 | * |
||
219 | * @param int $index <p>Position of the character.</p> |
||
220 | * |
||
221 | * @return static <p>The character at $index.</p> |
||
222 | */ |
||
223 | public function at(int $index): self |
||
229 | |||
230 | /** |
||
231 | * Gets the substring before the first occurrence of a separator. |
||
232 | * If no match is found returns new empty Stringy object. |
||
233 | * |
||
234 | * @param string $separator |
||
235 | * |
||
236 | * @return static |
||
237 | */ |
||
238 | public function beforeFirst(string $separator): self |
||
248 | |||
249 | /** |
||
250 | * Gets the substring before the first occurrence of a separator. |
||
251 | * If no match is found returns new empty Stringy object. |
||
252 | * |
||
253 | * @param string $separator |
||
254 | * |
||
255 | * @return static |
||
256 | */ |
||
257 | public function beforeFirstIgnoreCase(string $separator): self |
||
267 | |||
268 | /** |
||
269 | * Gets the substring before the last occurrence of a separator. |
||
270 | * If no match is found returns new empty Stringy object. |
||
271 | * |
||
272 | * @param string $separator |
||
273 | * |
||
274 | * @return static |
||
275 | */ |
||
276 | public function beforeLast(string $separator): self |
||
286 | |||
287 | /** |
||
288 | * Gets the substring before the last occurrence of a separator. |
||
289 | * If no match is found returns new empty Stringy object. |
||
290 | * |
||
291 | * @param string $separator |
||
292 | * |
||
293 | * @return static |
||
294 | */ |
||
295 | public function beforeLastIgnoreCase(string $separator): self |
||
305 | |||
306 | /** |
||
307 | * Returns the substring between $start and $end, if found, or an empty |
||
308 | * string. An optional offset may be supplied from which to begin the |
||
309 | * search for the start string. |
||
310 | * |
||
311 | * @param string $start <p>Delimiter marking the start of the substring.</p> |
||
312 | * @param string $end <p>Delimiter marking the end of the substring.</p> |
||
313 | * @param int $offset [optional] <p>Index from which to begin the search. Default: 0</p> |
||
314 | * |
||
315 | * @return static <p>Object whose $str is a substring between $start and $end.</p> |
||
316 | */ |
||
317 | public function between(string $start, string $end, int $offset = 0): self |
||
329 | |||
330 | /** |
||
331 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
332 | * capitalizes letters following digits, spaces, dashes and underscores, |
||
333 | * and removes spaces, dashes, as well as underscores. |
||
334 | * |
||
335 | * @return static <p>Object with $str in camelCase.</p> |
||
336 | */ |
||
337 | public function camelize(): self |
||
344 | |||
345 | /** |
||
346 | * Returns the string with the first letter of each word capitalized, |
||
347 | * except for when the word is a name which shouldn't be capitalized. |
||
348 | * |
||
349 | * @return static <p>Object with $str capitalized.</p> |
||
350 | */ |
||
351 | public function capitalizePersonalName(): self |
||
358 | |||
359 | /** |
||
360 | * Returns an array consisting of the characters in the string. |
||
361 | * |
||
362 | * @return array <p>An array of string chars.</p> |
||
363 | */ |
||
364 | public function chars(): array |
||
368 | |||
369 | /** |
||
370 | * Trims the string and replaces consecutive whitespace characters with a |
||
371 | * single space. This includes tabs and newline characters, as well as |
||
372 | * multibyte whitespace such as the thin space and ideographic space. |
||
373 | * |
||
374 | * @return static <p>Object with a trimmed $str and condensed whitespace.</p> |
||
375 | */ |
||
376 | 1 | public function collapseWhitespace(): self |
|
382 | |||
383 | /** |
||
384 | * Returns true if the string contains $needle, false otherwise. By default |
||
385 | * the comparison is case-sensitive, but can be made insensitive by setting |
||
386 | * $caseSensitive to false. |
||
387 | * |
||
388 | * @param string $needle <p>Substring to look for.</p> |
||
389 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
390 | * |
||
391 | * @return bool <p>Whether or not $str contains $needle.</p> |
||
392 | */ |
||
393 | public function contains(string $needle, bool $caseSensitive = true): bool |
||
402 | |||
403 | /** |
||
404 | * Returns true if the string contains all $needles, false otherwise. By |
||
405 | * default the comparison is case-sensitive, but can be made insensitive by |
||
406 | * setting $caseSensitive to false. |
||
407 | * |
||
408 | * @param array $needles <p>SubStrings to look for.</p> |
||
409 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
410 | * |
||
411 | * @return bool <p>Whether or not $str contains $needle.</p> |
||
412 | */ |
||
413 | public function containsAll(array $needles, bool $caseSensitive = true): bool |
||
422 | |||
423 | /** |
||
424 | * Returns true if the string contains any $needles, false otherwise. By |
||
425 | * default the comparison is case-sensitive, but can be made insensitive by |
||
426 | * setting $caseSensitive to false. |
||
427 | * |
||
428 | * @param array $needles <p>SubStrings to look for.</p> |
||
429 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
430 | * |
||
431 | * @return bool <p>Whether or not $str contains $needle.</p> |
||
432 | */ |
||
433 | public function containsAny(array $needles, bool $caseSensitive = true): bool |
||
442 | |||
443 | /** |
||
444 | * Returns the length of the string, implementing the countable interface. |
||
445 | * |
||
446 | * @return int <p>The number of characters in the string, given the encoding.</p> |
||
447 | */ |
||
448 | 1 | public function count(): int |
|
452 | |||
453 | /** |
||
454 | * Returns the number of occurrences of $substring in the given string. |
||
455 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
456 | * by setting $caseSensitive to false. |
||
457 | * |
||
458 | * @param string $substring <p>The substring to search for.</p> |
||
459 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
460 | * |
||
461 | * @return int|false <p>This functions returns an integer or false if there isn't a string.</p> |
||
462 | */ |
||
463 | public function countSubstr(string $substring, bool $caseSensitive = true) |
||
472 | |||
473 | /** |
||
474 | * Creates a Stringy object and assigns both str and encoding properties |
||
475 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
476 | * $encoding is not specified, it defaults to mb_internal_encoding(). It |
||
477 | * then returns the initialized object. Throws an InvalidArgumentException |
||
478 | * if the first argument is an array or object without a __toString method. |
||
479 | * |
||
480 | * @param mixed $str [optional] <p>Value to modify, after being cast to string. Default: ''</p> |
||
481 | * @param string $encoding [optional] <p>The character encoding. Fallback: 'UTF-8'</p> |
||
482 | * |
||
483 | * @return static <p>A Stringy object.</p> |
||
484 | * |
||
485 | * @throws \InvalidArgumentException <p>if an array or object without a |
||
486 | * __toString method is passed as the first argument</p> |
||
487 | */ |
||
488 | 309 | public static function create($str = '', string $encoding = null): self |
|
498 | |||
499 | /** |
||
500 | * Returns a lowercase and trimmed string separated by dashes. Dashes are |
||
501 | * inserted before uppercase characters (with the exception of the first |
||
502 | * character of the string), and in place of spaces as well as underscores. |
||
503 | * |
||
504 | * @return static <p>Object with a dasherized $str</p> |
||
505 | */ |
||
506 | public function dasherize(): self |
||
512 | |||
513 | /** |
||
514 | * Returns a lowercase and trimmed string separated by the given delimiter. |
||
515 | * Delimiters are inserted before uppercase characters (with the exception |
||
516 | * of the first character of the string), and in place of spaces, dashes, |
||
517 | * and underscores. Alpha delimiters are not converted to lowercase. |
||
518 | * |
||
519 | * @param string $delimiter <p>Sequence used to separate parts of the string.</p> |
||
520 | * |
||
521 | * @return static <p>Object with a delimited $str.</p> |
||
522 | */ |
||
523 | public function delimit(string $delimiter): self |
||
529 | |||
530 | /** |
||
531 | * Returns true if the string ends with $substring, false otherwise. By |
||
532 | * default, the comparison is case-sensitive, but can be made insensitive |
||
533 | * by setting $caseSensitive to false. |
||
534 | * |
||
535 | * @param string $substring <p>The substring to look for.</p> |
||
536 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
537 | * |
||
538 | * @return bool <p>Whether or not $str ends with $substring.</p> |
||
539 | */ |
||
540 | 11 | public function endsWith(string $substring, bool $caseSensitive = true): bool |
|
548 | |||
549 | /** |
||
550 | * Returns true if the string ends with any of $substrings, false otherwise. |
||
551 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
552 | * by setting $caseSensitive to false. |
||
553 | * |
||
554 | * @param string[] $substrings <p>Substrings to look for.</p> |
||
555 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
556 | * |
||
557 | * @return bool <p>Whether or not $str ends with $substring.</p> |
||
558 | */ |
||
559 | public function endsWithAny(array $substrings, bool $caseSensitive = true): bool |
||
567 | |||
568 | /** |
||
569 | * Ensures that the string begins with $substring. If it doesn't, it's |
||
570 | * prepended. |
||
571 | * |
||
572 | * @param string $substring <p>The substring to add if not present.</p> |
||
573 | * |
||
574 | * @return static <p>Object with its $str prefixed by the $substring.</p> |
||
575 | */ |
||
576 | public function ensureLeft(string $substring): self |
||
582 | |||
583 | /** |
||
584 | * Ensures that the string ends with $substring. If it doesn't, it's appended. |
||
585 | * |
||
586 | * @param string $substring <p>The substring to add if not present.</p> |
||
587 | * |
||
588 | * @return static <p>Object with its $str suffixed by the $substring.</p> |
||
589 | */ |
||
590 | public function ensureRight(string $substring): self |
||
596 | |||
597 | /** |
||
598 | * Create a escape html version of the string via "UTF8::htmlspecialchars()". |
||
599 | * |
||
600 | * @return static |
||
601 | */ |
||
602 | 6 | public function escape(): self |
|
612 | |||
613 | /** |
||
614 | * Create an extract from a sentence, so if the search-string was found, it try to centered in the output. |
||
615 | * |
||
616 | * @param string $search |
||
617 | * @param int|null $length [optional] <p>Default: null === text->length / 2</p> |
||
618 | * @param string $replacerForSkippedText [optional] <p>Default: …</p> |
||
619 | * |
||
620 | * @return static |
||
621 | */ |
||
622 | public function extractText(string $search = '', int $length = null, string $replacerForSkippedText = '…'): self |
||
634 | |||
635 | /** |
||
636 | * Returns the first $n characters of the string. |
||
637 | * |
||
638 | * @param int $n <p>Number of characters to retrieve from the start.</p> |
||
639 | * |
||
640 | * @return static <p>Object with its $str being the first $n chars.</p> |
||
641 | */ |
||
642 | public function first(int $n): self |
||
648 | |||
649 | /** |
||
650 | * Returns the encoding used by the Stringy object. |
||
651 | * |
||
652 | * @return string <p>The current value of the $encoding property.</p> |
||
653 | */ |
||
654 | 3 | public function getEncoding(): string |
|
658 | |||
659 | /** |
||
660 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
661 | * interface. The ArrayIterator's constructor is passed an array of chars |
||
662 | * in the multibyte string. This enables the use of foreach with instances |
||
663 | * of Stringy\Stringy. |
||
664 | * |
||
665 | * @return \ArrayIterator <p>An iterator for the characters in the string.</p> |
||
666 | */ |
||
667 | public function getIterator(): \ArrayIterator |
||
671 | |||
672 | /** |
||
673 | * Returns true if the string contains a lower case char, false otherwise. |
||
674 | * |
||
675 | * @return bool <p>Whether or not the string contains a lower case character.</p> |
||
676 | */ |
||
677 | public function hasLowerCase(): bool |
||
681 | |||
682 | /** |
||
683 | * Returns true if the string contains an upper case char, false otherwise. |
||
684 | * |
||
685 | * @return bool <p>Whether or not the string contains an upper case character.</p> |
||
686 | */ |
||
687 | public function hasUpperCase(): bool |
||
691 | |||
692 | /** |
||
693 | * Convert all HTML entities to their applicable characters. |
||
694 | * |
||
695 | * @param int $flags [optional] <p> |
||
696 | * A bitmask of one or more of the following flags, which specify how to handle quotes and |
||
697 | * which document type to use. The default is ENT_COMPAT. |
||
698 | * <table> |
||
699 | * Available <i>flags</i> constants |
||
700 | * <tr valign="top"> |
||
701 | * <td>Constant Name</td> |
||
702 | * <td>Description</td> |
||
703 | * </tr> |
||
704 | * <tr valign="top"> |
||
705 | * <td><b>ENT_COMPAT</b></td> |
||
706 | * <td>Will convert double-quotes and leave single-quotes alone.</td> |
||
707 | * </tr> |
||
708 | * <tr valign="top"> |
||
709 | * <td><b>ENT_QUOTES</b></td> |
||
710 | * <td>Will convert both double and single quotes.</td> |
||
711 | * </tr> |
||
712 | * <tr valign="top"> |
||
713 | * <td><b>ENT_NOQUOTES</b></td> |
||
714 | * <td>Will leave both double and single quotes unconverted.</td> |
||
715 | * </tr> |
||
716 | * <tr valign="top"> |
||
717 | * <td><b>ENT_HTML401</b></td> |
||
718 | * <td> |
||
719 | * Handle code as HTML 4.01. |
||
720 | * </td> |
||
721 | * </tr> |
||
722 | * <tr valign="top"> |
||
723 | * <td><b>ENT_XML1</b></td> |
||
724 | * <td> |
||
725 | * Handle code as XML 1. |
||
726 | * </td> |
||
727 | * </tr> |
||
728 | * <tr valign="top"> |
||
729 | * <td><b>ENT_XHTML</b></td> |
||
730 | * <td> |
||
731 | * Handle code as XHTML. |
||
732 | * </td> |
||
733 | * </tr> |
||
734 | * <tr valign="top"> |
||
735 | * <td><b>ENT_HTML5</b></td> |
||
736 | * <td> |
||
737 | * Handle code as HTML 5. |
||
738 | * </td> |
||
739 | * </tr> |
||
740 | * </table> |
||
741 | * </p> |
||
742 | * |
||
743 | * @return static <p>Object with the resulting $str after being html decoded.</p> |
||
744 | */ |
||
745 | 5 | View Code Duplication | public function htmlDecode(int $flags = ENT_COMPAT): self |
755 | |||
756 | /** |
||
757 | * Convert all applicable characters to HTML entities. |
||
758 | * |
||
759 | * @param int $flags [optional] <p> |
||
760 | * A bitmask of one or more of the following flags, which specify how to handle quotes and |
||
761 | * which document type to use. The default is ENT_COMPAT. |
||
762 | * <table> |
||
763 | * Available <i>flags</i> constants |
||
764 | * <tr valign="top"> |
||
765 | * <td>Constant Name</td> |
||
766 | * <td>Description</td> |
||
767 | * </tr> |
||
768 | * <tr valign="top"> |
||
769 | * <td><b>ENT_COMPAT</b></td> |
||
770 | * <td>Will convert double-quotes and leave single-quotes alone.</td> |
||
771 | * </tr> |
||
772 | * <tr valign="top"> |
||
773 | * <td><b>ENT_QUOTES</b></td> |
||
774 | * <td>Will convert both double and single quotes.</td> |
||
775 | * </tr> |
||
776 | * <tr valign="top"> |
||
777 | * <td><b>ENT_NOQUOTES</b></td> |
||
778 | * <td>Will leave both double and single quotes unconverted.</td> |
||
779 | * </tr> |
||
780 | * <tr valign="top"> |
||
781 | * <td><b>ENT_HTML401</b></td> |
||
782 | * <td> |
||
783 | * Handle code as HTML 4.01. |
||
784 | * </td> |
||
785 | * </tr> |
||
786 | * <tr valign="top"> |
||
787 | * <td><b>ENT_XML1</b></td> |
||
788 | * <td> |
||
789 | * Handle code as XML 1. |
||
790 | * </td> |
||
791 | * </tr> |
||
792 | * <tr valign="top"> |
||
793 | * <td><b>ENT_XHTML</b></td> |
||
794 | * <td> |
||
795 | * Handle code as XHTML. |
||
796 | * </td> |
||
797 | * </tr> |
||
798 | * <tr valign="top"> |
||
799 | * <td><b>ENT_HTML5</b></td> |
||
800 | * <td> |
||
801 | * Handle code as HTML 5. |
||
802 | * </td> |
||
803 | * </tr> |
||
804 | * </table> |
||
805 | * </p> |
||
806 | * |
||
807 | * @return static <p>Object with the resulting $str after being html encoded.</p> |
||
808 | */ |
||
809 | 5 | View Code Duplication | public function htmlEncode(int $flags = ENT_COMPAT): self |
819 | |||
820 | /** |
||
821 | * Capitalizes the first word of the string, replaces underscores with |
||
822 | * spaces, and strips '_id'. |
||
823 | * |
||
824 | * @return static <p>Object with a humanized $str.</p> |
||
825 | */ |
||
826 | public function humanize(): self |
||
832 | |||
833 | /** |
||
834 | * Returns the index of the first occurrence of $needle in the string, |
||
835 | * and false if not found. Accepts an optional offset from which to begin |
||
836 | * the search. |
||
837 | * |
||
838 | * @param string $needle <p>Substring to look for.</p> |
||
839 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> |
||
840 | * |
||
841 | * @return int|false <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> |
||
842 | */ |
||
843 | 10 | public function indexOf(string $needle, int $offset = 0) |
|
852 | |||
853 | /** |
||
854 | * Returns the index of the first occurrence of $needle in the string, |
||
855 | * and false if not found. Accepts an optional offset from which to begin |
||
856 | * the search. |
||
857 | * |
||
858 | * @param string $needle <p>Substring to look for.</p> |
||
859 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> |
||
860 | * |
||
861 | * @return int|false <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> |
||
862 | */ |
||
863 | public function indexOfIgnoreCase(string $needle, int $offset = 0) |
||
872 | |||
873 | /** |
||
874 | * Returns the index of the last occurrence of $needle in the string, |
||
875 | * and false if not found. Accepts an optional offset from which to begin |
||
876 | * the search. Offsets may be negative to count from the last character |
||
877 | * in the string. |
||
878 | * |
||
879 | * @param string $needle <p>Substring to look for.</p> |
||
880 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> |
||
881 | * |
||
882 | * @return int|false <p>The last occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> |
||
883 | */ |
||
884 | 10 | 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 | public function indexOfLastIgnoreCase(string $needle, int $offset = 0) |
||
914 | |||
915 | /** |
||
916 | * Inserts $substring into the string at the $index provided. |
||
917 | * |
||
918 | * @param string $substring <p>String to be inserted.</p> |
||
919 | * @param int $index <p>The index at which to insert the substring.</p> |
||
920 | * |
||
921 | * @return static <p>Object with the resulting $str after the insertion.</p> |
||
922 | */ |
||
923 | public function insert(string $substring, int $index): self |
||
934 | |||
935 | /** |
||
936 | * Returns true if the string contains the $pattern, otherwise false. |
||
937 | * |
||
938 | * WARNING: Asterisks ("*") are translated into (".*") zero-or-more regular |
||
939 | * expression wildcards. |
||
940 | * |
||
941 | * @credit Originally from Laravel, thanks Taylor. |
||
942 | * |
||
943 | * @param string $pattern <p>The string or pattern to match against.</p> |
||
944 | * |
||
945 | * @return bool <p>Whether or not we match the provided pattern.</p> |
||
946 | */ |
||
947 | 1 | public function is(string $pattern): bool |
|
958 | |||
959 | /** |
||
960 | * Returns true if the string contains only alphabetic chars, false otherwise. |
||
961 | * |
||
962 | * @return bool <p>Whether or not $str contains only alphabetic chars.</p> |
||
963 | */ |
||
964 | public function isAlpha(): bool |
||
968 | |||
969 | /** |
||
970 | * Returns true if the string contains only alphabetic and numeric chars, false otherwise. |
||
971 | * |
||
972 | * @return bool <p>Whether or not $str contains only alphanumeric chars.</p> |
||
973 | */ |
||
974 | public function isAlphanumeric(): bool |
||
978 | |||
979 | /** |
||
980 | * Returns true if the string is base64 encoded, false otherwise. |
||
981 | * |
||
982 | * @return bool <p>Whether or not $str is base64 encoded.</p> |
||
983 | */ |
||
984 | 7 | public function isBase64(): bool |
|
988 | |||
989 | /** |
||
990 | * Returns true if the string contains only whitespace chars, false otherwise. |
||
991 | * |
||
992 | * @return bool <p>Whether or not $str contains only whitespace characters.</p> |
||
993 | */ |
||
994 | public function isBlank(): bool |
||
998 | |||
999 | /** |
||
1000 | * Returns true if the string contains a valid E-Mail address, false otherwise. |
||
1001 | * |
||
1002 | * @param bool $useExampleDomainCheck [optional] <p>Default: false</p> |
||
1003 | * @param bool $useTypoInDomainCheck [optional] <p>Default: false</p> |
||
1004 | * @param bool $useTemporaryDomainCheck [optional] <p>Default: false</p> |
||
1005 | * @param bool $useDnsCheck [optional] <p>Default: false</p> |
||
1006 | * |
||
1007 | * @return bool <p>Whether or not $str contains a valid E-Mail address.</p> |
||
1008 | */ |
||
1009 | 1 | public function isEmail(bool $useExampleDomainCheck = false, bool $useTypoInDomainCheck = false, bool $useTemporaryDomainCheck = false, bool $useDnsCheck = false): bool |
|
1013 | |||
1014 | /** |
||
1015 | * Determine whether the string is considered to be empty. |
||
1016 | * |
||
1017 | * A variable is considered empty if it does not exist or if its value equals FALSE. |
||
1018 | * empty() does not generate a warning if the variable does not exist. |
||
1019 | * |
||
1020 | * @return bool <p>Whether or not $str is empty().</p> |
||
1021 | */ |
||
1022 | public function isEmpty(): bool |
||
1026 | |||
1027 | /** |
||
1028 | * Returns true if the string contains only hexadecimal chars, false otherwise. |
||
1029 | * |
||
1030 | * @return bool <p>Whether or not $str contains only hexadecimal chars.</p> |
||
1031 | */ |
||
1032 | public function isHexadecimal(): bool |
||
1036 | |||
1037 | /** |
||
1038 | * Returns true if the string contains HTML-Tags, false otherwise. |
||
1039 | * |
||
1040 | * @return bool <p>Whether or not $str contains HTML-Tags.</p> |
||
1041 | */ |
||
1042 | 1 | public function isHtml(): bool |
|
1046 | |||
1047 | /** |
||
1048 | * Returns true if the string is JSON, false otherwise. Unlike json_decode |
||
1049 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
||
1050 | * in that an empty string is not considered valid JSON. |
||
1051 | * |
||
1052 | * @return bool <p>Whether or not $str is JSON.</p> |
||
1053 | */ |
||
1054 | 20 | public function isJson(): bool |
|
1058 | |||
1059 | /** |
||
1060 | * Returns true if the string contains only lower case chars, false otherwise. |
||
1061 | * |
||
1062 | * @return bool <p>Whether or not $str contains only lower case characters.</p> |
||
1063 | */ |
||
1064 | public function isLowerCase(): bool |
||
1068 | |||
1069 | /** |
||
1070 | * Returns true if the string is serialized, false otherwise. |
||
1071 | * |
||
1072 | * @return bool <p>Whether or not $str is serialized.</p> |
||
1073 | */ |
||
1074 | public function isSerialized(): bool |
||
1078 | |||
1079 | /** |
||
1080 | * Returns true if the string contains only lower case chars, false |
||
1081 | * otherwise. |
||
1082 | * |
||
1083 | * @return bool <p>Whether or not $str contains only lower case characters.</p> |
||
1084 | */ |
||
1085 | public function isUpperCase(): bool |
||
1089 | |||
1090 | /** |
||
1091 | * Returns the last $n characters of the string. |
||
1092 | * |
||
1093 | * @param int $n <p>Number of characters to retrieve from the end.</p> |
||
1094 | * |
||
1095 | * @return static <p>Object with its $str being the last $n chars.</p> |
||
1096 | */ |
||
1097 | public function last(int $n): self |
||
1107 | |||
1108 | /** |
||
1109 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". |
||
1110 | * If no match is found returns new empty Stringy object. |
||
1111 | * |
||
1112 | * @param string $needle <p>The string to look for.</p> |
||
1113 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1114 | * |
||
1115 | * @return static |
||
1116 | */ |
||
1117 | public function lastSubstringOf(string $needle, bool $beforeNeedle = false): self |
||
1123 | |||
1124 | /** |
||
1125 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". |
||
1126 | * If no match is found returns new empty Stringy object. |
||
1127 | * |
||
1128 | * @param string $needle <p>The string to look for.</p> |
||
1129 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1130 | * |
||
1131 | * @return static |
||
1132 | */ |
||
1133 | public function lastSubstringOfIgnoreCase(string $needle, bool $beforeNeedle = false): self |
||
1139 | |||
1140 | /** |
||
1141 | * Returns the length of the string. |
||
1142 | * |
||
1143 | * @return int <p>The number of characters in $str given the encoding.</p> |
||
1144 | */ |
||
1145 | 5 | public function length(): int |
|
1149 | |||
1150 | /** |
||
1151 | * Line-Wrap the string after $limit, but also after the next word. |
||
1152 | * |
||
1153 | * @param int $limit |
||
1154 | * |
||
1155 | * @return static |
||
1156 | */ |
||
1157 | public function lineWrapAfterWord(int $limit): self |
||
1163 | |||
1164 | /** |
||
1165 | * Splits on newlines and carriage returns, returning an array of Stringy |
||
1166 | * objects corresponding to the lines in the string. |
||
1167 | * |
||
1168 | * @return static[] <p>An array of Stringy objects.</p> |
||
1169 | */ |
||
1170 | public function lines(): array |
||
1180 | |||
1181 | /** |
||
1182 | * Returns the longest common prefix between the string and $otherStr. |
||
1183 | * |
||
1184 | * @param string $otherStr <p>Second string for comparison.</p> |
||
1185 | * |
||
1186 | * @return static <p>Object with its $str being the longest common prefix.</p> |
||
1187 | */ |
||
1188 | public function longestCommonPrefix(string $otherStr): self |
||
1198 | |||
1199 | /** |
||
1200 | * Returns the longest common substring between the string and $otherStr. |
||
1201 | * In the case of ties, it returns that which occurs first. |
||
1202 | * |
||
1203 | * @param string $otherStr <p>Second string for comparison.</p> |
||
1204 | * |
||
1205 | * @return static <p>Object with its $str being the longest common substring.</p> |
||
1206 | */ |
||
1207 | public function longestCommonSubstring(string $otherStr): self |
||
1217 | |||
1218 | /** |
||
1219 | * Returns the longest common suffix between the string and $otherStr. |
||
1220 | * |
||
1221 | * @param string $otherStr <p>Second string for comparison.</p> |
||
1222 | * |
||
1223 | * @return static <p>Object with its $str being the longest common suffix.</p> |
||
1224 | */ |
||
1225 | public function longestCommonSuffix(string $otherStr): self |
||
1235 | |||
1236 | /** |
||
1237 | * Converts the first character of the string to lower case. |
||
1238 | * |
||
1239 | * @return static <p>Object with the first character of $str being lower case.</p> |
||
1240 | */ |
||
1241 | 5 | public function lowerCaseFirst(): self |
|
1247 | |||
1248 | /** |
||
1249 | * Returns true if $str matches the supplied pattern, false otherwise. |
||
1250 | * |
||
1251 | * @param string $pattern <p>Regex pattern to match against.</p> |
||
1252 | * |
||
1253 | * @return bool <p>Whether or not $str matches the pattern.</p> |
||
1254 | */ |
||
1255 | protected function matchesPattern(string $pattern): bool |
||
1259 | |||
1260 | /** |
||
1261 | * Returns whether or not a character exists at an index. Offsets may be |
||
1262 | * negative to count from the last character in the string. Implements |
||
1263 | * part of the ArrayAccess interface. |
||
1264 | * |
||
1265 | * @param int $offset <p>The index to check.</p> |
||
1266 | * |
||
1267 | * @return boolean <p>Whether or not the index exists.</p> |
||
1268 | */ |
||
1269 | public function offsetExists($offset): bool |
||
1277 | |||
1278 | /** |
||
1279 | * Returns the character at the given index. Offsets may be negative to |
||
1280 | * count from the last character in the string. Implements part of the |
||
1281 | * ArrayAccess interface, and throws an OutOfBoundsException if the index |
||
1282 | * does not exist. |
||
1283 | * |
||
1284 | * @param int $offset <p>The <strong>index</strong> from which to retrieve the char.</p> |
||
1285 | * |
||
1286 | * @return string <p>The character at the specified index.</p> |
||
1287 | * |
||
1288 | * @throws \OutOfBoundsException <p>If the positive or negative offset does not exist.</p> |
||
1289 | */ |
||
1290 | 1 | public function offsetGet($offset): string |
|
1294 | |||
1295 | /** |
||
1296 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1297 | * when called. This maintains the immutability of Stringy objects. |
||
1298 | * |
||
1299 | * @param int $offset <p>The index of the character.</p> |
||
1300 | * @param mixed $value <p>Value to set.</p> |
||
1301 | * |
||
1302 | * @throws \Exception <p>When called.</p> |
||
1303 | */ |
||
1304 | 1 | public function offsetSet($offset, $value) |
|
1310 | |||
1311 | /** |
||
1312 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1313 | * when called. This maintains the immutability of Stringy objects. |
||
1314 | * |
||
1315 | * @param int $offset <p>The index of the character.</p> |
||
1316 | * |
||
1317 | * @throws \Exception <p>When called.</p> |
||
1318 | */ |
||
1319 | 1 | public function offsetUnset($offset) |
|
1325 | |||
1326 | /** |
||
1327 | * Pads the string to a given length with $padStr. If length is less than |
||
1328 | * or equal to the length of the string, no padding takes places. The |
||
1329 | * default string used for padding is a space, and the default type (one of |
||
1330 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
||
1331 | * if $padType isn't one of those 3 values. |
||
1332 | * |
||
1333 | * @param int $length <p>Desired string length after padding.</p> |
||
1334 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1335 | * @param string $padType [optional] <p>One of 'left', 'right', 'both'. Default: 'right'</p> |
||
1336 | * |
||
1337 | * @return static <p>Object with a padded $str.</p> |
||
1338 | * |
||
1339 | * @throws \InvalidArgumentException <p>If $padType isn't one of 'right', 'left' or 'both'.</p> |
||
1340 | */ |
||
1341 | 1 | public function pad(int $length, string $padStr = ' ', string $padType = 'right'): self |
|
1353 | |||
1354 | /** |
||
1355 | * Returns a new string of a given length such that both sides of the |
||
1356 | * string are padded. Alias for pad() with a $padType of 'both'. |
||
1357 | * |
||
1358 | * @param int $length <p>Desired string length after padding.</p> |
||
1359 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1360 | * |
||
1361 | * @return static <p>String with padding applied.</p> |
||
1362 | */ |
||
1363 | public function padBoth(int $length, string $padStr = ' '): self |
||
1374 | |||
1375 | /** |
||
1376 | * Returns a new string of a given length such that the beginning of the |
||
1377 | * string is padded. Alias for pad() with a $padType of 'left'. |
||
1378 | * |
||
1379 | * @param int $length <p>Desired string length after padding.</p> |
||
1380 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1381 | * |
||
1382 | * @return static <p>String with left padding.</p> |
||
1383 | */ |
||
1384 | public function padLeft(int $length, string $padStr = ' '): self |
||
1395 | |||
1396 | /** |
||
1397 | * Returns a new string of a given length such that the end of the string |
||
1398 | * is padded. Alias for pad() with a $padType of 'right'. |
||
1399 | * |
||
1400 | * @param int $length <p>Desired string length after padding.</p> |
||
1401 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1402 | * |
||
1403 | * @return static <p>String with right padding.</p> |
||
1404 | */ |
||
1405 | public function padRight(int $length, string $padStr = ' '): self |
||
1416 | |||
1417 | /** |
||
1418 | * Returns a new string starting with $string. |
||
1419 | * |
||
1420 | * @param string $string <p>The string to append.</p> |
||
1421 | * |
||
1422 | * @return static <p>Object with appended $string.</p> |
||
1423 | */ |
||
1424 | 2 | public function prepend(string $string): self |
|
1428 | |||
1429 | /** |
||
1430 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
1431 | * |
||
1432 | * @param string $pattern <p>The regular expression pattern.</p> |
||
1433 | * @param string $replacement <p>The string to replace with.</p> |
||
1434 | * @param string $options [optional] <p>Matching conditions to be used.</p> |
||
1435 | * @param string $delimiter [optional] <p>Delimiter the the regex. Default: '/'</p> |
||
1436 | * |
||
1437 | * @return static <p>Object with the result2ing $str after the replacements.</p> |
||
1438 | */ |
||
1439 | public function regexReplace(string $pattern, string $replacement, string $options = '', string $delimiter = '/'): self |
||
1451 | |||
1452 | /** |
||
1453 | * Remove html via "strip_tags()" from the string. |
||
1454 | * |
||
1455 | * @param string $allowableTags [optional] <p>You can use the optional second parameter to specify tags which should |
||
1456 | * not be stripped. Default: null |
||
1457 | * </p> |
||
1458 | * |
||
1459 | * @return static |
||
1460 | */ |
||
1461 | public function removeHtml(string $allowableTags = null): self |
||
1467 | |||
1468 | /** |
||
1469 | * Remove all breaks [<br> | \r\n | \r | \n | ...] from the string. |
||
1470 | * |
||
1471 | * @param string $replacement [optional] <p>Default is a empty string.</p> |
||
1472 | * |
||
1473 | * @return static |
||
1474 | */ |
||
1475 | public function removeHtmlBreak(string $replacement = ''): self |
||
1481 | |||
1482 | /** |
||
1483 | * Returns a new string with the prefix $substring removed, if present. |
||
1484 | * |
||
1485 | * @param string $substring <p>The prefix to remove.</p> |
||
1486 | * |
||
1487 | * @return static <p>Object having a $str without the prefix $substring.</p> |
||
1488 | */ |
||
1489 | public function removeLeft(string $substring): self |
||
1495 | |||
1496 | /** |
||
1497 | * Returns a new string with the suffix $substring removed, if present. |
||
1498 | * |
||
1499 | * @param string $substring <p>The suffix to remove.</p> |
||
1500 | * |
||
1501 | * @return static <p>Object having a $str without the suffix $substring.</p> |
||
1502 | */ |
||
1503 | public function removeRight(string $substring): self |
||
1510 | |||
1511 | /** |
||
1512 | * Try to remove all XSS-attacks from the string. |
||
1513 | * |
||
1514 | * @return static |
||
1515 | */ |
||
1516 | 6 | public function removeXss(): self |
|
1528 | |||
1529 | /** |
||
1530 | * Returns a repeated string given a multiplier. |
||
1531 | * |
||
1532 | * @param int $multiplier <p>The number of times to repeat the string.</p> |
||
1533 | * |
||
1534 | * @return static <p>Object with a repeated str.</p> |
||
1535 | */ |
||
1536 | 7 | public function repeat(int $multiplier): self |
|
1542 | |||
1543 | /** |
||
1544 | * Replaces all occurrences of $search in $str by $replacement. |
||
1545 | * |
||
1546 | * @param string $search <p>The needle to search for.</p> |
||
1547 | * @param string $replacement <p>The string to replace with.</p> |
||
1548 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
1549 | * |
||
1550 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1551 | */ |
||
1552 | 28 | View Code Duplication | public function replace(string $search, string $replacement, bool $caseSensitive = true): self |
1562 | |||
1563 | /** |
||
1564 | * Replaces all occurrences of $search in $str by $replacement. |
||
1565 | * |
||
1566 | * @param array $search <p>The elements to search for.</p> |
||
1567 | * @param string|array $replacement <p>The string to replace with.</p> |
||
1568 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
1569 | * |
||
1570 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1571 | */ |
||
1572 | 30 | View Code Duplication | public function replaceAll(array $search, $replacement, bool $caseSensitive = true): self |
1582 | |||
1583 | /** |
||
1584 | * Replaces all occurrences of $search from the beginning of string with $replacement. |
||
1585 | * |
||
1586 | * @param string $search <p>The string to search for.</p> |
||
1587 | * @param string $replacement <p>The replacement.</p> |
||
1588 | * |
||
1589 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1590 | */ |
||
1591 | public function replaceBeginning(string $search, string $replacement): self |
||
1597 | |||
1598 | /** |
||
1599 | * Replaces all occurrences of $search from the ending of string with $replacement. |
||
1600 | * |
||
1601 | * @param string $search <p>The string to search for.</p> |
||
1602 | * @param string $replacement <p>The replacement.</p> |
||
1603 | * |
||
1604 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1605 | */ |
||
1606 | public function replaceEnding(string $search, string $replacement): self |
||
1613 | |||
1614 | /** |
||
1615 | * Returns a reversed string. A multibyte version of strrev(). |
||
1616 | * |
||
1617 | * @return static <p>Object with a reversed $str.</p> |
||
1618 | */ |
||
1619 | 5 | public function reverse(): self |
|
1625 | |||
1626 | /** |
||
1627 | * Truncates the string to a given length, while ensuring that it does not |
||
1628 | * split words. If $substring is provided, and truncating occurs, the |
||
1629 | * string is further truncated so that the substring may be appended without |
||
1630 | * exceeding the desired length. |
||
1631 | * |
||
1632 | * @param int $length <p>Desired length of the truncated string.</p> |
||
1633 | * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p> |
||
1634 | * |
||
1635 | * @return static <p>Object with the resulting $str after truncating.</p> |
||
1636 | */ |
||
1637 | public function safeTruncate(int $length, string $substring = ''): self |
||
1643 | |||
1644 | /** |
||
1645 | * Shorten the string after $length, but also after the next word. |
||
1646 | * |
||
1647 | * @param int $length |
||
1648 | * @param string $strAddOn [optional] <p>Default: '…'</p> |
||
1649 | * |
||
1650 | * @return static |
||
1651 | */ |
||
1652 | 4 | public function shortenAfterWord(int $length, string $strAddOn = '…'): self |
|
1658 | |||
1659 | /** |
||
1660 | * A multibyte string shuffle function. It returns a string with its |
||
1661 | * characters in random order. |
||
1662 | * |
||
1663 | * @return static <p>Object with a shuffled $str.</p> |
||
1664 | */ |
||
1665 | 3 | public function shuffle(): self |
|
1671 | |||
1672 | /** |
||
1673 | * Returns the substring beginning at $start, and up to, but not including |
||
1674 | * the index specified by $end. If $end is omitted, the function extracts |
||
1675 | * the remaining string. If $end is negative, it is computed from the end |
||
1676 | * of the string. |
||
1677 | * |
||
1678 | * @param int $start <p>Initial index from which to begin extraction.</p> |
||
1679 | * @param int $end [optional] <p>Index at which to end extraction. Default: null</p> |
||
1680 | * |
||
1681 | * @return static <p>Object with its $str being the extracted substring.</p> |
||
1682 | */ |
||
1683 | public function slice(int $start, int $end = null): self |
||
1689 | |||
1690 | /** |
||
1691 | * Converts the string into an URL slug. This includes replacing non-ASCII |
||
1692 | * characters with their closest ASCII equivalents, removing remaining |
||
1693 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
||
1694 | * $replacement. The replacement defaults to a single dash, and the string |
||
1695 | * is also converted to lowercase. |
||
1696 | * |
||
1697 | * @param string $replacement [optional] <p>The string used to replace whitespace. Default: '-'</p> |
||
1698 | * @param string $language [optional] <p>The language for the url. Default: 'de'</p> |
||
1699 | * @param bool $strToLower [optional] <p>string to lower. Default: true</p> |
||
1700 | * |
||
1701 | * @return static <p>Object whose $str has been converted to an URL slug.</p> |
||
1702 | */ |
||
1703 | 15 | public function slugify(string $replacement = '-', string $language = 'de', bool $strToLower = true): self |
|
1709 | |||
1710 | /** |
||
1711 | * Convert a string to e.g.: "snake_case" |
||
1712 | * |
||
1713 | * @return static <p>Object with $str in snake_case.</p> |
||
1714 | */ |
||
1715 | public function snakeize(): self |
||
1721 | |||
1722 | /** |
||
1723 | * Splits the string with the provided regular expression, returning an |
||
1724 | * array of Stringy objects. An optional integer $limit will truncate the |
||
1725 | * results. |
||
1726 | * |
||
1727 | * @param string $pattern <p>The regex with which to split the string.</p> |
||
1728 | * @param int $limit [optional] <p>Maximum number of results to return. Default: -1 === no limit</p> |
||
1729 | * |
||
1730 | * @return static[] <p>An array of Stringy objects.</p> |
||
1731 | */ |
||
1732 | public function split(string $pattern, int $limit = -1): array |
||
1742 | |||
1743 | /** |
||
1744 | * Returns true if the string begins with $substring, false otherwise. By |
||
1745 | * default, the comparison is case-sensitive, but can be made insensitive |
||
1746 | * by setting $caseSensitive to false. |
||
1747 | * |
||
1748 | * @param string $substring <p>The substring to look for.</p> |
||
1749 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
1750 | * |
||
1751 | * @return bool <p>Whether or not $str starts with $substring.</p> |
||
1752 | */ |
||
1753 | 11 | public function startsWith(string $substring, bool $caseSensitive = true): bool |
|
1761 | |||
1762 | /** |
||
1763 | * Returns true if the string begins with any of $substrings, false otherwise. |
||
1764 | * By default the comparison is case-sensitive, but can be made insensitive by |
||
1765 | * setting $caseSensitive to false. |
||
1766 | * |
||
1767 | * @param array $substrings <p>Substrings to look for.</p> |
||
1768 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
1769 | * |
||
1770 | * @return bool <p>Whether or not $str starts with $substring.</p> |
||
1771 | */ |
||
1772 | public function startsWithAny(array $substrings, bool $caseSensitive = true): bool |
||
1780 | |||
1781 | /** |
||
1782 | * Strip all whitespace characters. This includes tabs and newline characters, |
||
1783 | * as well as multibyte whitespace such as the thin space and ideographic space. |
||
1784 | * |
||
1785 | * @return static |
||
1786 | */ |
||
1787 | 12 | public function stripWhitespace(): self |
|
1793 | |||
1794 | /** |
||
1795 | * Remove css media-queries. |
||
1796 | * |
||
1797 | * @return static |
||
1798 | */ |
||
1799 | public function stripeCssMediaQueries(): self |
||
1805 | |||
1806 | /** |
||
1807 | * Remove empty html-tag. |
||
1808 | * |
||
1809 | * e.g.: <tag></tag> |
||
1810 | * |
||
1811 | * @return static |
||
1812 | */ |
||
1813 | public function stripeEmptyHtmlTags(): self |
||
1819 | |||
1820 | /** |
||
1821 | * Returns the substring beginning at $start with the specified $length. |
||
1822 | * It differs from the UTF8::substr() function in that providing a $length of |
||
1823 | * null will return the rest of the string, rather than an empty string. |
||
1824 | * |
||
1825 | * @param int $start <p>Position of the first character to use.</p> |
||
1826 | * @param int $length [optional] <p>Maximum number of characters used. Default: null</p> |
||
1827 | * |
||
1828 | * @return static <p>Object with its $str being the substring.</p> |
||
1829 | */ |
||
1830 | 9 | public function substr(int $start, int $length = null): self |
|
1841 | |||
1842 | /** |
||
1843 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". |
||
1844 | * If no match is found returns new empty Stringy object. |
||
1845 | * |
||
1846 | * @param string $needle <p>The string to look for.</p> |
||
1847 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1848 | * |
||
1849 | * @return static |
||
1850 | */ |
||
1851 | public function substringOf(string $needle, bool $beforeNeedle = false): self |
||
1857 | |||
1858 | /** |
||
1859 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". |
||
1860 | * If no match is found returns new empty Stringy object. |
||
1861 | * |
||
1862 | * @param string $needle <p>The string to look for.</p> |
||
1863 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1864 | * |
||
1865 | * @return static |
||
1866 | */ |
||
1867 | public function substringOfIgnoreCase(string $needle, bool $beforeNeedle = false): self |
||
1873 | |||
1874 | /** |
||
1875 | * Surrounds $str with the given substring. |
||
1876 | * |
||
1877 | * @param string $substring <p>The substring to add to both sides.</P> |
||
1878 | * |
||
1879 | * @return static <p>Object whose $str had the substring both prepended and appended.</p> |
||
1880 | */ |
||
1881 | public function surround(string $substring): self |
||
1887 | |||
1888 | /** |
||
1889 | * Returns a case swapped version of the string. |
||
1890 | * |
||
1891 | * @return static <p>Object whose $str has each character's case swapped.</P> |
||
1892 | */ |
||
1893 | 4 | public function swapCase(): self |
|
1899 | |||
1900 | /** |
||
1901 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
||
1902 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
||
1903 | * equivalents. |
||
1904 | * |
||
1905 | * @return static <p>Object whose $str has those characters removed.</p> |
||
1906 | */ |
||
1907 | 4 | public function tidy(): self |
|
1913 | |||
1914 | /** |
||
1915 | * Returns a trimmed string with the first letter of each word capitalized. |
||
1916 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
1917 | * capitalized. |
||
1918 | * |
||
1919 | * @param array|null $ignore [optional] <p>An array of words not to capitalize or null. Default: null</p> |
||
1920 | * |
||
1921 | * @return static <p>Object with a titleized $str.</p> |
||
1922 | */ |
||
1923 | public function titleize(array $ignore = null): self |
||
1929 | |||
1930 | /** |
||
1931 | * Returns a trimmed string in proper title case. |
||
1932 | * |
||
1933 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
1934 | * capitalized. |
||
1935 | * |
||
1936 | * Adapted from John Gruber's script. |
||
1937 | * |
||
1938 | * @see https://gist.github.com/gruber/9f9e8650d68b13ce4d78 |
||
1939 | * |
||
1940 | * @param array $ignore <p>An array of words not to capitalize.</p> |
||
1941 | * |
||
1942 | * @return static <p>Object with a titleized $str</p> |
||
1943 | */ |
||
1944 | public function titleizeForHumans(array $ignore = []): self |
||
1950 | |||
1951 | /** |
||
1952 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
||
1953 | * replaced with their closest ASCII counterparts, and the rest are removed |
||
1954 | * unless instructed otherwise. |
||
1955 | * |
||
1956 | * @param bool $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance | |
||
1957 | * Default: false</p> |
||
1958 | * |
||
1959 | * @return static <p>Object whose $str contains only ASCII characters.</p> |
||
1960 | */ |
||
1961 | 16 | public function toAscii(bool $strict = false): self |
|
1967 | |||
1968 | /** |
||
1969 | * Returns a boolean representation of the given logical string value. |
||
1970 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
1971 | * 'off', and 'no' will return false. In all instances, case is ignored. |
||
1972 | * For other numeric strings, their sign will determine the return value. |
||
1973 | * In addition, blank strings consisting of only whitespace will return |
||
1974 | * false. For all other strings, the return value is a result of a |
||
1975 | * boolean cast. |
||
1976 | * |
||
1977 | * @return bool <p>A boolean value for the string.</p> |
||
1978 | */ |
||
1979 | public function toBoolean(): bool |
||
1983 | |||
1984 | /** |
||
1985 | * Converts all characters in the string to lowercase. |
||
1986 | * |
||
1987 | * @return static <p>Object with all characters of $str being lowercase.</p> |
||
1988 | */ |
||
1989 | 7 | public function toLowerCase(): self |
|
1995 | |||
1996 | /** |
||
1997 | * Converts each tab in the string to some number of spaces, as defined by |
||
1998 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
||
1999 | * |
||
2000 | * @param int $tabLength [optional] <p>Number of spaces to replace each tab with. Default: 4</p> |
||
2001 | * |
||
2002 | * @return static <p>Object whose $str has had tabs switched to spaces.</p> |
||
2003 | */ |
||
2004 | public function toSpaces(int $tabLength = 4): self |
||
2010 | |||
2011 | /** |
||
2012 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
||
2013 | * |
||
2014 | * @return string |
||
2015 | */ |
||
2016 | 271 | public function toString(): string |
|
2020 | |||
2021 | /** |
||
2022 | * Converts each occurrence of some consecutive number of spaces, as |
||
2023 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
||
2024 | * are converted to a tab. |
||
2025 | * |
||
2026 | * @param int $tabLength [optional] <p>Number of spaces to replace with a tab. Default: 4</p> |
||
2027 | * |
||
2028 | * @return static <p>Object whose $str has had spaces switched to tabs.</p> |
||
2029 | */ |
||
2030 | public function toTabs(int $tabLength = 4): self |
||
2036 | |||
2037 | /** |
||
2038 | * Converts the first character of each word in the string to uppercase |
||
2039 | * and all other chars to lowercase. |
||
2040 | * |
||
2041 | * @return static <p>Object with all characters of $str being title-cased.</p> |
||
2042 | */ |
||
2043 | public function toTitleCase(): self |
||
2049 | |||
2050 | /** |
||
2051 | * Converts all characters in the string to uppercase. |
||
2052 | * |
||
2053 | * @return static <p>Object with all characters of $str being uppercase.</p> |
||
2054 | */ |
||
2055 | 5 | public function toUpperCase(): self |
|
2061 | |||
2062 | /** |
||
2063 | * Returns a string with whitespace removed from the start and end of the |
||
2064 | * string. Supports the removal of unicode whitespace. Accepts an optional |
||
2065 | * string of characters to strip instead of the defaults. |
||
2066 | * |
||
2067 | * @param string $chars [optional] <p>String of characters to strip. Default: null</p> |
||
2068 | * |
||
2069 | * @return static <p>Object with a trimmed $str.</p> |
||
2070 | */ |
||
2071 | 12 | public function trim(string $chars = null): self |
|
2077 | |||
2078 | /** |
||
2079 | * Returns a string with whitespace removed from the start of the string. |
||
2080 | * Supports the removal of unicode whitespace. Accepts an optional |
||
2081 | * string of characters to strip instead of the defaults. |
||
2082 | * |
||
2083 | * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p> |
||
2084 | * |
||
2085 | * @return static <p>Object with a trimmed $str.</p> |
||
2086 | */ |
||
2087 | 13 | public function trimLeft(string $chars = null): self |
|
2093 | |||
2094 | /** |
||
2095 | * Returns a string with whitespace removed from the end of the string. |
||
2096 | * Supports the removal of unicode whitespace. Accepts an optional |
||
2097 | * string of characters to strip instead of the defaults. |
||
2098 | * |
||
2099 | * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p> |
||
2100 | * |
||
2101 | * @return static <p>Object with a trimmed $str.</p> |
||
2102 | */ |
||
2103 | 13 | public function trimRight(string $chars = null): self |
|
2109 | |||
2110 | /** |
||
2111 | * Truncates the string to a given length. If $substring is provided, and |
||
2112 | * truncating occurs, the string is further truncated so that the substring |
||
2113 | * may be appended without exceeding the desired length. |
||
2114 | * |
||
2115 | * @param int $length <p>Desired length of the truncated string.</p> |
||
2116 | * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p> |
||
2117 | * |
||
2118 | * @return static <p>Object with the resulting $str after truncating.</p> |
||
2119 | */ |
||
2120 | public function truncate(int $length, string $substring = ''): self |
||
2126 | |||
2127 | /** |
||
2128 | * Returns a lowercase and trimmed string separated by underscores. |
||
2129 | * Underscores are inserted before uppercase characters (with the exception |
||
2130 | * of the first character of the string), and in place of spaces as well as |
||
2131 | * dashes. |
||
2132 | * |
||
2133 | * @return static <p>Object with an underscored $str.</p> |
||
2134 | */ |
||
2135 | public function underscored(): self |
||
2139 | |||
2140 | /** |
||
2141 | * Returns an UpperCamelCase version of the supplied string. It trims |
||
2142 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
||
2143 | * and underscores, and removes spaces, dashes, underscores. |
||
2144 | * |
||
2145 | * @return static <p>Object with $str in UpperCamelCase.</p> |
||
2146 | */ |
||
2147 | public function upperCamelize(): self |
||
2153 | |||
2154 | /** |
||
2155 | * Converts the first character of the supplied string to upper case. |
||
2156 | * |
||
2157 | * @return static <p>Object with the first character of $str being upper case.</p> |
||
2158 | */ |
||
2159 | 5 | public function upperCaseFirst(): self |
|
2165 | |||
2166 | /** |
||
2167 | * Converts the string into an valid UTF-8 string. |
||
2168 | * |
||
2169 | * @return static |
||
2170 | */ |
||
2171 | 1 | public function utf8ify(): self |
|
2177 | } |
||
2178 |
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: