1 | <?php |
||
26 | class Strings |
||
27 | { |
||
28 | /** |
||
29 | * Coerces a value into a string or throws an exception. |
||
30 | * |
||
31 | * @param mixed $var - The value to coerce |
||
32 | * @return string The converted string |
||
33 | * @throws \InvalidArgumentException if the value could not be coerced |
||
34 | */ |
||
35 | 2 | public static function coerce($var): string |
|
36 | { |
||
37 | 2 | if ($var === null || is_scalar($var) || is_callable([$var, '__toString'])) { |
|
38 | 1 | return (string) $var; |
|
39 | } |
||
40 | 1 | throw new \InvalidArgumentException("Could not convert to string: " . gettype($var)); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Returns a filter that coerces a value to a string. |
||
45 | * |
||
46 | * @return callable The created filter |
||
47 | */ |
||
48 | 1 | public static function toString(): callable |
|
52 | |||
53 | /** |
||
54 | * Returns a new lowercasing filter. |
||
55 | * |
||
56 | * @return \Closure The created filter |
||
57 | */ |
||
58 | 1 | public static function lowerCase(): \Closure |
|
64 | |||
65 | /** |
||
66 | * Returns a new uppercasing filter. |
||
67 | * |
||
68 | * @return \Closure The created filter |
||
69 | */ |
||
70 | 1 | public static function upperCase(): \Closure |
|
76 | |||
77 | /** |
||
78 | * Returns a new uppercasing filter. |
||
79 | * |
||
80 | * @return \Closure The created filter |
||
81 | */ |
||
82 | 1 | public static function titleCase(): \Closure |
|
88 | |||
89 | /** |
||
90 | * Returns a new whitespace trimming filter. |
||
91 | * |
||
92 | * @return \Closure The created filter |
||
93 | */ |
||
94 | 1 | public static function trim(): \Closure |
|
100 | |||
101 | /** |
||
102 | * Returns a new substring filter. |
||
103 | * |
||
104 | * @param int $length The maximum string length |
||
105 | * @return \Closure The created filter |
||
106 | */ |
||
107 | 1 | public static function cut(int $length): \Closure |
|
113 | |||
114 | /** |
||
115 | * Creates a new search and replace filter. |
||
116 | * |
||
117 | * @param string $search The value to find |
||
118 | * @param string $replacement The value to use as a replacement |
||
119 | * @return \Closure The created filter |
||
120 | */ |
||
121 | 1 | public static function replace(string $search, string $replacement): \Closure |
|
127 | |||
128 | /** |
||
129 | * Creates a new regular expression filter. |
||
130 | * |
||
131 | * @param string $pattern The search pattern |
||
132 | * @param string $replacement The value to use as a replacement |
||
133 | * @return \Closure The created filter |
||
134 | */ |
||
135 | public static function regex(string $pattern, string $replacement): \Closure |
||
141 | |||
142 | /** |
||
143 | * Creates a new filter that removes non-alphanumeric characters. |
||
144 | * |
||
145 | * @return \Closure The created filter |
||
146 | */ |
||
147 | 1 | public static function alnum(): \Closure |
|
151 | |||
152 | /** |
||
153 | * Creates a new filter that removes non-alphabetic characters. |
||
154 | * |
||
155 | * @return \Closure The created filter |
||
156 | */ |
||
157 | 1 | public static function alpha(): \Closure |
|
161 | |||
162 | /** |
||
163 | * Creates a new filter that removes non-digit characters. |
||
164 | * |
||
165 | * @return \Closure The created filter |
||
166 | */ |
||
167 | 1 | public static function numeric(): \Closure |
|
171 | |||
172 | /** |
||
173 | * Creates a new filter that turns any newlines into UNIX-style newlines. |
||
174 | * |
||
175 | * @return \Closure The created filter |
||
176 | */ |
||
177 | 1 | public static function unixNewlines(): \Closure |
|
181 | |||
182 | /** |
||
183 | * Creates a new filter that turns multiple newlines into two. |
||
184 | * |
||
185 | * Meant to be run after `unixNewlines`. |
||
186 | * |
||
187 | * @return \Closure The created filter |
||
188 | */ |
||
189 | 1 | public static function compactNewlines(): \Closure |
|
193 | } |
||
194 |