44
55namespace Matronator \Parsem ;
66
7+ use Symfony \Component \Yaml \Yaml ;
8+
79class Filters
810{
911 public const ENCODING = 'UTF-8 ' ;
@@ -15,8 +17,10 @@ class Filters
1517 'camelCase ' , 'snakeCase ' , 'kebabCase ' , 'pascalCase ' , 'titleCase ' ,
1618 'length ' ,
1719 'reverse ' , 'random ' , 'shuffle ' ,
18- 'truncate ' ,
19- 'escape ' , 'unescape ' , 'hash ' ];
20+ 'truncate ' , 'trim ' ,
21+ 'url ' , 'stripTags ' , 'nl2br ' ,
22+ 'escape ' , 'unescape ' , 'hash ' , 'rot13 ' , 'encode ' , 'decode ' ,
23+ ];
2024
2125 public static function upper (string $ string ): string
2226 {
@@ -40,14 +44,20 @@ public static function lowerFirst(string $string): string
4044 return $ fc . mb_substr ($ string , 1 , null , static ::ENCODING );
4145 }
4246
43- public static function first (string $ string ): string
47+ public static function first (string | array $ value ): mixed
4448 {
45- return mb_substr ($ string , 0 , 1 , static ::ENCODING );
49+ if (is_array ($ value )) {
50+ return reset ($ value );
51+ }
52+ return mb_substr ($ value , 0 , 1 , static ::ENCODING );
4653 }
4754
48- public static function last (string $ string ): string
55+ public static function last (string | array $ value ): mixed
4956 {
50- return mb_substr ($ string , -1 , 1 , static ::ENCODING );
57+ if (is_array ($ value )) {
58+ return end ($ value );
59+ }
60+ return mb_substr ($ value , -1 , 1 , static ::ENCODING );
5161 }
5262
5363 public static function camelCase (string $ string ): string
@@ -87,8 +97,17 @@ public static function pascalCase(string $string): string
8797
8898 public static function titleCase (string $ string ): string
8999 {
90- $ string = str_replace (['- ' , '_ ' ], ' ' , $ string );
100+ // $string = str_replace(['-', '_'], ' ', $string);
101+ // $split = preg_split('/(?=[A-Z])/', $string);
102+ // $string = implode(' ', $split);
103+ // $string = ucwords($string);
104+
105+ // $string = static::camelCase($string);
106+ // $string = preg_replace('/([a-z])([A-Z])/', '$1 $2', $string);
107+ $ string = str_replace (['_ ' ], ' ' , $ string );
108+ $ string = str_replace (['- ' ], ' 🜛 ' , $ string );
91109 $ string = ucwords ($ string );
110+ $ string = str_replace ([' 🜛 ' ], '- ' , $ string );
92111 return $ string ;
93112 }
94113
@@ -102,7 +121,7 @@ public static function reverse(array|string $value): array|string
102121 return is_string ($ value ) ? strrev ($ value ) : array_reverse ($ value );
103122 }
104123
105- public static function random (array |string $ value ): array | string
124+ public static function random (array |string $ value ): mixed
106125 {
107126 if (is_string ($ value )) {
108127 return $ value [rand (0 , strlen ($ value ) - 1 )];
@@ -113,10 +132,12 @@ public static function random(array|string $value): array|string
113132
114133 public static function shuffle (array |string $ value ): array |string
115134 {
116- $ array = is_string ($ value ) ? str_split ($ value ) : $ value ;
135+ if (is_string ($ value )) {
136+ return str_shuffle ($ value );
137+ }
138+ $ array = clone $ value ;
117139 shuffle ($ array );
118-
119- return $ value ;
140+ return $ array ;
120141 }
121142
122143 public static function truncate (string $ string , int $ length , string $ ending = '... ' ): string
@@ -127,4 +148,71 @@ public static function truncate(string $string, int $length, string $ending = '.
127148
128149 return mb_substr ($ string , 0 , $ length , static ::ENCODING ) . $ ending ;
129150 }
151+
152+ public static function trim (string $ string , string $ side = 'both ' , string $ characters = " \n\r\t\v\0" ): string
153+ {
154+ if ($ side === 'left ' ) {
155+ return ltrim ($ string , $ characters , );
156+ } else if ($ side === 'right ' ) {
157+ return rtrim ($ string , $ characters );
158+ }
159+
160+ return trim ($ string , $ characters );
161+ }
162+
163+ public static function url (string $ string ): string
164+ {
165+ return rawurlencode ($ string );
166+ }
167+
168+ public static function stripTags (string $ string ): string
169+ {
170+ return strip_tags ($ string );
171+ }
172+
173+ public static function nl2br (string $ string , bool $ xhtmlSyntax = false ): string
174+ {
175+ return nl2br ($ string , $ xhtmlSyntax );
176+ }
177+
178+ public static function escape (string $ string ): string
179+ {
180+ return htmlspecialchars ($ string , ENT_QUOTES , static ::ENCODING );
181+ }
182+
183+ public static function unescape (string $ string ): string
184+ {
185+ return html_entity_decode ($ string , ENT_QUOTES , static ::ENCODING );
186+ }
187+
188+ public static function hash (string $ string , string $ algorithm = 'md5 ' , string |null $ secret = null ): string
189+ {
190+ if ($ secret ) {
191+ return hash_hmac ($ algorithm , $ string , $ secret );
192+ }
193+ return hash ($ algorithm , $ string );
194+ }
195+
196+ public static function rot13 (string $ string ): string
197+ {
198+ return str_rot13 ($ string );
199+ }
200+
201+ public static function encode (string $ string , string $ encoding = 'base64 ' ): string
202+ {
203+ switch ($ encoding ) {
204+ case 'base64 ' :
205+ return base64_encode ($ string );
206+ case 'hex ' :
207+ return bin2hex ($ string );
208+ case 'url ' :
209+ return rawurlencode ($ string );
210+ case 'json ' :
211+ return json_encode ($ string );
212+ case 'yaml ' :
213+ return Yaml::dump ($ string );
214+ default :
215+ return $ string ;
216+ }
217+ }
130218}
0 commit comments