|
4 | 4 |
|
5 | 5 | use Inhere\Validate\Exception\ValidateException;
|
6 | 6 | use function array_merge;
|
| 7 | +use function count; |
7 | 8 | use function is_int;
|
8 | 9 | use function is_numeric;
|
| 10 | +use function is_scalar; |
| 11 | +use function is_string; |
9 | 12 |
|
10 | 13 | /**
|
11 | 14 | * Class Valid - Simple Data Validator TODO
|
@@ -95,8 +98,126 @@ public static function getString(string $field, int $minLen = null, int $maxLen
|
95 | 98 | return 0;
|
96 | 99 | }
|
97 | 100 |
|
| 101 | + /** |
| 102 | + * @param string $field |
| 103 | + * @param int|null $min The value length must be >= $min |
| 104 | + * @param int|null $max The value length must be <= $min |
| 105 | + * @param array|null $default |
| 106 | + * |
| 107 | + * @return array |
| 108 | + */ |
98 | 109 | public static function getStrings(string $field, int $min = null, int $max = null, array $default = null): array
|
99 | 110 | {
|
| 111 | + if (!isset(self::$data[$field])) { |
| 112 | + if ($default === null) { |
| 113 | + throw new ValidateException($field, 'is required and must be string array'); |
| 114 | + } |
| 115 | + |
| 116 | + return $default; |
| 117 | + } |
| 118 | + |
| 119 | + if (!$val = self::$data[$field]) { |
| 120 | + if ($default === null) { |
| 121 | + throw new ValidateException($field, 'is required and must be string array'); |
| 122 | + } |
| 123 | + |
| 124 | + return $default; |
| 125 | + } |
| 126 | + |
| 127 | + if (is_scalar($val)) { |
| 128 | + return [(string)$val]; |
| 129 | + } |
| 130 | + |
| 131 | + return self::checkArrayValues($val, $field, $min, $max); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @param mixed $val |
| 136 | + * @param string $field |
| 137 | + * @param int|null $min |
| 138 | + * @param int|null $max |
| 139 | + * |
| 140 | + * @return array |
| 141 | + */ |
| 142 | + protected static function checkArrayValues($val, string $field, int $min = null, int $max = null): array |
| 143 | + { |
| 144 | + $arr = (array)$val; |
| 145 | + $len = count($arr); |
| 146 | + |
| 147 | + // check min and max value |
| 148 | + if ($min !== null && $len < $min) { |
| 149 | + throw new ValidateException($field, "length must be greater or equal to $min"); |
| 150 | + } |
| 151 | + if ($max !== null && $len > $max) { |
| 152 | + throw new ValidateException($field, "length must be less than or equal to $max"); |
| 153 | + } |
| 154 | + |
| 155 | + return $arr; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @param string $field |
| 160 | + * @param int|null $min |
| 161 | + * @param int|null $max |
| 162 | + * @param array|null $default |
| 163 | + * |
| 164 | + * @return array |
| 165 | + */ |
| 166 | + public static function getArray(string $field, int $min = null, int $max = null, array $default = null): array |
| 167 | + { |
| 168 | + if (!isset(self::$data[$field])) { |
| 169 | + if ($default === null) { |
| 170 | + throw new ValidateException($field, 'is required and must be array'); |
| 171 | + } |
| 172 | + |
| 173 | + return $default; |
| 174 | + } |
| 175 | + |
| 176 | + if (!$val = self::$data[$field]) { |
| 177 | + if ($default === null) { |
| 178 | + throw new ValidateException($field, 'is required and must be array'); |
| 179 | + } |
| 180 | + |
| 181 | + return $default; |
| 182 | + } |
| 183 | + |
| 184 | + if (is_scalar($val)) { |
| 185 | + return [$val]; |
| 186 | + } |
| 187 | + |
| 188 | + return self::checkArrayValues($val, $field, $min, $max); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @param string $field |
| 193 | + * @param int|null $min |
| 194 | + * @param int|null $max |
| 195 | + * @param array|null $default |
| 196 | + * |
| 197 | + * @return array |
| 198 | + */ |
| 199 | + public static function getArrayByJSON(string $field, int $min = null, int $max = null, array $default = null): array |
| 200 | + { |
| 201 | + if (!isset(self::$data[$field])) { |
| 202 | + if ($default === null) { |
| 203 | + throw new ValidateException($field, 'is required and must be JSON string'); |
| 204 | + } |
| 205 | + |
| 206 | + return $default; |
| 207 | + } |
| 208 | + |
| 209 | + if (!$val = self::$data[$field]) { |
| 210 | + if ($default === null) { |
| 211 | + throw new ValidateException($field, 'is required and must be JSON string'); |
| 212 | + } |
| 213 | + |
| 214 | + return $default; |
| 215 | + } |
| 216 | + |
| 217 | + if (!is_string($val)) { |
| 218 | + throw new ValidateException($field, 'must be an string'); |
| 219 | + } |
| 220 | + |
100 | 221 | return [];
|
101 | 222 | }
|
102 | 223 | }
|
0 commit comments