1 | <?php |
||
26 | class Casts |
||
27 | { |
||
28 | private static $truthy = ['yes', 'y', 'on', '1', 'true', 't']; |
||
29 | |||
30 | /** |
||
31 | * Creates a filter that casts values to booleans. |
||
32 | * |
||
33 | * @return \Closure The created filter |
||
34 | */ |
||
35 | public static function toBoolean(): \Closure |
||
36 | { |
||
37 | 1 | return function ($value) { |
|
38 | 1 | return is_bool($value) ? $value : |
|
39 | 1 | (is_scalar($value) ? |
|
40 | 1 | in_array(strtolower(trim(Strings::coerce($value))), Casts::$truthy, true) |
|
41 | 1 | : false); |
|
42 | 1 | }; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Creates a filter that casts values to integers. |
||
47 | * |
||
48 | * @return \Closure The created filter |
||
49 | */ |
||
50 | public static function toInteger(): \Closure |
||
56 | |||
57 | /** |
||
58 | * Creates a filter that casts values to floats. |
||
59 | * |
||
60 | * @return \Closure The created filter |
||
61 | */ |
||
62 | public static function toFloat(): \Closure |
||
68 | |||
69 | /** |
||
70 | * Creates a filter that casts values to arrays. |
||
71 | * |
||
72 | * @return callable The created filter |
||
73 | */ |
||
74 | 1 | public static function toArray(): callable |
|
78 | |||
79 | /** |
||
80 | * Creates a filter that casts values to a default if empty. |
||
81 | * |
||
82 | * @return \Closure The created filter |
||
83 | */ |
||
84 | public static function toDefault($default = null): \Closure |
||
90 | } |
||
91 |