Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Stringy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Stringy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess |
||
16 | { |
||
17 | /** |
||
18 | * An instance's string. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $str; |
||
23 | |||
24 | /** |
||
25 | * The string's encoding, which should be one of the mbstring module's |
||
26 | * supported encodings. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $encoding; |
||
31 | |||
32 | /** |
||
33 | * Initializes a Stringy object and assigns both str and encoding properties |
||
34 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
35 | * $encoding is not specified, it defaults to mb_internal_encoding(). Throws |
||
36 | * an InvalidArgumentException if the first argument is an array or object |
||
37 | * without a __toString method. |
||
38 | * |
||
39 | * @param mixed $str Value to modify, after being cast to string |
||
40 | * @param string $encoding The character encoding |
||
41 | * |
||
42 | * @throws \InvalidArgumentException if an array or object without a |
||
43 | * __toString method is passed as the first argument |
||
44 | */ |
||
45 | 1061 | public function __construct($str = '', $encoding = null) |
|
46 | { |
||
47 | 1061 | if (is_array($str)) { |
|
48 | 1 | throw new \InvalidArgumentException( |
|
49 | 1 | 'Passed value cannot be an array' |
|
50 | ); |
||
51 | 1060 | } elseif (is_object($str) && !method_exists($str, '__toString')) { |
|
52 | 1 | throw new \InvalidArgumentException( |
|
53 | 1 | 'Passed object must have a __toString method' |
|
54 | ); |
||
55 | } |
||
56 | |||
57 | // don't throw a notice on PHP 5.3 |
||
58 | 1059 | if (!defined('ENT_SUBSTITUTE')) { |
|
59 | define('ENT_SUBSTITUTE', 8); |
||
60 | } |
||
61 | |||
62 | // init |
||
63 | 1059 | UTF8::checkForSupport(); |
|
64 | |||
65 | 1059 | $this->str = (string)$str; |
|
66 | |||
67 | 1059 | if ($encoding) { |
|
|
|||
68 | 846 | $this->encoding = $encoding; |
|
69 | } else { |
||
70 | 691 | UTF8::mbstring_loaded(); |
|
71 | 691 | $this->encoding = mb_internal_encoding(); |
|
72 | } |
||
73 | |||
74 | 1059 | if ($encoding) { |
|
75 | 846 | $this->encoding = $encoding; |
|
76 | } else { |
||
77 | 691 | $this->encoding = mb_internal_encoding(); |
|
78 | } |
||
79 | 1059 | } |
|
80 | |||
81 | /** |
||
82 | * Returns the value in $str. |
||
83 | * |
||
84 | * @return string The current value of the $str property |
||
85 | */ |
||
86 | 150 | public function __toString() |
|
90 | |||
91 | /** |
||
92 | * Returns a new string with $string appended. |
||
93 | * |
||
94 | * @param string $string The string to append |
||
95 | * |
||
96 | * @return Stringy Object with appended $string |
||
97 | */ |
||
98 | 5 | public function append($string) |
|
102 | |||
103 | /** |
||
104 | * Append an password (limited to chars that are good readable). |
||
105 | * |
||
106 | * @param int $length length of the random string |
||
107 | * |
||
108 | * @return Stringy Object with appended password |
||
109 | */ |
||
110 | 1 | public function appendPassword($length) |
|
116 | |||
117 | /** |
||
118 | * Append an unique identifier. |
||
119 | * |
||
120 | * @param string|int $extraPrefix |
||
121 | * |
||
122 | * @return Stringy Object with appended unique identifier as md5-hash |
||
123 | */ |
||
124 | 1 | public function appendUniqueIdentifier($extraPrefix = '') |
|
134 | |||
135 | /** |
||
136 | * Append an random string. |
||
137 | * |
||
138 | * @param int $length length of the random string |
||
139 | * @param string $possibleChars characters string for the random selection |
||
140 | * |
||
141 | * @return Stringy Object with appended random string |
||
142 | */ |
||
143 | 2 | public function appendRandomString($length, $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') |
|
144 | { |
||
145 | // init |
||
146 | 2 | $i = 0; |
|
147 | 2 | $length = (int)$length; |
|
148 | 2 | $str = $this->str; |
|
149 | 2 | $maxlength = UTF8::strlen($possibleChars, $this->encoding); |
|
150 | |||
151 | 2 | if ($maxlength === 0) { |
|
152 | 1 | return $this; |
|
153 | } |
||
154 | |||
155 | // add random chars |
||
156 | 2 | while ($i < $length) { |
|
157 | 2 | $char = UTF8::substr($possibleChars, mt_rand(0, $maxlength - 1), 1, $this->encoding); |
|
158 | 2 | $str .= $char; |
|
159 | 2 | $i++; |
|
160 | } |
||
161 | |||
162 | 2 | return $this->append($str); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Creates a Stringy object and assigns both str and encoding properties |
||
167 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
168 | * $encoding is not specified, it defaults to mb_internal_encoding(). It |
||
169 | * then returns the initialized object. Throws an InvalidArgumentException |
||
170 | * if the first argument is an array or object without a __toString method. |
||
171 | * |
||
172 | * @param mixed $str Value to modify, after being cast to string |
||
173 | * @param string $encoding The character encoding |
||
174 | * |
||
175 | * @return Stringy A Stringy object |
||
176 | * @throws \InvalidArgumentException if an array or object without a |
||
177 | * __toString method is passed as the first argument |
||
178 | */ |
||
179 | 1051 | public static function create($str = '', $encoding = null) |
|
183 | |||
184 | /** |
||
185 | * Returns the substring between $start and $end, if found, or an empty |
||
186 | * string. An optional offset may be supplied from which to begin the |
||
187 | * search for the start string. |
||
188 | * |
||
189 | * @param string $start Delimiter marking the start of the substring |
||
190 | * @param string $end Delimiter marking the end of the substring |
||
191 | * @param int $offset Index from which to begin the search |
||
192 | * |
||
193 | * @return Stringy Object whose $str is a substring between $start and $end |
||
194 | */ |
||
195 | 16 | public function between($start, $end, $offset = 0) |
|
210 | |||
211 | /** |
||
212 | * Returns the index of the first occurrence of $needle in the string, |
||
213 | * and false if not found. Accepts an optional offset from which to begin |
||
214 | * the search. |
||
215 | * |
||
216 | * @param string $needle Substring to look for |
||
217 | * @param int $offset Offset from which to search |
||
218 | * |
||
219 | * @return int|bool The occurrence's index if found, otherwise false |
||
220 | */ |
||
221 | 28 | public function indexOf($needle, $offset = 0) |
|
225 | |||
226 | /** |
||
227 | * Returns the substring beginning at $start with the specified $length. |
||
228 | * It differs from the UTF8::substr() function in that providing a $length of |
||
229 | * null will return the rest of the string, rather than an empty string. |
||
230 | * |
||
231 | * @param int $start Position of the first character to use |
||
232 | * @param int $length Maximum number of characters used |
||
233 | * |
||
234 | * @return Stringy Object with its $str being the substring |
||
235 | */ |
||
236 | 64 | public function substr($start, $length = null) |
|
237 | { |
||
238 | 64 | if ($length === null) { |
|
239 | 19 | $length = $this->length(); |
|
240 | } |
||
241 | |||
242 | 64 | $str = UTF8::substr($this->str, $start, $length, $this->encoding); |
|
243 | |||
244 | 64 | return static::create($str, $this->encoding); |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * Returns the length of the string. |
||
249 | * |
||
250 | * @return int The number of characters in $str given the encoding |
||
251 | */ |
||
252 | 248 | public function length() |
|
256 | |||
257 | /** |
||
258 | * Trims the string and replaces consecutive whitespace characters with a |
||
259 | * single space. This includes tabs and newline characters, as well as |
||
260 | * multibyte whitespace such as the thin space and ideographic space. |
||
261 | * |
||
262 | * @return Stringy Object with a trimmed $str and condensed whitespace |
||
263 | */ |
||
264 | 52 | public function collapseWhitespace() |
|
268 | |||
269 | /** |
||
270 | * Returns a string with whitespace removed from the start and end of the |
||
271 | * string. Supports the removal of unicode whitespace. Accepts an optional |
||
272 | * string of characters to strip instead of the defaults. |
||
273 | * |
||
274 | * @param string $chars Optional string of characters to strip |
||
275 | * |
||
276 | * @return Stringy Object with a trimmed $str |
||
277 | */ |
||
278 | 153 | View Code Duplication | public function trim($chars = null) |
279 | { |
||
280 | 153 | if (!$chars) { |
|
281 | 152 | $chars = '[:space:]'; |
|
282 | } else { |
||
283 | 1 | $chars = preg_quote($chars, '/'); |
|
284 | } |
||
285 | |||
286 | 153 | return $this->regexReplace("^[$chars]+|[$chars]+\$", ''); |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
291 | * |
||
292 | * @param string $pattern The regular expression pattern |
||
293 | * @param string $replacement The string to replace with |
||
294 | * @param string $options Matching conditions to be used |
||
295 | * |
||
296 | * @return Stringy Object with the result2ing $str after the replacements |
||
297 | */ |
||
298 | 223 | public function regexReplace($pattern, $replacement, $options = '') |
|
299 | { |
||
300 | 223 | if ($options === 'msr') { |
|
301 | 8 | $options = 'ms'; |
|
302 | } |
||
303 | |||
304 | 223 | $str = preg_replace( |
|
305 | 223 | '/' . $pattern . '/u' . $options, |
|
306 | $replacement, |
||
307 | 223 | $this->str |
|
308 | ); |
||
309 | |||
310 | 223 | return static::create($str, $this->encoding); |
|
311 | } |
||
312 | |||
313 | /** |
||
314 | * Returns true if the string contains all $needles, false otherwise. By |
||
315 | * default the comparison is case-sensitive, but can be made insensitive by |
||
316 | * setting $caseSensitive to false. |
||
317 | * |
||
318 | * @param array $needles SubStrings to look for |
||
319 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
320 | * |
||
321 | * @return bool Whether or not $str contains $needle |
||
322 | */ |
||
323 | 43 | View Code Duplication | public function containsAll($needles, $caseSensitive = true) |
324 | { |
||
325 | /** @noinspection IsEmptyFunctionUsageInspection */ |
||
326 | 43 | if (empty($needles)) { |
|
327 | 1 | return false; |
|
328 | } |
||
329 | |||
330 | 42 | foreach ($needles as $needle) { |
|
331 | 42 | if (!$this->contains($needle, $caseSensitive)) { |
|
332 | 42 | return false; |
|
333 | } |
||
334 | } |
||
335 | |||
336 | 24 | return true; |
|
337 | } |
||
338 | |||
339 | /** |
||
340 | * Returns true if the string contains $needle, false otherwise. By default |
||
341 | * the comparison is case-sensitive, but can be made insensitive by setting |
||
342 | * $caseSensitive to false. |
||
343 | * |
||
344 | * @param string $needle Substring to look for |
||
345 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
346 | * |
||
347 | * @return bool Whether or not $str contains $needle |
||
348 | */ |
||
349 | 105 | public function contains($needle, $caseSensitive = true) |
|
359 | |||
360 | /** |
||
361 | * Returns true if the string contains any $needles, false otherwise. By |
||
362 | * default the comparison is case-sensitive, but can be made insensitive by |
||
363 | * setting $caseSensitive to false. |
||
364 | * |
||
365 | * @param array $needles SubStrings to look for |
||
366 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
367 | * |
||
368 | * @return bool Whether or not $str contains $needle |
||
369 | */ |
||
370 | 43 | View Code Duplication | public function containsAny($needles, $caseSensitive = true) |
371 | { |
||
372 | /** @noinspection IsEmptyFunctionUsageInspection */ |
||
373 | 43 | if (empty($needles)) { |
|
374 | 1 | return false; |
|
375 | } |
||
376 | |||
377 | 42 | foreach ($needles as $needle) { |
|
378 | 42 | if ($this->contains($needle, $caseSensitive)) { |
|
379 | 42 | return true; |
|
380 | } |
||
381 | } |
||
382 | |||
383 | 18 | return false; |
|
384 | } |
||
385 | |||
386 | /** |
||
387 | * Returns the length of the string, implementing the countable interface. |
||
388 | * |
||
389 | * @return int The number of characters in the string, given the encoding |
||
390 | */ |
||
391 | 1 | public function count() |
|
395 | |||
396 | /** |
||
397 | * Returns the number of occurrences of $substring in the given string. |
||
398 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
399 | * by setting $caseSensitive to false. |
||
400 | * |
||
401 | * @param string $substring The substring to search for |
||
402 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
403 | * |
||
404 | * @return int The number of $substring occurrences |
||
405 | */ |
||
406 | 15 | public function countSubstr($substring, $caseSensitive = true) |
|
417 | |||
418 | /** |
||
419 | * Returns a lowercase and trimmed string separated by dashes. Dashes are |
||
420 | * inserted before uppercase characters (with the exception of the first |
||
421 | * character of the string), and in place of spaces as well as underscores. |
||
422 | * |
||
423 | * @return Stringy Object with a dasherized $str |
||
424 | */ |
||
425 | 19 | public function dasherize() |
|
429 | |||
430 | /** |
||
431 | * Returns a lowercase and trimmed string separated by the given delimiter. |
||
432 | * Delimiters are inserted before uppercase characters (with the exception |
||
433 | * of the first character of the string), and in place of spaces, dashes, |
||
434 | * and underscores. Alpha delimiters are not converted to lowercase. |
||
435 | * |
||
436 | * @param string $delimiter Sequence used to separate parts of the string |
||
437 | * |
||
438 | * @return Stringy Object with a delimited $str |
||
439 | */ |
||
440 | 49 | public function delimit($delimiter) |
|
452 | |||
453 | /** |
||
454 | * Ensures that the string begins with $substring. If it doesn't, it's |
||
455 | * prepended. |
||
456 | * |
||
457 | * @param string $substring The substring to add if not present |
||
458 | * |
||
459 | * @return Stringy Object with its $str prefixed by the $substring |
||
460 | */ |
||
461 | 10 | View Code Duplication | public function ensureLeft($substring) |
462 | { |
||
463 | 10 | $stringy = static::create($this->str, $this->encoding); |
|
464 | |||
465 | 10 | if (!$stringy->startsWith($substring)) { |
|
466 | 4 | $stringy->str = $substring . $stringy->str; |
|
467 | } |
||
468 | |||
469 | 10 | return $stringy; |
|
470 | } |
||
471 | |||
472 | /** |
||
473 | * Returns true if the string begins with $substring, false otherwise. By |
||
474 | * default, the comparison is case-sensitive, but can be made insensitive |
||
475 | * by setting $caseSensitive to false. |
||
476 | * |
||
477 | * @param string $substring The substring to look for |
||
478 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
479 | * |
||
480 | * @return bool Whether or not $str starts with $substring |
||
481 | */ |
||
482 | 33 | public function startsWith($substring, $caseSensitive = true) |
|
483 | { |
||
484 | 33 | $str = $this->str; |
|
485 | |||
486 | 33 | View Code Duplication | if (!$caseSensitive) { |
487 | 4 | $substring = UTF8::strtolower($substring, $this->encoding); |
|
488 | 4 | $str = UTF8::strtolower($this->str, $this->encoding); |
|
489 | } |
||
490 | |||
491 | 33 | return UTF8::strpos($str, $substring, $this->encoding) === 0; |
|
492 | } |
||
493 | |||
494 | /** |
||
495 | * Ensures that the string ends with $substring. If it doesn't, it's |
||
496 | * appended. |
||
497 | * |
||
498 | * @param string $substring The substring to add if not present |
||
499 | * |
||
500 | * @return Stringy Object with its $str suffixed by the $substring |
||
501 | */ |
||
502 | 10 | View Code Duplication | public function ensureRight($substring) |
503 | { |
||
504 | 10 | $stringy = static::create($this->str, $this->encoding); |
|
505 | |||
506 | 10 | if (!$stringy->endsWith($substring)) { |
|
507 | 4 | $stringy->str .= $substring; |
|
508 | } |
||
509 | |||
510 | 10 | return $stringy; |
|
511 | } |
||
512 | |||
513 | /** |
||
514 | * Returns true if the string ends with $substring, false otherwise. By |
||
515 | * default, the comparison is case-sensitive, but can be made insensitive |
||
516 | * by setting $caseSensitive to false. |
||
517 | * |
||
518 | * @param string $substring The substring to look for |
||
519 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
520 | * |
||
521 | * @return bool Whether or not $str ends with $substring |
||
522 | */ |
||
523 | 33 | public function endsWith($substring, $caseSensitive = true) |
|
524 | { |
||
525 | 33 | $substringLength = UTF8::strlen($substring, $this->encoding); |
|
526 | 33 | $strLength = $this->length(); |
|
527 | |||
528 | 33 | $endOfStr = UTF8::substr( |
|
529 | 33 | $this->str, |
|
530 | 33 | $strLength - $substringLength, |
|
531 | $substringLength, |
||
532 | 33 | $this->encoding |
|
533 | ); |
||
534 | |||
535 | 33 | View Code Duplication | if (!$caseSensitive) { |
536 | 4 | $substring = UTF8::strtolower($substring, $this->encoding); |
|
537 | 4 | $endOfStr = UTF8::strtolower($endOfStr, $this->encoding); |
|
538 | } |
||
539 | |||
540 | 33 | return (string)$substring === $endOfStr; |
|
541 | } |
||
542 | |||
543 | /** |
||
544 | * Returns the first $n characters of the string. |
||
545 | * |
||
546 | * @param int $n Number of characters to retrieve from the start |
||
547 | * |
||
548 | * @return Stringy Object with its $str being the first $n chars |
||
549 | */ |
||
550 | 12 | View Code Duplication | public function first($n) |
551 | { |
||
552 | 12 | $stringy = static::create($this->str, $this->encoding); |
|
553 | |||
554 | 12 | if ($n < 0) { |
|
555 | 2 | $stringy->str = ''; |
|
556 | } else { |
||
557 | 10 | return $stringy->substr(0, $n); |
|
558 | } |
||
559 | |||
560 | 2 | return $stringy; |
|
561 | } |
||
562 | |||
563 | /** |
||
564 | * Returns the encoding used by the Stringy object. |
||
565 | * |
||
566 | * @return string The current value of the $encoding property |
||
567 | */ |
||
568 | 3 | public function getEncoding() |
|
572 | |||
573 | /** |
||
574 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
575 | * interface. The ArrayIterator's constructor is passed an array of chars |
||
576 | * in the multibyte string. This enables the use of foreach with instances |
||
577 | * of Stringy\Stringy. |
||
578 | * |
||
579 | * @return \ArrayIterator An iterator for the characters in the string |
||
580 | */ |
||
581 | 1 | public function getIterator() |
|
585 | |||
586 | /** |
||
587 | * Returns an array consisting of the characters in the string. |
||
588 | * |
||
589 | * @return array An array of string chars |
||
590 | */ |
||
591 | 4 | public function chars() |
|
592 | { |
||
593 | // init |
||
594 | 4 | $chars = array(); |
|
595 | 4 | $l = $this->length(); |
|
596 | |||
597 | 4 | for ($i = 0; $i < $l; $i++) { |
|
598 | 3 | $chars[] = $this->at($i)->str; |
|
599 | } |
||
600 | |||
601 | 4 | return $chars; |
|
602 | } |
||
603 | |||
604 | /** |
||
605 | * Returns the character at $index, with indexes starting at 0. |
||
606 | * |
||
607 | * @param int $index Position of the character |
||
608 | * |
||
609 | * @return Stringy The character at $index |
||
610 | */ |
||
611 | 11 | public function at($index) |
|
615 | |||
616 | /** |
||
617 | * Returns true if the string contains a lower case char, false |
||
618 | * otherwise. |
||
619 | * |
||
620 | * @return bool Whether or not the string contains a lower case character. |
||
621 | */ |
||
622 | 12 | public function hasLowerCase() |
|
626 | |||
627 | /** |
||
628 | * Returns true if $str matches the supplied pattern, false otherwise. |
||
629 | * |
||
630 | * @param string $pattern Regex pattern to match against |
||
631 | * |
||
632 | * @return bool Whether or not $str matches the pattern |
||
633 | */ |
||
634 | 91 | private function matchesPattern($pattern) |
|
642 | |||
643 | /** |
||
644 | * Returns true if the string contains an upper case char, false |
||
645 | * otherwise. |
||
646 | * |
||
647 | * @return bool Whether or not the string contains an upper case character. |
||
648 | */ |
||
649 | 12 | public function hasUpperCase() |
|
653 | |||
654 | /** |
||
655 | * Convert all HTML entities to their applicable characters. |
||
656 | * |
||
657 | * @param int|null $flags Optional flags |
||
658 | * |
||
659 | * @return Stringy Object with the resulting $str after being html decoded. |
||
660 | */ |
||
661 | 5 | public function htmlDecode($flags = ENT_COMPAT) |
|
667 | |||
668 | /** |
||
669 | * Convert all applicable characters to HTML entities. |
||
670 | * |
||
671 | * @param int|null $flags Optional flags |
||
672 | * |
||
673 | * @return Stringy Object with the resulting $str after being html encoded. |
||
674 | */ |
||
675 | 5 | public function htmlEncode($flags = ENT_COMPAT) |
|
681 | |||
682 | /** |
||
683 | * Capitalizes the first word of the string, replaces underscores with |
||
684 | * spaces, and strips '_id'. |
||
685 | * |
||
686 | * @return Stringy Object with a humanized $str |
||
687 | */ |
||
688 | 3 | public function humanize() |
|
694 | |||
695 | /** |
||
696 | * Converts the first character of the supplied string to upper case. |
||
697 | * |
||
698 | * @return Stringy Object with the first character of $str being upper case |
||
699 | */ |
||
700 | 27 | View Code Duplication | public function upperCaseFirst() |
701 | { |
||
702 | 27 | $first = UTF8::substr($this->str, 0, 1, $this->encoding); |
|
703 | 27 | $rest = UTF8::substr( |
|
704 | 27 | $this->str, |
|
705 | 27 | 1, |
|
706 | 27 | $this->length() - 1, |
|
707 | 27 | $this->encoding |
|
708 | ); |
||
709 | |||
710 | 27 | $str = UTF8::strtoupper($first, $this->encoding) . $rest; |
|
711 | |||
712 | 27 | return static::create($str, $this->encoding); |
|
713 | } |
||
714 | |||
715 | /** |
||
716 | * Returns the index of the last occurrence of $needle in the string, |
||
717 | * and false if not found. Accepts an optional offset from which to begin |
||
718 | * the search. Offsets may be negative to count from the last character |
||
719 | * in the string. |
||
720 | * |
||
721 | * @param string $needle Substring to look for |
||
722 | * @param int $offset Offset from which to search |
||
723 | * |
||
724 | * @return int|bool The last occurrence's index if found, otherwise false |
||
725 | */ |
||
726 | 12 | public function indexOfLast($needle, $offset = 0) |
|
730 | |||
731 | /** |
||
732 | * Inserts $substring into the string at the $index provided. |
||
733 | * |
||
734 | * @param string $substring String to be inserted |
||
735 | * @param int $index The index at which to insert the substring |
||
736 | * |
||
737 | * @return Stringy Object with the resulting $str after the insertion |
||
738 | */ |
||
739 | 8 | View Code Duplication | public function insert($substring, $index) |
753 | |||
754 | /** |
||
755 | * Returns true if the string contains only alphabetic chars, false |
||
756 | * otherwise. |
||
757 | * |
||
758 | * @return bool Whether or not $str contains only alphabetic chars |
||
759 | */ |
||
760 | 10 | public function isAlpha() |
|
764 | |||
765 | /** |
||
766 | * Determine whether the string is considered to be empty. |
||
767 | * |
||
768 | * A variable is considered empty if it does not exist or if its value equals FALSE. |
||
769 | * empty() does not generate a warning if the variable does not exist. |
||
770 | * |
||
771 | * @return bool |
||
772 | */ |
||
773 | public function isEmpty() |
||
777 | |||
778 | /** |
||
779 | * Returns true if the string contains only alphabetic and numeric chars, |
||
780 | * false otherwise. |
||
781 | * |
||
782 | * @return bool Whether or not $str contains only alphanumeric chars |
||
783 | */ |
||
784 | 13 | public function isAlphanumeric() |
|
788 | |||
789 | /** |
||
790 | * Returns true if the string contains only whitespace chars, false |
||
791 | * otherwise. |
||
792 | * |
||
793 | * @return bool Whether or not $str contains only whitespace characters |
||
794 | */ |
||
795 | 15 | public function isBlank() |
|
799 | |||
800 | /** |
||
801 | * Returns true if the string contains only hexadecimal chars, false |
||
802 | * otherwise. |
||
803 | * |
||
804 | * @return bool Whether or not $str contains only hexadecimal chars |
||
805 | */ |
||
806 | 13 | public function isHexadecimal() |
|
810 | |||
811 | /** |
||
812 | * Returns true if the string contains HTML-Tags, false otherwise. |
||
813 | * |
||
814 | * @return bool Whether or not $str contains HTML-Tags |
||
815 | */ |
||
816 | 1 | public function isHtml() |
|
820 | |||
821 | /** |
||
822 | * Returns true if the string contains a valid E-Mail address, false otherwise. |
||
823 | * |
||
824 | * @param bool $useExampleDomainCheck |
||
825 | * @param bool $useTypoInDomainCheck |
||
826 | * @param bool $useTemporaryDomainCheck |
||
827 | * @param bool $useDnsCheck |
||
828 | * |
||
829 | * @return bool Whether or not $str contains a valid E-Mail address |
||
830 | */ |
||
831 | 1 | public function isEmail($useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false) |
|
835 | |||
836 | /** |
||
837 | * Returns true if the string is JSON, false otherwise. Unlike json_decode |
||
838 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
||
839 | * in that an empty string is not considered valid JSON. |
||
840 | * |
||
841 | * @return bool Whether or not $str is JSON |
||
842 | */ |
||
843 | 20 | public function isJson() |
|
857 | |||
858 | /** |
||
859 | * Returns true if the string contains only lower case chars, false |
||
860 | * otherwise. |
||
861 | * |
||
862 | * @return bool Whether or not $str contains only lower case characters |
||
863 | */ |
||
864 | 8 | public function isLowerCase() |
|
872 | |||
873 | /** |
||
874 | * Returns true if the string is serialized, false otherwise. |
||
875 | * |
||
876 | * @return bool Whether or not $str is serialized |
||
877 | */ |
||
878 | 7 | public function isSerialized() |
|
879 | { |
||
880 | 7 | if (!isset($this->str[0])) { |
|
881 | 1 | return false; |
|
882 | } |
||
883 | |||
884 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
885 | if ( |
||
886 | 6 | $this->str === 'b:0;' |
|
887 | || |
||
888 | 6 | @unserialize($this->str) !== false |
|
889 | ) { |
||
890 | 4 | return true; |
|
891 | } else { |
||
892 | 2 | return false; |
|
893 | } |
||
894 | } |
||
895 | |||
896 | /** |
||
897 | * Returns true if the string contains only lower case chars, false |
||
898 | * otherwise. |
||
899 | * |
||
900 | * @return bool Whether or not $str contains only lower case characters |
||
901 | */ |
||
902 | 8 | public function isUpperCase() |
|
906 | |||
907 | /** |
||
908 | * Returns the last $n characters of the string. |
||
909 | * |
||
910 | * @param int $n Number of characters to retrieve from the end |
||
911 | * |
||
912 | * @return Stringy Object with its $str being the last $n chars |
||
913 | */ |
||
914 | 12 | View Code Duplication | public function last($n) |
915 | { |
||
916 | 12 | $stringy = static::create($this->str, $this->encoding); |
|
917 | |||
918 | 12 | if ($n <= 0) { |
|
919 | 4 | $stringy->str = ''; |
|
920 | } else { |
||
921 | 8 | return $stringy->substr(-$n); |
|
922 | } |
||
923 | |||
924 | 4 | return $stringy; |
|
925 | } |
||
926 | |||
927 | /** |
||
928 | * Splits on newlines and carriage returns, returning an array of Stringy |
||
929 | * objects corresponding to the lines in the string. |
||
930 | * |
||
931 | * @return Stringy[] An array of Stringy objects |
||
932 | */ |
||
933 | 15 | public function lines() |
|
934 | { |
||
935 | 15 | $array = preg_split('/[\r\n]{1,2}/u', $this->str); |
|
936 | /** @noinspection CallableInLoopTerminationConditionInspection */ |
||
937 | /** @noinspection ForeachInvariantsInspection */ |
||
938 | 15 | View Code Duplication | for ($i = 0; $i < count($array); $i++) { |
939 | 15 | $array[$i] = static::create($array[$i], $this->encoding); |
|
940 | } |
||
941 | |||
942 | 15 | return $array; |
|
943 | } |
||
944 | |||
945 | /** |
||
946 | * Returns the longest common prefix between the string and $otherStr. |
||
947 | * |
||
948 | * @param string $otherStr Second string for comparison |
||
949 | * |
||
950 | * @return Stringy Object with its $str being the longest common prefix |
||
951 | */ |
||
952 | 10 | public function longestCommonPrefix($otherStr) |
|
953 | { |
||
954 | 10 | $encoding = $this->encoding; |
|
955 | 10 | $maxLength = min($this->length(), UTF8::strlen($otherStr, $encoding)); |
|
956 | |||
957 | 10 | $longestCommonPrefix = ''; |
|
958 | 10 | for ($i = 0; $i < $maxLength; $i++) { |
|
959 | 8 | $char = UTF8::substr($this->str, $i, 1, $encoding); |
|
960 | |||
961 | 8 | if ($char == UTF8::substr($otherStr, $i, 1, $encoding)) { |
|
962 | 6 | $longestCommonPrefix .= $char; |
|
963 | } else { |
||
964 | 6 | break; |
|
965 | } |
||
966 | } |
||
967 | |||
968 | 10 | return static::create($longestCommonPrefix, $encoding); |
|
969 | } |
||
970 | |||
971 | /** |
||
972 | * Returns the longest common suffix between the string and $otherStr. |
||
973 | * |
||
974 | * @param string $otherStr Second string for comparison |
||
975 | * |
||
976 | * @return Stringy Object with its $str being the longest common suffix |
||
977 | */ |
||
978 | 10 | public function longestCommonSuffix($otherStr) |
|
979 | { |
||
980 | 10 | $encoding = $this->encoding; |
|
981 | 10 | $maxLength = min($this->length(), UTF8::strlen($otherStr, $encoding)); |
|
982 | |||
983 | 10 | $longestCommonSuffix = ''; |
|
984 | 10 | for ($i = 1; $i <= $maxLength; $i++) { |
|
985 | 8 | $char = UTF8::substr($this->str, -$i, 1, $encoding); |
|
986 | |||
987 | 8 | if ($char == UTF8::substr($otherStr, -$i, 1, $encoding)) { |
|
988 | 6 | $longestCommonSuffix = $char . $longestCommonSuffix; |
|
989 | } else { |
||
990 | 6 | break; |
|
991 | } |
||
992 | } |
||
993 | |||
994 | 10 | return static::create($longestCommonSuffix, $encoding); |
|
995 | } |
||
996 | |||
997 | /** |
||
998 | * Returns the longest common substring between the string and $otherStr. |
||
999 | * In the case of ties, it returns that which occurs first. |
||
1000 | * |
||
1001 | * @param string $otherStr Second string for comparison |
||
1002 | * |
||
1003 | * @return Stringy Object with its $str being the longest common substring |
||
1004 | */ |
||
1005 | 10 | public function longestCommonSubstring($otherStr) |
|
1006 | { |
||
1007 | // Uses dynamic programming to solve |
||
1008 | // http://en.wikipedia.org/wiki/Longest_common_substring_problem |
||
1009 | 10 | $encoding = $this->encoding; |
|
1010 | 10 | $stringy = static::create($this->str, $encoding); |
|
1011 | 10 | $strLength = $stringy->length(); |
|
1012 | 10 | $otherLength = UTF8::strlen($otherStr, $encoding); |
|
1013 | |||
1014 | // Return if either string is empty |
||
1015 | 10 | if ($strLength == 0 || $otherLength == 0) { |
|
1016 | 2 | $stringy->str = ''; |
|
1017 | |||
1018 | 2 | return $stringy; |
|
1019 | } |
||
1020 | |||
1021 | 8 | $len = 0; |
|
1022 | 8 | $end = 0; |
|
1023 | 8 | $table = array_fill( |
|
1024 | 8 | 0, $strLength + 1, |
|
1025 | 8 | array_fill(0, $otherLength + 1, 0) |
|
1026 | ); |
||
1027 | |||
1028 | 8 | for ($i = 1; $i <= $strLength; $i++) { |
|
1029 | 8 | for ($j = 1; $j <= $otherLength; $j++) { |
|
1030 | 8 | $strChar = UTF8::substr($stringy->str, $i - 1, 1, $encoding); |
|
1031 | 8 | $otherChar = UTF8::substr($otherStr, $j - 1, 1, $encoding); |
|
1032 | |||
1033 | 8 | if ($strChar == $otherChar) { |
|
1034 | 8 | $table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
|
1035 | 8 | if ($table[$i][$j] > $len) { |
|
1036 | 8 | $len = $table[$i][$j]; |
|
1037 | 8 | $end = $i; |
|
1038 | } |
||
1039 | } else { |
||
1040 | 8 | $table[$i][$j] = 0; |
|
1041 | } |
||
1042 | } |
||
1043 | } |
||
1044 | |||
1045 | 8 | $stringy->str = UTF8::substr($stringy->str, $end - $len, $len, $encoding); |
|
1046 | |||
1047 | 8 | return $stringy; |
|
1048 | } |
||
1049 | |||
1050 | /** |
||
1051 | * Returns whether or not a character exists at an index. Offsets may be |
||
1052 | * negative to count from the last character in the string. Implements |
||
1053 | * part of the ArrayAccess interface. |
||
1054 | * |
||
1055 | * @param mixed $offset The index to check |
||
1056 | * |
||
1057 | * @return boolean Whether or not the index exists |
||
1058 | */ |
||
1059 | 6 | public function offsetExists($offset) |
|
1071 | |||
1072 | /** |
||
1073 | * Returns the character at the given index. Offsets may be negative to |
||
1074 | * count from the last character in the string. Implements part of the |
||
1075 | * ArrayAccess interface, and throws an OutOfBoundsException if the index |
||
1076 | * does not exist. |
||
1077 | * |
||
1078 | * @param mixed $offset The index from which to retrieve the char |
||
1079 | * |
||
1080 | * @return string The character at the specified index |
||
1081 | * @throws \OutOfBoundsException If the positive or negative offset does |
||
1082 | * not exist |
||
1083 | */ |
||
1084 | 2 | public function offsetGet($offset) |
|
1085 | { |
||
1086 | // init |
||
1087 | 2 | $offset = (int)$offset; |
|
1088 | 2 | $length = $this->length(); |
|
1089 | |||
1090 | if ( |
||
1091 | 2 | ($offset >= 0 && $length <= $offset) |
|
1092 | || |
||
1093 | 2 | $length < abs($offset) |
|
1094 | ) { |
||
1095 | 1 | throw new \OutOfBoundsException('No character exists at the index'); |
|
1096 | } |
||
1097 | |||
1098 | 1 | return UTF8::substr($this->str, $offset, 1, $this->encoding); |
|
1099 | } |
||
1100 | |||
1101 | /** |
||
1102 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1103 | * when called. This maintains the immutability of Stringy objects. |
||
1104 | * |
||
1105 | * @param mixed $offset The index of the character |
||
1106 | * @param mixed $value Value to set |
||
1107 | * |
||
1108 | * @throws \Exception When called |
||
1109 | */ |
||
1110 | 1 | public function offsetSet($offset, $value) |
|
1115 | |||
1116 | /** |
||
1117 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1118 | * when called. This maintains the immutability of Stringy objects. |
||
1119 | * |
||
1120 | * @param mixed $offset The index of the character |
||
1121 | * |
||
1122 | * @throws \Exception When called |
||
1123 | */ |
||
1124 | 1 | public function offsetUnset($offset) |
|
1129 | |||
1130 | /** |
||
1131 | * Pads the string to a given length with $padStr. If length is less than |
||
1132 | * or equal to the length of the string, no padding takes places. The |
||
1133 | * default string used for padding is a space, and the default type (one of |
||
1134 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
||
1135 | * if $padType isn't one of those 3 values. |
||
1136 | * |
||
1137 | * @param int $length Desired string length after padding |
||
1138 | * @param string $padStr String used to pad, defaults to space |
||
1139 | * @param string $padType One of 'left', 'right', 'both' |
||
1140 | * |
||
1141 | * @return Stringy Object with a padded $str |
||
1142 | * @throws \InvalidArgumentException If $padType isn't one of 'right', 'left' or 'both' |
||
1143 | */ |
||
1144 | 13 | public function pad($length, $padStr = ' ', $padType = 'right') |
|
1145 | { |
||
1146 | 13 | if (!in_array($padType, array('left', 'right', 'both'), true)) { |
|
1147 | 1 | throw new \InvalidArgumentException( |
|
1148 | 1 | 'Pad expects $padType ' . "to be one of 'left', 'right' or 'both'" |
|
1149 | ); |
||
1150 | } |
||
1151 | |||
1152 | switch ($padType) { |
||
1153 | 12 | case 'left': |
|
1154 | 3 | return $this->padLeft($length, $padStr); |
|
1155 | 9 | case 'right': |
|
1156 | 6 | return $this->padRight($length, $padStr); |
|
1157 | default: |
||
1158 | 3 | return $this->padBoth($length, $padStr); |
|
1159 | } |
||
1160 | } |
||
1161 | |||
1162 | /** |
||
1163 | * Returns a new string of a given length such that the beginning of the |
||
1164 | * string is padded. Alias for pad() with a $padType of 'left'. |
||
1165 | * |
||
1166 | * @param int $length Desired string length after padding |
||
1167 | * @param string $padStr String used to pad, defaults to space |
||
1168 | * |
||
1169 | * @return Stringy String with left padding |
||
1170 | */ |
||
1171 | 10 | public function padLeft($length, $padStr = ' ') |
|
1175 | |||
1176 | /** |
||
1177 | * Adds the specified amount of left and right padding to the given string. |
||
1178 | * The default character used is a space. |
||
1179 | * |
||
1180 | * @param int $left Length of left padding |
||
1181 | * @param int $right Length of right padding |
||
1182 | * @param string $padStr String used to pad |
||
1183 | * |
||
1184 | * @return Stringy String with padding applied |
||
1185 | */ |
||
1186 | 37 | private function applyPadding($left = 0, $right = 0, $padStr = ' ') |
|
1187 | { |
||
1188 | 37 | $stringy = static::create($this->str, $this->encoding); |
|
1189 | |||
1190 | 37 | $length = UTF8::strlen($padStr, $stringy->encoding); |
|
1191 | |||
1192 | 37 | $strLength = $stringy->length(); |
|
1193 | 37 | $paddedLength = $strLength + $left + $right; |
|
1194 | |||
1195 | 37 | if (!$length || $paddedLength <= $strLength) { |
|
1196 | 3 | return $stringy; |
|
1197 | } |
||
1198 | |||
1199 | 34 | $leftPadding = UTF8::substr( |
|
1200 | 34 | UTF8::str_repeat( |
|
1201 | $padStr, |
||
1202 | 34 | ceil($left / $length) |
|
1203 | ), |
||
1204 | 34 | 0, |
|
1205 | $left, |
||
1206 | 34 | $stringy->encoding |
|
1207 | ); |
||
1208 | |||
1209 | 34 | $rightPadding = UTF8::substr( |
|
1210 | 34 | UTF8::str_repeat( |
|
1211 | $padStr, |
||
1212 | 34 | ceil($right / $length) |
|
1213 | ), |
||
1214 | 34 | 0, |
|
1215 | $right, |
||
1216 | 34 | $stringy->encoding |
|
1217 | ); |
||
1218 | |||
1219 | 34 | $stringy->str = $leftPadding . $stringy->str . $rightPadding; |
|
1220 | |||
1221 | 34 | return $stringy; |
|
1222 | } |
||
1223 | |||
1224 | /** |
||
1225 | * Returns a new string of a given length such that the end of the string |
||
1226 | * is padded. Alias for pad() with a $padType of 'right'. |
||
1227 | * |
||
1228 | * @param int $length Desired string length after padding |
||
1229 | * @param string $padStr String used to pad, defaults to space |
||
1230 | * |
||
1231 | * @return Stringy String with right padding |
||
1232 | */ |
||
1233 | 13 | public function padRight($length, $padStr = ' ') |
|
1237 | |||
1238 | /** |
||
1239 | * Returns a new string of a given length such that both sides of the |
||
1240 | * string are padded. Alias for pad() with a $padType of 'both'. |
||
1241 | * |
||
1242 | * @param int $length Desired string length after padding |
||
1243 | * @param string $padStr String used to pad, defaults to space |
||
1244 | * |
||
1245 | * @return Stringy String with padding applied |
||
1246 | */ |
||
1247 | 14 | public function padBoth($length, $padStr = ' ') |
|
1253 | |||
1254 | /** |
||
1255 | * Returns a new string starting with $string. |
||
1256 | * |
||
1257 | * @param string $string The string to append |
||
1258 | * |
||
1259 | * @return Stringy Object with appended $string |
||
1260 | */ |
||
1261 | 2 | public function prepend($string) |
|
1265 | |||
1266 | /** |
||
1267 | * Returns a new string with the prefix $substring removed, if present. |
||
1268 | * |
||
1269 | * @param string $substring The prefix to remove |
||
1270 | * |
||
1271 | * @return Stringy Object having a $str without the prefix $substring |
||
1272 | */ |
||
1273 | 12 | View Code Duplication | public function removeLeft($substring) |
1285 | |||
1286 | /** |
||
1287 | * Returns a new string with the suffix $substring removed, if present. |
||
1288 | * |
||
1289 | * @param string $substring The suffix to remove |
||
1290 | * |
||
1291 | * @return Stringy Object having a $str without the suffix $substring |
||
1292 | */ |
||
1293 | 12 | View Code Duplication | public function removeRight($substring) |
1305 | |||
1306 | /** |
||
1307 | * Returns a repeated string given a multiplier. |
||
1308 | * |
||
1309 | * @param int $multiplier The number of times to repeat the string |
||
1310 | * |
||
1311 | * @return Stringy Object with a repeated str |
||
1312 | */ |
||
1313 | 7 | public function repeat($multiplier) |
|
1319 | |||
1320 | /** |
||
1321 | * Replaces all occurrences of $search in $str by $replacement. |
||
1322 | * |
||
1323 | * @param string $search The needle to search for |
||
1324 | * @param string $replacement The string to replace with |
||
1325 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
1326 | * |
||
1327 | * @return Stringy Object with the resulting $str after the replacements |
||
1328 | */ |
||
1329 | 28 | View Code Duplication | public function replace($search, $replacement, $caseSensitive = true) |
1330 | { |
||
1331 | 28 | if ($caseSensitive) { |
|
1332 | 21 | $return = UTF8::str_replace($search, $replacement, $this->str); |
|
1333 | } else { |
||
1334 | 7 | $return = UTF8::str_ireplace($search, $replacement, $this->str); |
|
1335 | } |
||
1336 | |||
1337 | 28 | return static::create($return); |
|
1338 | } |
||
1339 | |||
1340 | /** |
||
1341 | * Replaces all occurrences of $search in $str by $replacement. |
||
1342 | * |
||
1343 | * @param array $search The elements to search for |
||
1344 | * @param string|array $replacement The string to replace with |
||
1345 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
1346 | * |
||
1347 | * @return Stringy Object with the resulting $str after the replacements |
||
1348 | */ |
||
1349 | 30 | View Code Duplication | public function replaceAll(array $search, $replacement, $caseSensitive = true) |
1350 | { |
||
1351 | 30 | if ($caseSensitive) { |
|
1352 | 23 | $return = UTF8::str_replace($search, $replacement, $this->str); |
|
1353 | } else { |
||
1354 | 7 | $return = UTF8::str_ireplace($search, $replacement, $this->str); |
|
1355 | } |
||
1356 | |||
1357 | 30 | return static::create($return); |
|
1358 | } |
||
1359 | |||
1360 | /** |
||
1361 | * Replaces all occurrences of $search from the beginning of string with $replacement |
||
1362 | * |
||
1363 | * @param string $search |
||
1364 | * @param string $replacement |
||
1365 | * |
||
1366 | * @return Stringy Object with the resulting $str after the replacements |
||
1367 | */ |
||
1368 | 16 | public function replaceBeginning($search, $replacement) |
|
1374 | |||
1375 | /** |
||
1376 | * Replaces all occurrences of $search from the ending of string with $replacement |
||
1377 | * |
||
1378 | * @param string $search |
||
1379 | * @param string $replacement |
||
1380 | * |
||
1381 | * @return Stringy Object with the resulting $str after the replacements |
||
1382 | */ |
||
1383 | 16 | public function replaceEnding($search, $replacement) |
|
1389 | |||
1390 | /** |
||
1391 | * Returns a reversed string. A multibyte version of strrev(). |
||
1392 | * |
||
1393 | * @return Stringy Object with a reversed $str |
||
1394 | */ |
||
1395 | 5 | public function reverse() |
|
1401 | |||
1402 | /** |
||
1403 | * Truncates the string to a given length, while ensuring that it does not |
||
1404 | * split words. If $substring is provided, and truncating occurs, the |
||
1405 | * string is further truncated so that the substring may be appended without |
||
1406 | * exceeding the desired length. |
||
1407 | * |
||
1408 | * @param int $length Desired length of the truncated string |
||
1409 | * @param string $substring The substring to append if it can fit |
||
1410 | * |
||
1411 | * @return Stringy Object with the resulting $str after truncating |
||
1412 | */ |
||
1413 | 23 | public function safeTruncate($length, $substring = '') |
|
1414 | { |
||
1415 | 23 | $stringy = static::create($this->str, $this->encoding); |
|
1416 | 23 | if ($length >= $stringy->length()) { |
|
1417 | 4 | return $stringy; |
|
1418 | } |
||
1419 | |||
1420 | // Need to further trim the string so we can append the substring |
||
1421 | 19 | $encoding = $stringy->encoding; |
|
1422 | 19 | $substringLength = UTF8::strlen($substring, $encoding); |
|
1423 | 19 | $length -= $substringLength; |
|
1424 | |||
1425 | 19 | $truncated = UTF8::substr($stringy->str, 0, $length, $encoding); |
|
1426 | |||
1442 | |||
1443 | /** |
||
1444 | * A multibyte string shuffle function. It returns a string with its |
||
1445 | * characters in random order. |
||
1446 | * |
||
1447 | * @return Stringy Object with a shuffled $str |
||
1448 | */ |
||
1449 | 3 | public function shuffle() |
|
1455 | |||
1456 | /** |
||
1457 | * Converts the string into an URL slug. This includes replacing non-ASCII |
||
1458 | * characters with their closest ASCII equivalents, removing remaining |
||
1459 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
||
1460 | * $replacement. The replacement defaults to a single dash, and the string |
||
1461 | * is also converted to lowercase. |
||
1462 | * |
||
1463 | * @param string $replacement The string used to replace whitespace |
||
1464 | * @param string $language The language for the url |
||
1465 | * @param bool $strToLower string to lower |
||
1466 | * |
||
1467 | * @return Stringy Object whose $str has been converted to an URL slug |
||
1468 | */ |
||
1469 | 15 | public function slugify($replacement = '-', $language = 'de', $strToLower = true) |
|
1475 | |||
1476 | /** |
||
1477 | * Remove css media-queries. |
||
1478 | * |
||
1479 | * @return Stringy |
||
1480 | */ |
||
1481 | 1 | public function stripeCssMediaQueries() |
|
1487 | |||
1488 | /** |
||
1489 | * Remove empty html-tag. |
||
1490 | * |
||
1491 | * e.g.: <tag></tag> |
||
1492 | * |
||
1493 | * @return Stringy |
||
1494 | */ |
||
1495 | 1 | public function stripeEmptyHtmlTags() |
|
1501 | |||
1502 | /** |
||
1503 | * Converts the string into an valid UTF-8 string. |
||
1504 | * |
||
1505 | * @return Stringy |
||
1506 | */ |
||
1507 | 1 | public function utf8ify() |
|
1511 | |||
1512 | /** |
||
1513 | * escape html |
||
1514 | * |
||
1515 | * @return Stringy |
||
1516 | */ |
||
1517 | 6 | public function escape() |
|
1527 | |||
1528 | /** |
||
1529 | * Create an extract from a text, so if the search-string was found, it will be centered in the output. |
||
1530 | * |
||
1531 | * @param string $search |
||
1532 | * @param int|null $length |
||
1533 | * @param string $ellipsis |
||
1534 | * |
||
1535 | * @return Stringy |
||
1536 | */ |
||
1537 | 1 | public function extractText($search = '', $length = null, $ellipsis = '...') |
|
1654 | |||
1655 | |||
1656 | /** |
||
1657 | * remove xss from html |
||
1658 | * |
||
1659 | * @return Stringy |
||
1660 | */ |
||
1661 | 6 | public function removeXss() |
|
1673 | |||
1674 | /** |
||
1675 | * remove html-break [br | \r\n | \r | \n | ...] |
||
1676 | * |
||
1677 | * @param string $replacement |
||
1678 | * |
||
1679 | * @return Stringy |
||
1680 | */ |
||
1681 | 6 | public function removeHtmlBreak($replacement = '') |
|
1687 | |||
1688 | /** |
||
1689 | * remove html |
||
1690 | * |
||
1691 | * @param $allowableTags |
||
1692 | * |
||
1693 | * @return Stringy |
||
1694 | */ |
||
1695 | 6 | public function removeHtml($allowableTags = null) |
|
1701 | |||
1702 | /** |
||
1703 | * Returns the substring beginning at $start, and up to, but not including |
||
1704 | * the index specified by $end. If $end is omitted, the function extracts |
||
1705 | * the remaining string. If $end is negative, it is computed from the end |
||
1706 | * of the string. |
||
1707 | * |
||
1708 | * @param int $start Initial index from which to begin extraction |
||
1709 | * @param int $end Optional index at which to end extraction |
||
1710 | * |
||
1711 | * @return Stringy Object with its $str being the extracted substring |
||
1712 | */ |
||
1713 | 18 | public function slice($start, $end = null) |
|
1729 | |||
1730 | /** |
||
1731 | * Splits the string with the provided regular expression, returning an |
||
1732 | * array of Stringy objects. An optional integer $limit will truncate the |
||
1733 | * results. |
||
1734 | * |
||
1735 | * @param string $pattern The regex with which to split the string |
||
1736 | * @param int $limit Optional maximum number of results to return |
||
1737 | * |
||
1738 | * @return Stringy[] An array of Stringy objects |
||
1739 | */ |
||
1740 | 19 | public function split($pattern, $limit = null) |
|
1774 | |||
1775 | /** |
||
1776 | * Surrounds $str with the given substring. |
||
1777 | * |
||
1778 | * @param string $substring The substring to add to both sides |
||
1779 | * |
||
1780 | * @return Stringy Object whose $str had the substring both prepended and |
||
1781 | * appended |
||
1782 | */ |
||
1783 | 5 | public function surround($substring) |
|
1789 | |||
1790 | /** |
||
1791 | * Returns a case swapped version of the string. |
||
1792 | * |
||
1793 | * @return Stringy Object whose $str has each character's case swapped |
||
1794 | */ |
||
1795 | 5 | public function swapCase() |
|
1803 | |||
1804 | /** |
||
1805 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
||
1806 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
||
1807 | * equivalents. |
||
1808 | * |
||
1809 | * @return Stringy Object whose $str has those characters removed |
||
1810 | */ |
||
1811 | 4 | public function tidy() |
|
1817 | |||
1818 | /** |
||
1819 | * Returns a trimmed string with the first letter of each word capitalized. |
||
1820 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
1821 | * capitalized. |
||
1822 | * |
||
1823 | * @param array $ignore An array of words not to capitalize |
||
1824 | * |
||
1825 | * @return Stringy Object with a titleized $str |
||
1826 | */ |
||
1827 | 5 | public function titleize($ignore = null) |
|
1848 | |||
1849 | /** |
||
1850 | * Converts all characters in the string to lowercase. An alias for PHP's |
||
1851 | * UTF8::strtolower(). |
||
1852 | * |
||
1853 | * @return Stringy Object with all characters of $str being lowercase |
||
1854 | */ |
||
1855 | 27 | public function toLowerCase() |
|
1861 | |||
1862 | /** |
||
1863 | * Returns true if the string is base64 encoded, false otherwise. |
||
1864 | * |
||
1865 | * @return bool Whether or not $str is base64 encoded |
||
1866 | */ |
||
1867 | 7 | public function isBase64() |
|
1871 | |||
1872 | /** |
||
1873 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
||
1874 | * replaced with their closest ASCII counterparts, and the rest are removed |
||
1875 | * unless instructed otherwise. |
||
1876 | * |
||
1877 | * @param $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance</p> |
||
1878 | * |
||
1879 | * @return Stringy Object whose $str contains only ASCII characters |
||
1880 | */ |
||
1881 | 16 | public function toAscii($strict = false) |
|
1887 | |||
1888 | /** |
||
1889 | * Returns a boolean representation of the given logical string value. |
||
1890 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
1891 | * 'off', and 'no' will return false. In all instances, case is ignored. |
||
1892 | * For other numeric strings, their sign will determine the return value. |
||
1893 | * In addition, blank strings consisting of only whitespace will return |
||
1894 | * false. For all other strings, the return value is a result of a |
||
1895 | * boolean cast. |
||
1896 | * |
||
1897 | * @return bool A boolean value for the string |
||
1898 | */ |
||
1899 | 15 | public function toBoolean() |
|
1921 | |||
1922 | /** |
||
1923 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
||
1924 | * |
||
1925 | * @return string |
||
1926 | */ |
||
1927 | 968 | public function toString() |
|
1931 | |||
1932 | /** |
||
1933 | * Converts each tab in the string to some number of spaces, as defined by |
||
1934 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
||
1935 | * |
||
1936 | * @param int $tabLength Number of spaces to replace each tab with |
||
1937 | * |
||
1938 | * @return Stringy Object whose $str has had tabs switched to spaces |
||
1939 | */ |
||
1940 | 6 | View Code Duplication | public function toSpaces($tabLength = 4) |
1947 | |||
1948 | /** |
||
1949 | * Converts each occurrence of some consecutive number of spaces, as |
||
1950 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
||
1951 | * are converted to a tab. |
||
1952 | * |
||
1953 | * @param int $tabLength Number of spaces to replace with a tab |
||
1954 | * |
||
1955 | * @return Stringy Object whose $str has had spaces switched to tabs |
||
1956 | */ |
||
1957 | 5 | View Code Duplication | public function toTabs($tabLength = 4) |
1964 | |||
1965 | /** |
||
1966 | * Converts the first character of each word in the string to uppercase. |
||
1967 | * |
||
1968 | * @return Stringy Object with all characters of $str being title-cased |
||
1969 | */ |
||
1970 | 5 | public function toTitleCase() |
|
1977 | |||
1978 | /** |
||
1979 | * Converts all characters in the string to uppercase. An alias for PHP's |
||
1980 | * UTF8::strtoupper(). |
||
1981 | * |
||
1982 | * @return Stringy Object with all characters of $str being uppercase |
||
1983 | */ |
||
1984 | 5 | public function toUpperCase() |
|
1990 | |||
1991 | /** |
||
1992 | * Returns a string with whitespace removed from the start of the string. |
||
1993 | * Supports the removal of unicode whitespace. Accepts an optional |
||
1994 | * string of characters to strip instead of the defaults. |
||
1995 | * |
||
1996 | * @param string $chars Optional string of characters to strip |
||
1997 | * |
||
1998 | * @return Stringy Object with a trimmed $str |
||
1999 | */ |
||
2000 | 13 | View Code Duplication | public function trimLeft($chars = null) |
2010 | |||
2011 | /** |
||
2012 | * Returns a string with whitespace removed from the end of the string. |
||
2013 | * Supports the removal of unicode whitespace. Accepts an optional |
||
2014 | * string of characters to strip instead of the defaults. |
||
2015 | * |
||
2016 | * @param string $chars Optional string of characters to strip |
||
2017 | * |
||
2018 | * @return Stringy Object with a trimmed $str |
||
2019 | */ |
||
2020 | 13 | View Code Duplication | public function trimRight($chars = null) |
2030 | |||
2031 | /** |
||
2032 | * Truncates the string to a given length. If $substring is provided, and |
||
2033 | * truncating occurs, the string is further truncated so that the substring |
||
2034 | * may be appended without exceeding the desired length. |
||
2035 | * |
||
2036 | * @param int $length Desired length of the truncated string |
||
2037 | * @param string $substring The substring to append if it can fit |
||
2038 | * |
||
2039 | * @return Stringy Object with the resulting $str after truncating |
||
2040 | */ |
||
2041 | 22 | View Code Duplication | public function truncate($length, $substring = '') |
2057 | |||
2058 | /** |
||
2059 | * Returns a lowercase and trimmed string separated by underscores. |
||
2060 | * Underscores are inserted before uppercase characters (with the exception |
||
2061 | * of the first character of the string), and in place of spaces as well as |
||
2062 | * dashes. |
||
2063 | * |
||
2064 | * @return Stringy Object with an underscored $str |
||
2065 | */ |
||
2066 | 16 | public function underscored() |
|
2070 | |||
2071 | /** |
||
2072 | * Returns an UpperCamelCase version of the supplied string. It trims |
||
2073 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
||
2074 | * and underscores, and removes spaces, dashes, underscores. |
||
2075 | * |
||
2076 | * @return Stringy Object with $str in UpperCamelCase |
||
2077 | */ |
||
2078 | 13 | public function upperCamelize() |
|
2082 | |||
2083 | /** |
||
2084 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
2085 | * capitalizes letters following digits, spaces, dashes and underscores, |
||
2086 | * and removes spaces, dashes, as well as underscores. |
||
2087 | * |
||
2088 | * @return Stringy Object with $str in camelCase |
||
2089 | */ |
||
2090 | 32 | public function camelize() |
|
2118 | |||
2119 | /** |
||
2120 | * Convert a string to e.g.: "snake_case" |
||
2121 | * |
||
2122 | * @return Stringy Object with $str in snake_case |
||
2123 | */ |
||
2124 | 20 | public function snakeize() |
|
2167 | |||
2168 | /** |
||
2169 | * Converts the first character of the string to lower case. |
||
2170 | * |
||
2171 | * @return Stringy Object with the first character of $str being lower case |
||
2172 | */ |
||
2173 | 37 | View Code Duplication | public function lowerCaseFirst() |
2182 | |||
2183 | /** |
||
2184 | * Shorten the string after $length, but also after the next word. |
||
2185 | * |
||
2186 | * @param int $length |
||
2187 | * @param string $strAddOn |
||
2188 | * |
||
2189 | * @return Stringy |
||
2190 | */ |
||
2191 | 4 | public function shortenAfterWord($length, $strAddOn = '...') |
|
2197 | |||
2198 | /** |
||
2199 | * Line-Wrap the string after $limit, but also after the next word. |
||
2200 | * |
||
2201 | * @param int $limit |
||
2202 | * |
||
2203 | * @return Stringy |
||
2204 | */ |
||
2205 | 1 | public function lineWrapAfterWord($limit) |
|
2217 | |||
2218 | /** |
||
2219 | * Gets the substring after the first occurrence of a separator. |
||
2220 | * If no match is found returns new empty Stringy object. |
||
2221 | * |
||
2222 | * @param string $separator |
||
2223 | * |
||
2224 | * @return Stringy |
||
2225 | */ |
||
2226 | 1 | View Code Duplication | public function afterFirst($separator) |
2242 | |||
2243 | /** |
||
2244 | * Gets the substring after the last occurrence of a separator. |
||
2245 | * If no match is found returns new empty Stringy object. |
||
2246 | * |
||
2247 | * @param string $separator |
||
2248 | * |
||
2249 | * @return Stringy |
||
2250 | */ |
||
2251 | 1 | public function afterLast($separator) |
|
2268 | |||
2269 | /** |
||
2270 | * Gets the substring before the first occurrence of a separator. |
||
2271 | * If no match is found returns new empty Stringy object. |
||
2272 | * |
||
2273 | * @param string $separator |
||
2274 | * |
||
2275 | * @return Stringy |
||
2276 | */ |
||
2277 | 1 | View Code Duplication | public function beforeFirst($separator) |
2294 | |||
2295 | /** |
||
2296 | * Gets the substring before the last occurrence of a separator. |
||
2297 | * If no match is found returns new empty Stringy object. |
||
2298 | * |
||
2299 | * @param string $separator |
||
2300 | * |
||
2301 | * @return Stringy |
||
2302 | */ |
||
2303 | 1 | View Code Duplication | public function beforeLast($separator) |
2320 | |||
2321 | /** |
||
2322 | * Returns the string with the first letter of each word capitalized, |
||
2323 | * except for when the word is a name which shouldn't be capitalized. |
||
2324 | * |
||
2325 | * @return Stringy Object with $str capitalized |
||
2326 | */ |
||
2327 | 39 | public function capitalizePersonalName() |
|
2335 | |||
2336 | /** |
||
2337 | * @param string $word |
||
2338 | * |
||
2339 | * @return string |
||
2340 | */ |
||
2341 | 7 | private function capitalizeWord($word) |
|
2351 | |||
2352 | /** |
||
2353 | * Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius"). |
||
2354 | * |
||
2355 | * @param string $names |
||
2356 | * @param string $delimiter |
||
2357 | * |
||
2358 | * @return string |
||
2359 | */ |
||
2360 | 39 | private function capitalizePersonalNameByDelimiter($names, $delimiter) |
|
2436 | } |
||
2437 |
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: