1 | <?php |
||
6 | class RuleHelper |
||
7 | { |
||
8 | /** |
||
9 | * Method that parses the option variable and converts it into an array |
||
10 | * You can pass anything to a validator like: |
||
11 | * - a query string: 'min=3&max=5' |
||
12 | * - a JSON string: '{"min":3,"max":5}' |
||
13 | * - a CSV string: '5,true' (for this scenario the 'optionsIndexMap' property is required) |
||
14 | * |
||
15 | * @param mixed $options |
||
16 | * |
||
17 | * @return array |
||
18 | * @throws \InvalidArgumentException |
||
19 | */ |
||
20 | 166 | public static function normalizeOptions($options, array $optionsIndexMap = []) |
|
51 | |||
52 | /** |
||
53 | * Converts a HTTP query string to an array |
||
54 | * |
||
55 | * @param $str |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | 6 | public static function parseHttpQueryString(string $str) |
|
65 | |||
66 | /** |
||
67 | * Converts 'true' and 'false' strings to TRUE and FALSE |
||
68 | * |
||
69 | * @param $arr |
||
70 | * |
||
71 | * @return bool|array |
||
72 | */ |
||
73 | 9 | public static function convertBooleanStrings($arr) |
|
87 | |||
88 | |||
89 | /** |
||
90 | * Parses a CSV string and converts the result into an "options" array |
||
91 | * (an associative array that contains the options for the validation rule) |
||
92 | * |
||
93 | * @param $str |
||
94 | * |
||
95 | * @param array $optionsIndexMap |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | 3 | public static function parseCsvString($str, array $optionsIndexMap = []) |
|
100 | { |
||
101 | 3 | if (! isset($optionsIndexMap) || ! is_array($optionsIndexMap) || empty($optionsIndexMap)) { |
|
102 | throw new \InvalidArgumentException( |
||
103 | '`$optionsIndexMap` argument must be provided for CSV-type parameters' |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | 3 | $options = explode(',', $str); |
|
108 | 3 | $result = []; |
|
109 | 3 | foreach ($options as $k => $v) { |
|
110 | 3 | if (! isset($optionsIndexMap[$k])) { |
|
111 | throw new \InvalidArgumentException(sprintf( |
||
112 | '`$optionsIndexMap` for the validator is missing the %s index', |
||
113 | $k |
||
114 | )); |
||
115 | } |
||
116 | 3 | $result[$optionsIndexMap[$k]] = $v; |
|
117 | } |
||
118 | |||
119 | 3 | return static::convertBooleanStrings($result); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Checks if an array is associative (ie: the keys are not numbers in sequence) |
||
124 | * |
||
125 | * @param array $arr |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | 53 | public static function arrayIsAssoc($arr) |
|
133 | |||
134 | 4 | public static function normalizeFileSize($size) |
|
148 | |||
149 | |||
150 | |||
151 | 15 | public static function normalizeImageRatio($ratio) |
|
164 | } |
||
165 |