1 | <?php |
||||
2 | /** |
||||
3 | * Retour plugin for Craft CMS |
||||
4 | * |
||||
5 | * Retour allows you to intelligently redirect legacy URLs, so that you don't |
||||
6 | * lose SEO value when rebuilding & restructuring a website |
||||
7 | * |
||||
8 | * @link https://nystudio107.com/ |
||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
9 | * @copyright Copyright (c) 2018 nystudio107 |
||||
0 ignored issues
–
show
|
|||||
10 | */ |
||||
0 ignored issues
–
show
|
|||||
11 | |||||
12 | namespace nystudio107\retour\helpers; |
||||
13 | |||||
14 | use Stringy\Stringy; |
||||
15 | use function function_exists; |
||||
16 | |||||
17 | /** |
||||
0 ignored issues
–
show
|
|||||
18 | * @author nystudio107 |
||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||
19 | * @package Retour |
||||
0 ignored issues
–
show
|
|||||
20 | * @since 3.0.0 |
||||
0 ignored issues
–
show
|
|||||
21 | */ |
||||
0 ignored issues
–
show
|
|||||
22 | class Text |
||||
23 | { |
||||
24 | // Constants |
||||
25 | // ========================================================================= |
||||
26 | |||||
27 | // Public Static Methods |
||||
28 | // ========================================================================= |
||||
29 | |||||
30 | /** |
||||
31 | * Truncates the string to a given length. If $substring is provided, and |
||||
32 | * truncating occurs, the string is further truncated so that the substring |
||||
33 | * may be appended without exceeding the desired length. |
||||
34 | * |
||||
35 | * @param string $string The string to truncate |
||||
0 ignored issues
–
show
|
|||||
36 | * @param int $length Desired length of the truncated string |
||||
0 ignored issues
–
show
|
|||||
37 | * @param string $substring The substring to append if it can fit |
||||
38 | * |
||||
39 | * @return string with the resulting $str after truncating |
||||
40 | */ |
||||
41 | public static function truncate(string $string, int $length, string $substring = '…'): string |
||||
42 | { |
||||
43 | $result = $string; |
||||
44 | |||||
45 | if (!empty($string)) { |
||||
46 | $string = strip_tags($string); |
||||
47 | $result = (string)Stringy::create($string)->truncate($length, $substring); |
||||
48 | } |
||||
49 | |||||
50 | return $result; |
||||
51 | } |
||||
52 | |||||
53 | /** |
||||
54 | * Truncates the string to a given length, while ensuring that it does not |
||||
55 | * split words. If $substring is provided, and truncating occurs, the |
||||
56 | * string is further truncated so that the substring may be appended without |
||||
57 | * exceeding the desired length. |
||||
58 | * |
||||
59 | * @param string $string The string to truncate |
||||
0 ignored issues
–
show
|
|||||
60 | * @param int $length Desired length of the truncated string |
||||
0 ignored issues
–
show
|
|||||
61 | * @param string $substring The substring to append if it can fit |
||||
62 | * |
||||
63 | * @return string with the resulting $str after truncating |
||||
64 | */ |
||||
65 | public static function truncateOnWord(string $string, int $length, string $substring = '…'): string |
||||
66 | { |
||||
67 | $result = $string; |
||||
68 | |||||
69 | if (!empty($string)) { |
||||
70 | $string = strip_tags($string); |
||||
71 | $result = (string)Stringy::create($string)->safeTruncate($length, $substring); |
||||
72 | } |
||||
73 | |||||
74 | return $result; |
||||
75 | } |
||||
76 | |||||
77 | /** |
||||
78 | * Clean up the passed in text by converting it to UTF-8, stripping tags, |
||||
79 | * removing whitespace, and decoding HTML entities |
||||
80 | * |
||||
81 | * @param string $text |
||||
0 ignored issues
–
show
|
|||||
82 | * |
||||
83 | * @return string |
||||
84 | */ |
||||
85 | public static function cleanupText(string $text): string |
||||
86 | { |
||||
87 | if (empty($text)) { |
||||
88 | return ''; |
||||
89 | } |
||||
90 | // Convert to UTF-8 |
||||
91 | if (function_exists('iconv')) { |
||||
92 | $text = iconv(mb_detect_encoding($text, mb_detect_order(), true), 'UTF-8//IGNORE', $text); |
||||
0 ignored issues
–
show
It seems like
mb_detect_order() can also be of type true ; however, parameter $encodings of mb_detect_encoding() does only seem to accept array|null|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
93 | } else { |
||||
94 | ini_set('mbstring.substitute_character', 'none'); |
||||
95 | $text = mb_convert_encoding($text, 'UTF-8', 'UTF-8'); |
||||
96 | } |
||||
97 | // Strip HTML tags |
||||
98 | $text = strip_tags($text); |
||||
0 ignored issues
–
show
It seems like
$text can also be of type array ; however, parameter $string of strip_tags() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
99 | // Remove excess whitespace |
||||
100 | $text = preg_replace('/\s{2,}/u', ' ', $text); |
||||
101 | // Decode any HTML entities |
||||
102 | $text = html_entity_decode($text); |
||||
103 | |||||
104 | return $text; |
||||
105 | } |
||||
106 | |||||
107 | // Protected Static Methods |
||||
108 | // ========================================================================= |
||||
109 | } |
||||
110 |