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 | 1152 | public function __construct($str = '', string $encoding = null) |
|
48 | { |
||
49 | 1152 | if (\is_array($str)) { |
|
50 | 1 | throw new \InvalidArgumentException( |
|
51 | 1 | 'Passed value cannot be an array' |
|
52 | ); |
||
53 | } |
||
54 | |||
55 | if ( |
||
56 | 1151 | \is_object($str) |
|
57 | && |
||
58 | 1151 | !\method_exists($str, '__toString') |
|
59 | ) { |
||
60 | 1 | throw new \InvalidArgumentException( |
|
61 | 1 | 'Passed object must have a __toString method' |
|
62 | ); |
||
63 | } |
||
64 | |||
65 | 1150 | $this->str = (string)$str; |
|
66 | |||
67 | 1150 | if ($encoding) { |
|
|
|||
68 | 1143 | $this->encoding = UTF8::normalize_encoding($encoding); |
|
69 | } else { |
||
70 | 10 | $this->encoding = 'UTF-8'; |
|
71 | } |
||
72 | 1150 | } |
|
73 | |||
74 | /** |
||
75 | * Returns the value in $str. |
||
76 | * |
||
77 | * @return string <p>The current value of the $str property.</p> |
||
78 | */ |
||
79 | 221 | public function __toString() |
|
80 | { |
||
81 | 221 | return (string)$this->str; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Returns a new string with $string appended. |
||
86 | * |
||
87 | * @param string $string <p>The string to append.</p> |
||
88 | * |
||
89 | * @return static <p>Object with appended $string.</p> |
||
90 | */ |
||
91 | 5 | public function append(string $string): self |
|
92 | { |
||
93 | 5 | return static::create($this->str . $string, $this->encoding); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Append an password (limited to chars that are good readable). |
||
98 | * |
||
99 | * @param int $length <p>Length of the random string.</p> |
||
100 | * |
||
101 | * @return static <p>Object with appended password.</p> |
||
102 | */ |
||
103 | 1 | public function appendPassword(int $length): self |
|
104 | { |
||
105 | 1 | $possibleChars = '2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ'; |
|
106 | |||
107 | 1 | return $this->appendRandomString($length, $possibleChars); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Append an unique identifier. |
||
112 | * |
||
113 | * @param string|int $entropyExtra [optional] <p>Extra entropy via a string or int value.</p> |
||
114 | * @param bool $md5 [optional] <p>Return the unique identifier as md5-hash? Default: true</p> |
||
115 | * |
||
116 | * @return static <p>Object with appended unique identifier as md5-hash.</p> |
||
117 | */ |
||
118 | 1 | public function appendUniqueIdentifier($entropyExtra = '', bool $md5 = true): self |
|
119 | { |
||
120 | 1 | $uniqueHelper = \mt_rand() . |
|
121 | 1 | \session_id() . |
|
122 | 1 | ($_SERVER['REMOTE_ADDR'] ?? '') . |
|
123 | 1 | ($_SERVER['SERVER_ADDR'] ?? '') . |
|
124 | 1 | $entropyExtra; |
|
125 | |||
126 | 1 | $uniqueString = \uniqid($uniqueHelper, true); |
|
127 | |||
128 | 1 | if ($md5) { |
|
129 | 1 | $uniqueString = \md5($uniqueString . $uniqueHelper); |
|
130 | } |
||
131 | |||
132 | 1 | return $this->append($uniqueString); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Append an random string. |
||
137 | * |
||
138 | * @param int $length <p>Length of the random string.</p> |
||
139 | * @param string $possibleChars [optional] <p>Characters string for the random selection.</p> |
||
140 | * |
||
141 | * @return static <p>Object with appended random string.</p> |
||
142 | */ |
||
143 | 2 | public function appendRandomString(int $length, string $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'): self |
|
144 | { |
||
145 | // init |
||
146 | 2 | $i = 0; |
|
147 | 2 | $str = $this->str; |
|
148 | 2 | $maxlength = UTF8::strlen($possibleChars, $this->encoding); |
|
149 | |||
150 | 2 | if ($maxlength === 0) { |
|
151 | 1 | return $this; |
|
152 | } |
||
153 | |||
154 | // add random chars |
||
155 | 2 | while ($i < $length) { |
|
156 | 2 | $char = UTF8::substr($possibleChars, \random_int(0, $maxlength - 1), 1, $this->encoding); |
|
157 | 2 | $str .= $char; |
|
158 | 2 | $i++; |
|
159 | } |
||
160 | |||
161 | 2 | return $this->append($str); |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Creates a Stringy object and assigns both str and encoding properties |
||
166 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
167 | * $encoding is not specified, it defaults to mb_internal_encoding(). It |
||
168 | * then returns the initialized object. Throws an InvalidArgumentException |
||
169 | * if the first argument is an array or object without a __toString method. |
||
170 | * |
||
171 | * @param mixed $str [optional] <p>Value to modify, after being cast to string. Default: ''</p> |
||
172 | * @param string $encoding [optional] <p>The character encoding. Fallback: 'UTF-8'</p> |
||
173 | * |
||
174 | * @return static <p>A Stringy object.</p> |
||
175 | * |
||
176 | * @throws \InvalidArgumentException <p>if an array or object without a |
||
177 | * __toString method is passed as the first argument</p> |
||
178 | */ |
||
179 | 1142 | public static function create($str = '', string $encoding = null): self |
|
180 | { |
||
181 | 1142 | if ($encoding) { |
|
182 | 898 | $encoding = UTF8::normalize_encoding($encoding); |
|
183 | } else { |
||
184 | 766 | $encoding = 'UTF-8'; |
|
185 | } |
||
186 | |||
187 | 1142 | return new static($str, $encoding); |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * Returns the substring between $start and $end, if found, or an empty |
||
192 | * string. An optional offset may be supplied from which to begin the |
||
193 | * search for the start string. |
||
194 | * |
||
195 | * @param string $start <p>Delimiter marking the start of the substring.</p> |
||
196 | * @param string $end <p>Delimiter marking the end of the substring.</p> |
||
197 | * @param int $offset [optional] <p>Index from which to begin the search. Default: 0</p> |
||
198 | * |
||
199 | * @return static <p>Object whose $str is a substring between $start and $end.</p> |
||
200 | */ |
||
201 | 16 | public function between(string $start, string $end, int $offset = 0): self |
|
202 | { |
||
203 | 16 | $startIndex = $this->indexOf($start, $offset); |
|
204 | 16 | if ($startIndex === false) { |
|
205 | 2 | return static::create('', $this->encoding); |
|
206 | } |
||
207 | |||
208 | 14 | $substrIndex = $startIndex + UTF8::strlen($start, $this->encoding); |
|
209 | 14 | $endIndex = $this->indexOf($end, $substrIndex); |
|
210 | 14 | if ($endIndex === false) { |
|
211 | 2 | return static::create('', $this->encoding); |
|
212 | } |
||
213 | |||
214 | 12 | return $this->substr($substrIndex, $endIndex - $substrIndex); |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * Returns the index of the first occurrence of $needle in the string, |
||
219 | * and false if not found. Accepts an optional offset from which to begin |
||
220 | * the search. |
||
221 | * |
||
222 | * @param string $needle <p>Substring to look for.</p> |
||
223 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> |
||
224 | * |
||
225 | * @return int|false <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> |
||
226 | */ |
||
227 | 29 | public function indexOf(string $needle, int $offset = 0) |
|
228 | { |
||
229 | 29 | return UTF8::strpos($this->str, $needle, $offset, $this->encoding); |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * Returns the index of the first occurrence of $needle in the string, |
||
234 | * and false if not found. Accepts an optional offset from which to begin |
||
235 | * the search. |
||
236 | * |
||
237 | * @param string $needle <p>Substring to look for.</p> |
||
238 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> |
||
239 | * |
||
240 | * @return int|false <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> |
||
241 | */ |
||
242 | 2 | public function indexOfIgnoreCase(string $needle, int $offset = 0) |
|
246 | |||
247 | /** |
||
248 | * Returns the substring beginning at $start with the specified $length. |
||
249 | * It differs from the UTF8::substr() function in that providing a $length of |
||
250 | * null will return the rest of the string, rather than an empty string. |
||
251 | * |
||
252 | * @param int $start <p>Position of the first character to use.</p> |
||
253 | * @param int $length [optional] <p>Maximum number of characters used. Default: null</p> |
||
254 | * |
||
255 | * @return static <p>Object with its $str being the substring.</p> |
||
256 | */ |
||
257 | 65 | public function substr(int $start, int $length = null): self |
|
267 | |||
268 | /** |
||
269 | * Returns the length of the string. |
||
270 | * |
||
271 | * @return int <p>The number of characters in $str given the encoding.</p> |
||
272 | */ |
||
273 | 293 | public function length(): int |
|
277 | |||
278 | /** |
||
279 | * Trims the string and replaces consecutive whitespace characters with a |
||
280 | * single space. This includes tabs and newline characters, as well as |
||
281 | * multibyte whitespace such as the thin space and ideographic space. |
||
282 | * |
||
283 | * @return static <p>Object with a trimmed $str and condensed whitespace.</p> |
||
284 | */ |
||
285 | 52 | public function collapseWhitespace(): self |
|
289 | |||
290 | /** |
||
291 | * Returns a string with whitespace removed from the start and end of the |
||
292 | * string. Supports the removal of unicode whitespace. Accepts an optional |
||
293 | * string of characters to strip instead of the defaults. |
||
294 | * |
||
295 | * @param string $chars [optional] <p>String of characters to strip. Default: null</p> |
||
296 | * |
||
297 | * @return static <p>Object with a trimmed $str.</p> |
||
298 | */ |
||
299 | 188 | View Code Duplication | public function trim(string $chars = null): self |
309 | |||
310 | /** |
||
311 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
312 | * |
||
313 | * @param string $pattern <p>The regular expression pattern.</p> |
||
314 | * @param string $replacement <p>The string to replace with.</p> |
||
315 | * @param string $options [optional] <p>Matching conditions to be used.</p> |
||
316 | * @param string $delimiter [optional] <p>Delimiter the the regex. Default: '/'</p> |
||
317 | * |
||
318 | * @return static <p>Object with the result2ing $str after the replacements.</p> |
||
319 | */ |
||
320 | 259 | public function regexReplace(string $pattern, string $replacement, string $options = '', string $delimiter = '/'): self |
|
339 | |||
340 | /** |
||
341 | * Returns true if the string contains all $needles, false otherwise. By |
||
342 | * default the comparison is case-sensitive, but can be made insensitive by |
||
343 | * setting $caseSensitive to false. |
||
344 | * |
||
345 | * @param array $needles <p>SubStrings to look for.</p> |
||
346 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
347 | * |
||
348 | * @return bool <p>Whether or not $str contains $needle.</p> |
||
349 | */ |
||
350 | 43 | View Code Duplication | public function containsAll(array $needles, bool $caseSensitive = true): bool |
365 | |||
366 | /** |
||
367 | * Returns true if the string contains $needle, false otherwise. By default |
||
368 | * the comparison is case-sensitive, but can be made insensitive by setting |
||
369 | * $caseSensitive to false. |
||
370 | * |
||
371 | * @param string $needle <p>Substring to look for.</p> |
||
372 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
373 | * |
||
374 | * @return bool <p>Whether or not $str contains $needle.</p> |
||
375 | */ |
||
376 | 105 | public function contains(string $needle, bool $caseSensitive = true): bool |
|
386 | |||
387 | /** |
||
388 | * Returns true if the string contains any $needles, false otherwise. By |
||
389 | * default the comparison is case-sensitive, but can be made insensitive by |
||
390 | * setting $caseSensitive to false. |
||
391 | * |
||
392 | * @param array $needles <p>SubStrings to look for.</p> |
||
393 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
394 | * |
||
395 | * @return bool <p>Whether or not $str contains $needle.</p> |
||
396 | */ |
||
397 | 43 | View Code Duplication | public function containsAny(array $needles, bool $caseSensitive = true): bool |
412 | |||
413 | /** |
||
414 | * Returns the length of the string, implementing the countable interface. |
||
415 | * |
||
416 | * @return int <p>The number of characters in the string, given the encoding.</p> |
||
417 | */ |
||
418 | 1 | public function count(): int |
|
422 | |||
423 | /** |
||
424 | * Returns the number of occurrences of $substring in the given string. |
||
425 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
426 | * by setting $caseSensitive to false. |
||
427 | * |
||
428 | * @param string $substring <p>The substring to search for.</p> |
||
429 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
430 | * |
||
431 | * @return int|false <p>This functions returns an integer or false if there isn't a string.</p> |
||
432 | */ |
||
433 | 15 | public function countSubstr(string $substring, bool $caseSensitive = true) |
|
444 | |||
445 | /** |
||
446 | * Returns a lowercase and trimmed string separated by dashes. Dashes are |
||
447 | * inserted before uppercase characters (with the exception of the first |
||
448 | * character of the string), and in place of spaces as well as underscores. |
||
449 | * |
||
450 | * @return static <p>Object with a dasherized $str</p> |
||
451 | */ |
||
452 | 19 | public function dasherize(): self |
|
456 | |||
457 | /** |
||
458 | * Returns a lowercase and trimmed string separated by the given delimiter. |
||
459 | * Delimiters are inserted before uppercase characters (with the exception |
||
460 | * of the first character of the string), and in place of spaces, dashes, |
||
461 | * and underscores. Alpha delimiters are not converted to lowercase. |
||
462 | * |
||
463 | * @param string $delimiter <p>Sequence used to separate parts of the string.</p> |
||
464 | * |
||
465 | * @return static <p>Object with a delimited $str.</p> |
||
466 | */ |
||
467 | 49 | public function delimit(string $delimiter): self |
|
479 | |||
480 | /** |
||
481 | * Ensures that the string begins with $substring. If it doesn't, it's |
||
482 | * prepended. |
||
483 | * |
||
484 | * @param string $substring <p>The substring to add if not present.</p> |
||
485 | * |
||
486 | * @return static <p>Object with its $str prefixed by the $substring.</p> |
||
487 | */ |
||
488 | 10 | View Code Duplication | public function ensureLeft(string $substring): self |
498 | |||
499 | /** |
||
500 | * Returns true if the string begins with $substring, false otherwise. By |
||
501 | * default, the comparison is case-sensitive, but can be made insensitive |
||
502 | * by setting $caseSensitive to false. |
||
503 | * |
||
504 | * @param string $substring <p>The substring to look for.</p> |
||
505 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
506 | * |
||
507 | * @return bool <p>Whether or not $str starts with $substring.</p> |
||
508 | */ |
||
509 | 45 | public function startsWith(string $substring, bool $caseSensitive = true): bool |
|
520 | |||
521 | /** |
||
522 | * Returns true if the string begins with any of $substrings, false otherwise. |
||
523 | * By default the comparison is case-sensitive, but can be made insensitive by |
||
524 | * setting $caseSensitive to false. |
||
525 | * |
||
526 | * @param array $substrings <p>Substrings to look for.</p> |
||
527 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
528 | * |
||
529 | * @return bool <p>Whether or not $str starts with $substring.</p> |
||
530 | */ |
||
531 | 12 | View Code Duplication | public function startsWithAny(array $substrings, bool $caseSensitive = true): bool |
545 | |||
546 | /** |
||
547 | * Ensures that the string ends with $substring. If it doesn't, it's appended. |
||
548 | * |
||
549 | * @param string $substring <p>The substring to add if not present.</p> |
||
550 | * |
||
551 | * @return static <p>Object with its $str suffixed by the $substring.</p> |
||
552 | */ |
||
553 | 10 | View Code Duplication | public function ensureRight(string $substring): self |
563 | |||
564 | /** |
||
565 | * Returns true if the string ends with $substring, false otherwise. By |
||
566 | * default, the comparison is case-sensitive, but can be made insensitive |
||
567 | * by setting $caseSensitive to false. |
||
568 | * |
||
569 | * @param string $substring <p>The substring to look for.</p> |
||
570 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
571 | * |
||
572 | * @return bool <p>Whether or not $str ends with $substring.</p> |
||
573 | */ |
||
574 | 44 | public function endsWith(string $substring, bool $caseSensitive = true): bool |
|
593 | |||
594 | /** |
||
595 | * Returns true if the string ends with any of $substrings, false otherwise. |
||
596 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
597 | * by setting $caseSensitive to false. |
||
598 | * |
||
599 | * @param string[] $substrings <p>Substrings to look for.</p> |
||
600 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
601 | * |
||
602 | * @return bool <p>Whether or not $str ends with $substring.</p> |
||
603 | */ |
||
604 | 11 | View Code Duplication | public function endsWithAny(array $substrings, bool $caseSensitive = true): bool |
618 | |||
619 | /** |
||
620 | * Returns the first $n characters of the string. |
||
621 | * |
||
622 | * @param int $n <p>Number of characters to retrieve from the start.</p> |
||
623 | * |
||
624 | * @return static <p>Object with its $str being the first $n chars.</p> |
||
625 | */ |
||
626 | 13 | View Code Duplication | public function first(int $n): self |
638 | |||
639 | /** |
||
640 | * Returns the encoding used by the Stringy object. |
||
641 | * |
||
642 | * @return string <p>The current value of the $encoding property.</p> |
||
643 | */ |
||
644 | 3 | public function getEncoding(): string |
|
648 | |||
649 | /** |
||
650 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
651 | * interface. The ArrayIterator's constructor is passed an array of chars |
||
652 | * in the multibyte string. This enables the use of foreach with instances |
||
653 | * of Stringy\Stringy. |
||
654 | * |
||
655 | * @return \ArrayIterator <p>An iterator for the characters in the string.</p> |
||
656 | */ |
||
657 | 1 | public function getIterator(): \ArrayIterator |
|
661 | |||
662 | /** |
||
663 | * Returns an array consisting of the characters in the string. |
||
664 | * |
||
665 | * @return array <p>An array of string chars.</p> |
||
666 | */ |
||
667 | 4 | public function chars(): array |
|
679 | |||
680 | /** |
||
681 | * Returns the character at $index, with indexes starting at 0. |
||
682 | * |
||
683 | * @param int $index <p>Position of the character.</p> |
||
684 | * |
||
685 | * @return static <p>The character at $index.</p> |
||
686 | */ |
||
687 | 11 | public function at(int $index): self |
|
691 | |||
692 | /** |
||
693 | * Returns true if the string contains a lower case char, false otherwise. |
||
694 | * |
||
695 | * @return bool <p>Whether or not the string contains a lower case character.</p> |
||
696 | */ |
||
697 | 12 | public function hasLowerCase(): bool |
|
701 | |||
702 | /** |
||
703 | * Returns true if $str matches the supplied pattern, false otherwise. |
||
704 | * |
||
705 | * @param string $pattern <p>Regex pattern to match against.</p> |
||
706 | * |
||
707 | * @return bool <p>Whether or not $str matches the pattern.</p> |
||
708 | */ |
||
709 | 103 | protected function matchesPattern(string $pattern): bool |
|
717 | |||
718 | /** |
||
719 | * Returns true if the string contains an upper case char, false otherwise. |
||
720 | * |
||
721 | * @return bool <p>Whether or not the string contains an upper case character.</p> |
||
722 | */ |
||
723 | 12 | public function hasUpperCase(): bool |
|
727 | |||
728 | /** |
||
729 | * Convert all HTML entities to their applicable characters. |
||
730 | * |
||
731 | * @param int $flags [optional] <p> |
||
732 | * A bitmask of one or more of the following flags, which specify how to handle quotes and |
||
733 | * which document type to use. The default is ENT_COMPAT. |
||
734 | * <table> |
||
735 | * Available <i>flags</i> constants |
||
736 | * <tr valign="top"> |
||
737 | * <td>Constant Name</td> |
||
738 | * <td>Description</td> |
||
739 | * </tr> |
||
740 | * <tr valign="top"> |
||
741 | * <td><b>ENT_COMPAT</b></td> |
||
742 | * <td>Will convert double-quotes and leave single-quotes alone.</td> |
||
743 | * </tr> |
||
744 | * <tr valign="top"> |
||
745 | * <td><b>ENT_QUOTES</b></td> |
||
746 | * <td>Will convert both double and single quotes.</td> |
||
747 | * </tr> |
||
748 | * <tr valign="top"> |
||
749 | * <td><b>ENT_NOQUOTES</b></td> |
||
750 | * <td>Will leave both double and single quotes unconverted.</td> |
||
751 | * </tr> |
||
752 | * <tr valign="top"> |
||
753 | * <td><b>ENT_HTML401</b></td> |
||
754 | * <td> |
||
755 | * Handle code as HTML 4.01. |
||
756 | * </td> |
||
757 | * </tr> |
||
758 | * <tr valign="top"> |
||
759 | * <td><b>ENT_XML1</b></td> |
||
760 | * <td> |
||
761 | * Handle code as XML 1. |
||
762 | * </td> |
||
763 | * </tr> |
||
764 | * <tr valign="top"> |
||
765 | * <td><b>ENT_XHTML</b></td> |
||
766 | * <td> |
||
767 | * Handle code as XHTML. |
||
768 | * </td> |
||
769 | * </tr> |
||
770 | * <tr valign="top"> |
||
771 | * <td><b>ENT_HTML5</b></td> |
||
772 | * <td> |
||
773 | * Handle code as HTML 5. |
||
774 | * </td> |
||
775 | * </tr> |
||
776 | * </table> |
||
777 | * </p> |
||
778 | * |
||
779 | * @return static <p>Object with the resulting $str after being html decoded.</p> |
||
780 | */ |
||
781 | 5 | public function htmlDecode(int $flags = ENT_COMPAT): self |
|
787 | |||
788 | /** |
||
789 | * Convert all applicable characters to HTML entities. |
||
790 | * |
||
791 | * @param int $flags [optional] <p> |
||
792 | * A bitmask of one or more of the following flags, which specify how to handle quotes and |
||
793 | * which document type to use. The default is ENT_COMPAT. |
||
794 | * <table> |
||
795 | * Available <i>flags</i> constants |
||
796 | * <tr valign="top"> |
||
797 | * <td>Constant Name</td> |
||
798 | * <td>Description</td> |
||
799 | * </tr> |
||
800 | * <tr valign="top"> |
||
801 | * <td><b>ENT_COMPAT</b></td> |
||
802 | * <td>Will convert double-quotes and leave single-quotes alone.</td> |
||
803 | * </tr> |
||
804 | * <tr valign="top"> |
||
805 | * <td><b>ENT_QUOTES</b></td> |
||
806 | * <td>Will convert both double and single quotes.</td> |
||
807 | * </tr> |
||
808 | * <tr valign="top"> |
||
809 | * <td><b>ENT_NOQUOTES</b></td> |
||
810 | * <td>Will leave both double and single quotes unconverted.</td> |
||
811 | * </tr> |
||
812 | * <tr valign="top"> |
||
813 | * <td><b>ENT_HTML401</b></td> |
||
814 | * <td> |
||
815 | * Handle code as HTML 4.01. |
||
816 | * </td> |
||
817 | * </tr> |
||
818 | * <tr valign="top"> |
||
819 | * <td><b>ENT_XML1</b></td> |
||
820 | * <td> |
||
821 | * Handle code as XML 1. |
||
822 | * </td> |
||
823 | * </tr> |
||
824 | * <tr valign="top"> |
||
825 | * <td><b>ENT_XHTML</b></td> |
||
826 | * <td> |
||
827 | * Handle code as XHTML. |
||
828 | * </td> |
||
829 | * </tr> |
||
830 | * <tr valign="top"> |
||
831 | * <td><b>ENT_HTML5</b></td> |
||
832 | * <td> |
||
833 | * Handle code as HTML 5. |
||
834 | * </td> |
||
835 | * </tr> |
||
836 | * </table> |
||
837 | * </p> |
||
838 | * |
||
839 | * @return static <p>Object with the resulting $str after being html encoded.</p> |
||
840 | */ |
||
841 | 5 | public function htmlEncode(int $flags = ENT_COMPAT): self |
|
847 | |||
848 | /** |
||
849 | * Capitalizes the first word of the string, replaces underscores with |
||
850 | * spaces, and strips '_id'. |
||
851 | * |
||
852 | * @return static <p>Object with a humanized $str.</p> |
||
853 | */ |
||
854 | 3 | public function humanize(): self |
|
860 | |||
861 | /** |
||
862 | * Converts the first character of the supplied string to upper case. |
||
863 | * |
||
864 | * @return static <p>Object with the first character of $str being upper case.</p> |
||
865 | */ |
||
866 | 61 | View Code Duplication | public function upperCaseFirst(): self |
880 | |||
881 | /** |
||
882 | * Returns the index of the last occurrence of $needle in the string, |
||
883 | * and false if not found. Accepts an optional offset from which to begin |
||
884 | * the search. Offsets may be negative to count from the last character |
||
885 | * in the string. |
||
886 | * |
||
887 | * @param string $needle <p>Substring to look for.</p> |
||
888 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> |
||
889 | * |
||
890 | * @return int|false <p>The last occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> |
||
891 | */ |
||
892 | 12 | public function indexOfLast(string $needle, int $offset = 0) |
|
896 | |||
897 | /** |
||
898 | * Returns the index of the last occurrence of $needle in the string, |
||
899 | * and false if not found. Accepts an optional offset from which to begin |
||
900 | * the search. Offsets may be negative to count from the last character |
||
901 | * in the string. |
||
902 | * |
||
903 | * @param string $needle <p>Substring to look for.</p> |
||
904 | * @param int $offset [optional] <p>Offset from which to search. Default: 0</p> |
||
905 | * |
||
906 | * @return int|false <p>The last occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p> |
||
907 | */ |
||
908 | 2 | public function indexOfLastIgnoreCase(string $needle, int $offset = 0) |
|
912 | |||
913 | /** |
||
914 | * Inserts $substring into the string at the $index provided. |
||
915 | * |
||
916 | * @param string $substring <p>String to be inserted.</p> |
||
917 | * @param int $index <p>The index at which to insert the substring.</p> |
||
918 | * |
||
919 | * @return static <p>Object with the resulting $str after the insertion.</p> |
||
920 | */ |
||
921 | 8 | View Code Duplication | public function insert(string $substring, int $index): self |
935 | |||
936 | /** |
||
937 | * Returns true if the string contains the $pattern, otherwise false. |
||
938 | * |
||
939 | * WARNING: Asterisks ("*") are translated into (".*") zero-or-more regular |
||
940 | * expression wildcards. |
||
941 | * |
||
942 | * @credit Originally from Laravel, thanks Taylor. |
||
943 | * |
||
944 | * @param string $pattern <p>The string or pattern to match against.</p> |
||
945 | * |
||
946 | * @return bool <p>Whether or not we match the provided pattern.</p> |
||
947 | */ |
||
948 | 13 | public function is(string $pattern): bool |
|
959 | |||
960 | /** |
||
961 | * Returns true if the string contains only alphabetic chars, false otherwise. |
||
962 | * |
||
963 | * @return bool <p>Whether or not $str contains only alphabetic chars.</p> |
||
964 | */ |
||
965 | 10 | public function isAlpha(): bool |
|
969 | |||
970 | /** |
||
971 | * Determine whether the string is considered to be empty. |
||
972 | * |
||
973 | * A variable is considered empty if it does not exist or if its value equals FALSE. |
||
974 | * empty() does not generate a warning if the variable does not exist. |
||
975 | * |
||
976 | * @return bool <p>Whether or not $str is empty().</p> |
||
977 | */ |
||
978 | public function isEmpty(): bool |
||
982 | |||
983 | /** |
||
984 | * Returns true if the string contains only alphabetic and numeric chars, false otherwise. |
||
985 | * |
||
986 | * @return bool <p>Whether or not $str contains only alphanumeric chars.</p> |
||
987 | */ |
||
988 | 13 | public function isAlphanumeric(): bool |
|
992 | |||
993 | /** |
||
994 | * Returns true if the string contains only whitespace chars, false otherwise. |
||
995 | * |
||
996 | * @return bool <p>Whether or not $str contains only whitespace characters.</p> |
||
997 | */ |
||
998 | 15 | public function isBlank(): bool |
|
1002 | |||
1003 | /** |
||
1004 | * Returns true if the string contains only hexadecimal chars, false otherwise. |
||
1005 | * |
||
1006 | * @return bool <p>Whether or not $str contains only hexadecimal chars.</p> |
||
1007 | */ |
||
1008 | 13 | public function isHexadecimal(): bool |
|
1012 | |||
1013 | /** |
||
1014 | * Returns true if the string contains HTML-Tags, false otherwise. |
||
1015 | * |
||
1016 | * @return bool <p>Whether or not $str contains HTML-Tags.</p> |
||
1017 | */ |
||
1018 | 1 | public function isHtml(): bool |
|
1022 | |||
1023 | /** |
||
1024 | * Returns true if the string contains a valid E-Mail address, false otherwise. |
||
1025 | * |
||
1026 | * @param bool $useExampleDomainCheck [optional] <p>Default: false</p> |
||
1027 | * @param bool $useTypoInDomainCheck [optional] <p>Default: false</p> |
||
1028 | * @param bool $useTemporaryDomainCheck [optional] <p>Default: false</p> |
||
1029 | * @param bool $useDnsCheck [optional] <p>Default: false</p> |
||
1030 | * |
||
1031 | * @return bool <p>Whether or not $str contains a valid E-Mail address.</p> |
||
1032 | */ |
||
1033 | 1 | public function isEmail(bool $useExampleDomainCheck = false, bool $useTypoInDomainCheck = false, bool $useTemporaryDomainCheck = false, bool $useDnsCheck = false): bool |
|
1037 | |||
1038 | /** |
||
1039 | * Returns true if the string is JSON, false otherwise. Unlike json_decode |
||
1040 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
||
1041 | * in that an empty string is not considered valid JSON. |
||
1042 | * |
||
1043 | * @return bool <p>Whether or not $str is JSON.</p> |
||
1044 | */ |
||
1045 | 20 | public function isJson(): bool |
|
1055 | |||
1056 | /** |
||
1057 | * Returns true if the string contains only lower case chars, false otherwise. |
||
1058 | * |
||
1059 | * @return bool <p>Whether or not $str contains only lower case characters.</p> |
||
1060 | */ |
||
1061 | 8 | public function isLowerCase(): bool |
|
1069 | |||
1070 | /** |
||
1071 | * Returns true if the string is serialized, false otherwise. |
||
1072 | * |
||
1073 | * @return bool <p>Whether or not $str is serialized.</p> |
||
1074 | */ |
||
1075 | 7 | public function isSerialized(): bool |
|
1087 | |||
1088 | /** |
||
1089 | * Returns true if the string contains only lower case chars, false |
||
1090 | * otherwise. |
||
1091 | * |
||
1092 | * @return bool <p>Whether or not $str contains only lower case characters.</p> |
||
1093 | */ |
||
1094 | 8 | public function isUpperCase(): bool |
|
1098 | |||
1099 | /** |
||
1100 | * Returns the last $n characters of the string. |
||
1101 | * |
||
1102 | * @param int $n <p>Number of characters to retrieve from the end.</p> |
||
1103 | * |
||
1104 | * @return static <p>Object with its $str being the last $n chars.</p> |
||
1105 | */ |
||
1106 | 12 | View Code Duplication | public function last(int $n): self |
1118 | |||
1119 | /** |
||
1120 | * Splits on newlines and carriage returns, returning an array of Stringy |
||
1121 | * objects corresponding to the lines in the string. |
||
1122 | * |
||
1123 | * @return static[] <p>An array of Stringy objects.</p> |
||
1124 | */ |
||
1125 | 15 | public function lines(): array |
|
1136 | |||
1137 | /** |
||
1138 | * Returns the longest common prefix between the string and $otherStr. |
||
1139 | * |
||
1140 | * @param string $otherStr <p>Second string for comparison.</p> |
||
1141 | * |
||
1142 | * @return static <p>Object with its $str being the longest common prefix.</p> |
||
1143 | */ |
||
1144 | 10 | public function longestCommonPrefix(string $otherStr): self |
|
1162 | |||
1163 | /** |
||
1164 | * Returns the longest common suffix between the string and $otherStr. |
||
1165 | * |
||
1166 | * @param string $otherStr <p>Second string for comparison.</p> |
||
1167 | * |
||
1168 | * @return static <p>Object with its $str being the longest common suffix.</p> |
||
1169 | */ |
||
1170 | 10 | public function longestCommonSuffix(string $otherStr): self |
|
1188 | |||
1189 | /** |
||
1190 | * Returns the longest common substring between the string and $otherStr. |
||
1191 | * In the case of ties, it returns that which occurs first. |
||
1192 | * |
||
1193 | * @param string $otherStr <p>Second string for comparison.</p> |
||
1194 | * |
||
1195 | * @return static <p>Object with its $str being the longest common substring.</p> |
||
1196 | */ |
||
1197 | 10 | public function longestCommonSubstring(string $otherStr): self |
|
1242 | |||
1243 | /** |
||
1244 | * Returns whether or not a character exists at an index. Offsets may be |
||
1245 | * negative to count from the last character in the string. Implements |
||
1246 | * part of the ArrayAccess interface. |
||
1247 | * |
||
1248 | * @param int $offset <p>The index to check.</p> |
||
1249 | * |
||
1250 | * @return boolean <p>Whether or not the index exists.</p> |
||
1251 | */ |
||
1252 | 6 | public function offsetExists($offset): bool |
|
1264 | |||
1265 | /** |
||
1266 | * Returns the character at the given index. Offsets may be negative to |
||
1267 | * count from the last character in the string. Implements part of the |
||
1268 | * ArrayAccess interface, and throws an OutOfBoundsException if the index |
||
1269 | * does not exist. |
||
1270 | * |
||
1271 | * @param int $offset <p>The <strong>index</strong> from which to retrieve the char.</p> |
||
1272 | * |
||
1273 | * @return string <p>The character at the specified index.</p> |
||
1274 | * |
||
1275 | * @throws \OutOfBoundsException <p>If the positive or negative offset does not exist.</p> |
||
1276 | */ |
||
1277 | 2 | public function offsetGet($offset): string |
|
1293 | |||
1294 | /** |
||
1295 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1296 | * when called. This maintains the immutability of Stringy objects. |
||
1297 | * |
||
1298 | * @param int $offset <p>The index of the character.</p> |
||
1299 | * @param mixed $value <p>Value to set.</p> |
||
1300 | * |
||
1301 | * @throws \Exception <p>When called.</p> |
||
1302 | */ |
||
1303 | 1 | public function offsetSet($offset, $value) |
|
1309 | |||
1310 | /** |
||
1311 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1312 | * when called. This maintains the immutability of Stringy objects. |
||
1313 | * |
||
1314 | * @param int $offset <p>The index of the character.</p> |
||
1315 | * |
||
1316 | * @throws \Exception <p>When called.</p> |
||
1317 | */ |
||
1318 | 1 | public function offsetUnset($offset) |
|
1324 | |||
1325 | /** |
||
1326 | * Pads the string to a given length with $padStr. If length is less than |
||
1327 | * or equal to the length of the string, no padding takes places. The |
||
1328 | * default string used for padding is a space, and the default type (one of |
||
1329 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
||
1330 | * if $padType isn't one of those 3 values. |
||
1331 | * |
||
1332 | * @param int $length <p>Desired string length after padding.</p> |
||
1333 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1334 | * @param string $padType [optional] <p>One of 'left', 'right', 'both'. Default: 'right'</p> |
||
1335 | * |
||
1336 | * @return static <p>Object with a padded $str.</p> |
||
1337 | * |
||
1338 | * @throws \InvalidArgumentException <p>If $padType isn't one of 'right', 'left' or 'both'.</p> |
||
1339 | */ |
||
1340 | 13 | public function pad(int $length, string $padStr = ' ', string $padType = 'right'): self |
|
1357 | |||
1358 | /** |
||
1359 | * Returns a new string of a given length such that the beginning of the |
||
1360 | * string is padded. Alias for pad() with a $padType of 'left'. |
||
1361 | * |
||
1362 | * @param int $length <p>Desired string length after padding.</p> |
||
1363 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1364 | * |
||
1365 | * @return static <p>String with left padding.</p> |
||
1366 | */ |
||
1367 | 10 | public function padLeft(int $length, string $padStr = ' '): self |
|
1371 | |||
1372 | /** |
||
1373 | * Adds the specified amount of left and right padding to the given string. |
||
1374 | * The default character used is a space. |
||
1375 | * |
||
1376 | * @param int $left [optional] <p>Length of left padding. Default: 0</p> |
||
1377 | * @param int $right [optional] <p>Length of right padding. Default: 0</p> |
||
1378 | * @param string $padStr [optional] <p>String used to pad. Default: ' '</p> |
||
1379 | * |
||
1380 | * @return static <p>String with padding applied.</p> |
||
1381 | */ |
||
1382 | 37 | protected function applyPadding(int $left = 0, int $right = 0, string $padStr = ' '): self |
|
1419 | |||
1420 | /** |
||
1421 | * Returns a new string of a given length such that the end of the string |
||
1422 | * is padded. Alias for pad() with a $padType of 'right'. |
||
1423 | * |
||
1424 | * @param int $length <p>Desired string length after padding.</p> |
||
1425 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1426 | * |
||
1427 | * @return static <p>String with right padding.</p> |
||
1428 | */ |
||
1429 | 13 | public function padRight(int $length, string $padStr = ' '): self |
|
1433 | |||
1434 | /** |
||
1435 | * Returns a new string of a given length such that both sides of the |
||
1436 | * string are padded. Alias for pad() with a $padType of 'both'. |
||
1437 | * |
||
1438 | * @param int $length <p>Desired string length after padding.</p> |
||
1439 | * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p> |
||
1440 | * |
||
1441 | * @return static <p>String with padding applied.</p> |
||
1442 | */ |
||
1443 | 14 | public function padBoth(int $length, string $padStr = ' '): self |
|
1449 | |||
1450 | /** |
||
1451 | * Returns a new string starting with $string. |
||
1452 | * |
||
1453 | * @param string $string <p>The string to append.</p> |
||
1454 | * |
||
1455 | * @return static <p>Object with appended $string.</p> |
||
1456 | */ |
||
1457 | 2 | public function prepend(string $string): self |
|
1461 | |||
1462 | /** |
||
1463 | * Returns a new string with the prefix $substring removed, if present. |
||
1464 | * |
||
1465 | * @param string $substring <p>The prefix to remove.</p> |
||
1466 | * |
||
1467 | * @return static <p>Object having a $str without the prefix $substring.</p> |
||
1468 | */ |
||
1469 | 12 | View Code Duplication | public function removeLeft(string $substring): self |
1481 | |||
1482 | /** |
||
1483 | * Returns a new string with the suffix $substring removed, if present. |
||
1484 | * |
||
1485 | * @param string $substring <p>The suffix to remove.</p> |
||
1486 | * |
||
1487 | * @return static <p>Object having a $str without the suffix $substring.</p> |
||
1488 | */ |
||
1489 | 12 | View Code Duplication | public function removeRight(string $substring): self |
1501 | |||
1502 | /** |
||
1503 | * Returns a repeated string given a multiplier. |
||
1504 | * |
||
1505 | * @param int $multiplier <p>The number of times to repeat the string.</p> |
||
1506 | * |
||
1507 | * @return static <p>Object with a repeated str.</p> |
||
1508 | */ |
||
1509 | 7 | public function repeat(int $multiplier): self |
|
1515 | |||
1516 | /** |
||
1517 | * Replaces all occurrences of $search in $str by $replacement. |
||
1518 | * |
||
1519 | * @param string $search <p>The needle to search for.</p> |
||
1520 | * @param string $replacement <p>The string to replace with.</p> |
||
1521 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
1522 | * |
||
1523 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1524 | */ |
||
1525 | 29 | View Code Duplication | public function replace(string $search, string $replacement, bool $caseSensitive = true): self |
1535 | |||
1536 | /** |
||
1537 | * Replaces all occurrences of $search in $str by $replacement. |
||
1538 | * |
||
1539 | * @param array $search <p>The elements to search for.</p> |
||
1540 | * @param string|array $replacement <p>The string to replace with.</p> |
||
1541 | * @param bool $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p> |
||
1542 | * |
||
1543 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1544 | */ |
||
1545 | 30 | View Code Duplication | public function replaceAll(array $search, $replacement, bool $caseSensitive = true): self |
1555 | |||
1556 | /** |
||
1557 | * Replaces all occurrences of $search from the beginning of string with $replacement. |
||
1558 | * |
||
1559 | * @param string $search <p>The string to search for.</p> |
||
1560 | * @param string $replacement <p>The replacement.</p> |
||
1561 | * |
||
1562 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1563 | */ |
||
1564 | 16 | public function replaceBeginning(string $search, string $replacement): self |
|
1570 | |||
1571 | /** |
||
1572 | * Replaces all occurrences of $search from the ending of string with $replacement. |
||
1573 | * |
||
1574 | * @param string $search <p>The string to search for.</p> |
||
1575 | * @param string $replacement <p>The replacement.</p> |
||
1576 | * |
||
1577 | * @return static <p>Object with the resulting $str after the replacements.</p> |
||
1578 | */ |
||
1579 | 16 | public function replaceEnding(string $search, string $replacement): self |
|
1585 | |||
1586 | /** |
||
1587 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". |
||
1588 | * If no match is found returns new empty Stringy object. |
||
1589 | * |
||
1590 | * @param string $needle <p>The string to look for.</p> |
||
1591 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1592 | * |
||
1593 | * @return static |
||
1594 | */ |
||
1595 | 2 | View Code Duplication | public function substringOf(string $needle, bool $beforeNeedle = false): self |
1607 | |||
1608 | /** |
||
1609 | * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle". |
||
1610 | * If no match is found returns new empty Stringy object. |
||
1611 | * |
||
1612 | * @param string $needle <p>The string to look for.</p> |
||
1613 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1614 | * |
||
1615 | * @return static |
||
1616 | */ |
||
1617 | 2 | View Code Duplication | public function substringOfIgnoreCase(string $needle, bool $beforeNeedle = false): self |
1629 | |||
1630 | /** |
||
1631 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". |
||
1632 | * If no match is found returns new empty Stringy object. |
||
1633 | * |
||
1634 | * @param string $needle <p>The string to look for.</p> |
||
1635 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1636 | * |
||
1637 | * @return static |
||
1638 | */ |
||
1639 | 2 | View Code Duplication | public function lastSubstringOf(string $needle, bool $beforeNeedle = false): self |
1651 | |||
1652 | /** |
||
1653 | * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle". |
||
1654 | * If no match is found returns new empty Stringy object. |
||
1655 | * |
||
1656 | * @param string $needle <p>The string to look for.</p> |
||
1657 | * @param bool $beforeNeedle [optional] <p>Default: false</p> |
||
1658 | * |
||
1659 | * @return static |
||
1660 | */ |
||
1661 | 1 | View Code Duplication | public function lastSubstringOfIgnoreCase(string $needle, bool $beforeNeedle = false): self |
1673 | |||
1674 | /** |
||
1675 | * Returns a reversed string. A multibyte version of strrev(). |
||
1676 | * |
||
1677 | * @return static <p>Object with a reversed $str.</p> |
||
1678 | */ |
||
1679 | 5 | public function reverse(): self |
|
1685 | |||
1686 | /** |
||
1687 | * Truncates the string to a given length, while ensuring that it does not |
||
1688 | * split words. If $substring is provided, and truncating occurs, the |
||
1689 | * string is further truncated so that the substring may be appended without |
||
1690 | * exceeding the desired length. |
||
1691 | * |
||
1692 | * @param int $length <p>Desired length of the truncated string.</p> |
||
1693 | * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p> |
||
1694 | * |
||
1695 | * @return static <p>Object with the resulting $str after truncating.</p> |
||
1696 | */ |
||
1697 | 23 | public function safeTruncate(int $length, string $substring = ''): self |
|
1726 | |||
1727 | /** |
||
1728 | * A multibyte string shuffle function. It returns a string with its |
||
1729 | * characters in random order. |
||
1730 | * |
||
1731 | * @return static <p>Object with a shuffled $str.</p> |
||
1732 | */ |
||
1733 | 3 | public function shuffle(): self |
|
1739 | |||
1740 | /** |
||
1741 | * Converts the string into an URL slug. This includes replacing non-ASCII |
||
1742 | * characters with their closest ASCII equivalents, removing remaining |
||
1743 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
||
1744 | * $replacement. The replacement defaults to a single dash, and the string |
||
1745 | * is also converted to lowercase. |
||
1746 | * |
||
1747 | * @param string $replacement [optional] <p>The string used to replace whitespace. Default: '-'</p> |
||
1748 | * @param string $language [optional] <p>The language for the url. Default: 'de'</p> |
||
1749 | * @param bool $strToLower [optional] <p>string to lower. Default: true</p> |
||
1750 | * |
||
1751 | * @return static <p>Object whose $str has been converted to an URL slug.</p> |
||
1752 | */ |
||
1753 | 15 | public function slugify(string $replacement = '-', string $language = 'de', bool $strToLower = true): self |
|
1759 | |||
1760 | /** |
||
1761 | * Remove css media-queries. |
||
1762 | * |
||
1763 | * @return static |
||
1764 | */ |
||
1765 | 1 | public function stripeCssMediaQueries(): self |
|
1771 | |||
1772 | /** |
||
1773 | * Strip all whitespace characters. This includes tabs and newline characters, |
||
1774 | * as well as multibyte whitespace such as the thin space and ideographic space. |
||
1775 | * |
||
1776 | * @return static |
||
1777 | */ |
||
1778 | 12 | public function stripWhitespace(): self |
|
1782 | |||
1783 | /** |
||
1784 | * Remove empty html-tag. |
||
1785 | * |
||
1786 | * e.g.: <tag></tag> |
||
1787 | * |
||
1788 | * @return static |
||
1789 | */ |
||
1790 | 1 | public function stripeEmptyHtmlTags(): self |
|
1796 | |||
1797 | /** |
||
1798 | * Converts the string into an valid UTF-8 string. |
||
1799 | * |
||
1800 | * @return static |
||
1801 | */ |
||
1802 | 1 | public function utf8ify(): self |
|
1806 | |||
1807 | /** |
||
1808 | * Create a escape html version of the string via "UTF8::htmlspecialchars()". |
||
1809 | * |
||
1810 | * @return static |
||
1811 | */ |
||
1812 | 6 | public function escape(): self |
|
1822 | |||
1823 | /** |
||
1824 | * Create an extract from a sentence, so if the search-string was found, it try to centered in the output. |
||
1825 | * |
||
1826 | * @param string $search |
||
1827 | * @param int|null $length [optional] <p>Default: null === text->length / 2</p> |
||
1828 | * @param string $replacerForSkippedText [optional] <p>Default: …</p> |
||
1829 | * |
||
1830 | * @return static |
||
1831 | */ |
||
1832 | 1 | public function extractText(string $search = '', int $length = null, string $replacerForSkippedText = '…'): self |
|
1957 | |||
1958 | |||
1959 | /** |
||
1960 | * Try to remove all XSS-attacks from the string. |
||
1961 | * |
||
1962 | * @return static |
||
1963 | */ |
||
1964 | 6 | public function removeXss(): self |
|
1976 | |||
1977 | /** |
||
1978 | * Remove all breaks [<br> | \r\n | \r | \n | ...] from the string. |
||
1979 | * |
||
1980 | * @param string $replacement [optional] <p>Default is a empty string.</p> |
||
1981 | * |
||
1982 | * @return static |
||
1983 | */ |
||
1984 | 6 | public function removeHtmlBreak(string $replacement = ''): self |
|
1990 | |||
1991 | /** |
||
1992 | * Remove html via "strip_tags()" from the string. |
||
1993 | * |
||
1994 | * @param string $allowableTags [optional] <p>You can use the optional second parameter to specify tags which should |
||
1995 | * not be stripped. Default: null |
||
1996 | * </p> |
||
1997 | * |
||
1998 | * @return static |
||
1999 | */ |
||
2000 | 6 | public function removeHtml(string $allowableTags = null): self |
|
2006 | |||
2007 | /** |
||
2008 | * Returns the substring beginning at $start, and up to, but not including |
||
2009 | * the index specified by $end. If $end is omitted, the function extracts |
||
2010 | * the remaining string. If $end is negative, it is computed from the end |
||
2011 | * of the string. |
||
2012 | * |
||
2013 | * @param int $start <p>Initial index from which to begin extraction.</p> |
||
2014 | * @param int $end [optional] <p>Index at which to end extraction. Default: null</p> |
||
2015 | * |
||
2016 | * @return static <p>Object with its $str being the extracted substring.</p> |
||
2017 | */ |
||
2018 | 18 | public function slice(int $start, int $end = null): self |
|
2034 | |||
2035 | /** |
||
2036 | * Splits the string with the provided regular expression, returning an |
||
2037 | * array of Stringy objects. An optional integer $limit will truncate the |
||
2038 | * results. |
||
2039 | * |
||
2040 | * @param string $pattern <p>The regex with which to split the string.</p> |
||
2041 | * @param int $limit [optional] <p>Maximum number of results to return. Default: -1 === no limit</p> |
||
2042 | * |
||
2043 | * @return static[] <p>An array of Stringy objects.</p> |
||
2044 | */ |
||
2045 | 16 | public function split(string $pattern, int $limit = -1): array |
|
2079 | |||
2080 | /** |
||
2081 | * Surrounds $str with the given substring. |
||
2082 | * |
||
2083 | * @param string $substring <p>The substring to add to both sides.</P> |
||
2084 | * |
||
2085 | * @return static <p>Object whose $str had the substring both prepended and appended.</p> |
||
2086 | */ |
||
2087 | 5 | public function surround(string $substring): self |
|
2093 | |||
2094 | /** |
||
2095 | * Returns a case swapped version of the string. |
||
2096 | * |
||
2097 | * @return static <p>Object whose $str has each character's case swapped.</P> |
||
2098 | */ |
||
2099 | 5 | public function swapCase(): self |
|
2107 | |||
2108 | /** |
||
2109 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
||
2110 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
||
2111 | * equivalents. |
||
2112 | * |
||
2113 | * @return static <p>Object whose $str has those characters removed.</p> |
||
2114 | */ |
||
2115 | 4 | public function tidy(): self |
|
2121 | |||
2122 | /** |
||
2123 | * Returns a trimmed string in proper title case. |
||
2124 | * |
||
2125 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
2126 | * capitalized. |
||
2127 | * |
||
2128 | * Adapted from John Gruber's script. |
||
2129 | * |
||
2130 | * @see https://gist.github.com/gruber/9f9e8650d68b13ce4d78 |
||
2131 | * |
||
2132 | * @param array $ignore <p>An array of words not to capitalize.</p> |
||
2133 | * |
||
2134 | * @return static <p>Object with a titleized $str</p> |
||
2135 | */ |
||
2136 | 35 | public function titleizeForHumans(array $ignore = []): self |
|
2263 | |||
2264 | /** |
||
2265 | * Returns a trimmed string with the first letter of each word capitalized. |
||
2266 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
2267 | * capitalized. |
||
2268 | * |
||
2269 | * @param array|null $ignore [optional] <p>An array of words not to capitalize or null. Default: null</p> |
||
2270 | * |
||
2271 | * @return static <p>Object with a titleized $str.</p> |
||
2272 | */ |
||
2273 | 5 | public function titleize(array $ignore = null): self |
|
2294 | |||
2295 | /** |
||
2296 | * Converts all characters in the string to lowercase. |
||
2297 | * |
||
2298 | * @return static <p>Object with all characters of $str being lowercase.</p> |
||
2299 | */ |
||
2300 | 54 | public function toLowerCase(): self |
|
2306 | |||
2307 | /** |
||
2308 | * Returns true if the string is base64 encoded, false otherwise. |
||
2309 | * |
||
2310 | * @return bool <p>Whether or not $str is base64 encoded.</p> |
||
2311 | */ |
||
2312 | 7 | public function isBase64(): bool |
|
2316 | |||
2317 | /** |
||
2318 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
||
2319 | * replaced with their closest ASCII counterparts, and the rest are removed |
||
2320 | * unless instructed otherwise. |
||
2321 | * |
||
2322 | * @param bool $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance | |
||
2323 | * Default: false</p> |
||
2324 | * |
||
2325 | * @return static <p>Object whose $str contains only ASCII characters.</p> |
||
2326 | */ |
||
2327 | 16 | public function toAscii(bool $strict = false): self |
|
2333 | |||
2334 | /** |
||
2335 | * Returns a boolean representation of the given logical string value. |
||
2336 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
2337 | * 'off', and 'no' will return false. In all instances, case is ignored. |
||
2338 | * For other numeric strings, their sign will determine the return value. |
||
2339 | * In addition, blank strings consisting of only whitespace will return |
||
2340 | * false. For all other strings, the return value is a result of a |
||
2341 | * boolean cast. |
||
2342 | * |
||
2343 | * @return bool <p>A boolean value for the string.</p> |
||
2344 | */ |
||
2345 | 15 | public function toBoolean(): bool |
|
2369 | |||
2370 | /** |
||
2371 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
||
2372 | * |
||
2373 | * @return string |
||
2374 | */ |
||
2375 | 1076 | public function toString(): string |
|
2379 | |||
2380 | /** |
||
2381 | * Converts each tab in the string to some number of spaces, as defined by |
||
2382 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
||
2383 | * |
||
2384 | * @param int $tabLength [optional] <p>Number of spaces to replace each tab with. Default: 4</p> |
||
2385 | * |
||
2386 | * @return static <p>Object whose $str has had tabs switched to spaces.</p> |
||
2387 | */ |
||
2388 | 6 | View Code Duplication | public function toSpaces(int $tabLength = 4): self |
2395 | |||
2396 | /** |
||
2397 | * Converts each occurrence of some consecutive number of spaces, as |
||
2398 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
||
2399 | * are converted to a tab. |
||
2400 | * |
||
2401 | * @param int $tabLength [optional] <p>Number of spaces to replace with a tab. Default: 4</p> |
||
2402 | * |
||
2403 | * @return static <p>Object whose $str has had spaces switched to tabs.</p> |
||
2404 | */ |
||
2405 | 5 | View Code Duplication | public function toTabs(int $tabLength = 4): self |
2412 | |||
2413 | /** |
||
2414 | * Converts the first character of each word in the string to uppercase. |
||
2415 | * |
||
2416 | * @return static Object with all characters of $str being title-cased |
||
2417 | */ |
||
2418 | 5 | public function toTitleCase(): self |
|
2425 | |||
2426 | /** |
||
2427 | * Converts all characters in the string to uppercase. |
||
2428 | * |
||
2429 | * @return static Object with all characters of $str being uppercase |
||
2430 | */ |
||
2431 | 5 | public function toUpperCase(): self |
|
2437 | |||
2438 | /** |
||
2439 | * Returns a string with whitespace removed from the start of the string. |
||
2440 | * Supports the removal of unicode whitespace. Accepts an optional |
||
2441 | * string of characters to strip instead of the defaults. |
||
2442 | * |
||
2443 | * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p> |
||
2444 | * |
||
2445 | * @return static <p>Object with a trimmed $str.</p> |
||
2446 | */ |
||
2447 | 13 | View Code Duplication | public function trimLeft(string $chars = null): self |
2457 | |||
2458 | /** |
||
2459 | * Returns a string with whitespace removed from the end of the string. |
||
2460 | * Supports the removal of unicode whitespace. Accepts an optional |
||
2461 | * string of characters to strip instead of the defaults. |
||
2462 | * |
||
2463 | * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p> |
||
2464 | * |
||
2465 | * @return static <p>Object with a trimmed $str.</p> |
||
2466 | */ |
||
2467 | 13 | View Code Duplication | public function trimRight(string $chars = null): self |
2477 | |||
2478 | /** |
||
2479 | * Truncates the string to a given length. If $substring is provided, and |
||
2480 | * truncating occurs, the string is further truncated so that the substring |
||
2481 | * may be appended without exceeding the desired length. |
||
2482 | * |
||
2483 | * @param int $length <p>Desired length of the truncated string.</p> |
||
2484 | * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p> |
||
2485 | * |
||
2486 | * @return static <p>Object with the resulting $str after truncating.</p> |
||
2487 | */ |
||
2488 | 22 | View Code Duplication | public function truncate(int $length, string $substring = ''): self |
2504 | |||
2505 | /** |
||
2506 | * Returns a lowercase and trimmed string separated by underscores. |
||
2507 | * Underscores are inserted before uppercase characters (with the exception |
||
2508 | * of the first character of the string), and in place of spaces as well as |
||
2509 | * dashes. |
||
2510 | * |
||
2511 | * @return static <p>Object with an underscored $str.</p> |
||
2512 | */ |
||
2513 | 16 | public function underscored(): self |
|
2517 | |||
2518 | /** |
||
2519 | * Returns an UpperCamelCase version of the supplied string. It trims |
||
2520 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
||
2521 | * and underscores, and removes spaces, dashes, underscores. |
||
2522 | * |
||
2523 | * @return static <p>Object with $str in UpperCamelCase.</p> |
||
2524 | */ |
||
2525 | 13 | public function upperCamelize(): self |
|
2529 | |||
2530 | /** |
||
2531 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
2532 | * capitalizes letters following digits, spaces, dashes and underscores, |
||
2533 | * and removes spaces, dashes, as well as underscores. |
||
2534 | * |
||
2535 | * @return static <p>Object with $str in camelCase.</p> |
||
2536 | */ |
||
2537 | 32 | public function camelize(): self |
|
2565 | |||
2566 | /** |
||
2567 | * Convert a string to e.g.: "snake_case" |
||
2568 | * |
||
2569 | * @return static <p>Object with $str in snake_case.</p> |
||
2570 | */ |
||
2571 | 20 | public function snakeize(): self |
|
2614 | |||
2615 | /** |
||
2616 | * Converts the first character of the string to lower case. |
||
2617 | * |
||
2618 | * @return static <p>Object with the first character of $str being lower case.</p> |
||
2619 | */ |
||
2620 | 37 | View Code Duplication | public function lowerCaseFirst(): self |
2629 | |||
2630 | /** |
||
2631 | * Shorten the string after $length, but also after the next word. |
||
2632 | * |
||
2633 | * @param int $length |
||
2634 | * @param string $strAddOn [optional] <p>Default: '…'</p> |
||
2635 | * |
||
2636 | * @return static |
||
2637 | */ |
||
2638 | 4 | public function shortenAfterWord(int $length, string $strAddOn = '…'): self |
|
2644 | |||
2645 | /** |
||
2646 | * Line-Wrap the string after $limit, but also after the next word. |
||
2647 | * |
||
2648 | * @param int $limit |
||
2649 | * |
||
2650 | * @return static |
||
2651 | */ |
||
2652 | 1 | public function lineWrapAfterWord(int $limit): self |
|
2664 | |||
2665 | /** |
||
2666 | * Gets the substring after the first occurrence of a separator. |
||
2667 | * If no match is found returns new empty Stringy object. |
||
2668 | * |
||
2669 | * @param string $separator |
||
2670 | * |
||
2671 | * @return static |
||
2672 | */ |
||
2673 | 2 | View Code Duplication | public function afterFirst(string $separator): self |
2697 | |||
2698 | /** |
||
2699 | * Gets the substring after the first occurrence of a separator. |
||
2700 | * If no match is found returns new empty Stringy object. |
||
2701 | * |
||
2702 | * @param string $separator |
||
2703 | * |
||
2704 | * @return static |
||
2705 | */ |
||
2706 | 1 | View Code Duplication | public function afterFirstIgnoreCase(string $separator): self |
2730 | |||
2731 | /** |
||
2732 | * Gets the substring after the last occurrence of a separator. |
||
2733 | * If no match is found returns new empty Stringy object. |
||
2734 | * |
||
2735 | * @param string $separator |
||
2736 | * |
||
2737 | * @return static |
||
2738 | */ |
||
2739 | 1 | View Code Duplication | public function afterLastIgnoreCase(string $separator): self |
2764 | |||
2765 | /** |
||
2766 | * Gets the substring after the last occurrence of a separator. |
||
2767 | * If no match is found returns new empty Stringy object. |
||
2768 | * |
||
2769 | * @param string $separator |
||
2770 | * |
||
2771 | * @return static |
||
2772 | */ |
||
2773 | 1 | View Code Duplication | public function afterLast(string $separator): self |
2798 | |||
2799 | /** |
||
2800 | * Gets the substring before the first occurrence of a separator. |
||
2801 | * If no match is found returns new empty Stringy object. |
||
2802 | * |
||
2803 | * @param string $separator |
||
2804 | * |
||
2805 | * @return static |
||
2806 | */ |
||
2807 | 1 | View Code Duplication | public function beforeFirst(string $separator): self |
2832 | |||
2833 | /** |
||
2834 | * Gets the substring before the first occurrence of a separator. |
||
2835 | * If no match is found returns new empty Stringy object. |
||
2836 | * |
||
2837 | * @param string $separator |
||
2838 | * |
||
2839 | * @return static |
||
2840 | */ |
||
2841 | 1 | View Code Duplication | public function beforeFirstIgnoreCase(string $separator): self |
2866 | |||
2867 | /** |
||
2868 | * Gets the substring before the last occurrence of a separator. |
||
2869 | * If no match is found returns new empty Stringy object. |
||
2870 | * |
||
2871 | * @param string $separator |
||
2872 | * |
||
2873 | * @return static |
||
2874 | */ |
||
2875 | 1 | View Code Duplication | public function beforeLast(string $separator): self |
2900 | |||
2901 | /** |
||
2902 | * Gets the substring before the last occurrence of a separator. |
||
2903 | * If no match is found returns new empty Stringy object. |
||
2904 | * |
||
2905 | * @param string $separator |
||
2906 | * |
||
2907 | * @return static |
||
2908 | */ |
||
2909 | 1 | View Code Duplication | public function beforeLastIgnoreCase(string $separator): self |
2934 | |||
2935 | /** |
||
2936 | * Returns the string with the first letter of each word capitalized, |
||
2937 | * except for when the word is a name which shouldn't be capitalized. |
||
2938 | * |
||
2939 | * @return static <p>Object with $str capitalized.</p> |
||
2940 | */ |
||
2941 | 39 | public function capitalizePersonalName(): self |
|
2949 | |||
2950 | /** |
||
2951 | * @param string $word |
||
2952 | * |
||
2953 | * @return static <p>Object with $str capitalized.</p> |
||
2954 | */ |
||
2955 | 7 | protected function capitalizeWord(string $word): self |
|
2965 | |||
2966 | /** |
||
2967 | * Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius"). |
||
2968 | * |
||
2969 | * @param string $names |
||
2970 | * @param string $delimiter |
||
2971 | * |
||
2972 | * @return static |
||
2973 | */ |
||
2974 | 39 | protected function capitalizePersonalNameByDelimiter(string $names, string $delimiter): self |
|
3050 | } |
||
3051 |
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: