@@ -8,7 +8,15 @@ class Filters
88{
99 public const ENCODING = 'UTF-8 ' ;
1010
11- public const GLOBAL_FILTERS = ['upper ' , 'lower ' , 'upperFirst ' , 'lowerFirst ' , 'first ' , 'last ' , 'camelCase ' , 'snakeCase ' , 'kebabCase ' , 'pascalCase ' , 'titleCase ' , 'length ' , 'reverse ' , 'random ' , 'truncate ' ];
11+ public const GLOBAL_FILTERS = [
12+ 'upper ' , 'lower ' ,
13+ 'upperFirst ' , 'lowerFirst ' ,
14+ 'first ' , 'last ' ,
15+ 'camelCase ' , 'snakeCase ' , 'kebabCase ' , 'pascalCase ' , 'titleCase ' ,
16+ 'length ' ,
17+ 'reverse ' , 'random ' , 'shuffle ' ,
18+ 'truncate ' ,
19+ 'escape ' , 'unescape ' , 'hash ' ];
1220
1321 public static function upper (string $ string ): string
1422 {
@@ -78,30 +86,39 @@ public static function titleCase(string $string): string
7886 return $ string ;
7987 }
8088
81- public static function length (string $ string ): int
89+ public static function length (array | string $ value ): int
8290 {
83- return mb_strlen ($ string , static ::ENCODING );
91+ return is_string ( $ value ) ? mb_strlen ($ value , static ::ENCODING ) : count ( $ value );
8492 }
8593
86- public static function reverse (string $ string ): string
94+ public static function reverse (array | string $ value ): array | string
8795 {
88- return strrev ($ string );
96+ return is_string ( $ value ) ? strrev ($ value ) : array_reverse ( $ value );
8997 }
9098
91- public static function random (array |string $ array ): mixed
99+ public static function random (array |string $ value ): array | string
92100 {
93- if (is_string ($ array ))
94- return $ array [rand (0 , strlen ($ array ) - 1 )];
95-
96- return $ array [array_rand ($ array )];
101+ if (is_string ($ value )) {
102+ return $ value [rand (0 , strlen ($ value ) - 1 )];
103+ }
104+
105+ return $ value [array_rand ($ value )];
106+ }
107+
108+ public static function shuffle (array |string $ value ): array |string
109+ {
110+ $ array = is_string ($ value ) ? str_split ($ value ) : $ value ;
111+ shuffle ($ array );
112+
113+ return $ value ;
97114 }
98115
99116 public static function truncate (string $ string , int $ length , string $ ending = '... ' ): string
100117 {
101118 if (mb_strlen ($ string , static ::ENCODING ) <= $ length ) {
102119 return $ string ;
103120 }
104-
121+
105122 return mb_substr ($ string , 0 , $ length , static ::ENCODING ) . $ ending ;
106123 }
107124}
0 commit comments