|
38 | 38 | * @method static bool isStream(mixed $value, array $rule = []) Checks if value is resource |
39 | 39 | * @method static bool isDir(mixed $value, array $rule = []) Checks if value is directory |
40 | 40 | * @method static bool isDirectory(mixed $value, array $rule = []) Checks if value is directory |
| 41 | + * @method static bool isFile(mixed $value, array $rule = []) Checks if value is file |
41 | 42 | */ |
42 | 43 | class Validator |
43 | 44 | { |
@@ -253,6 +254,10 @@ protected function registerBuiltInTypes() |
253 | 254 | 'alias' => 'directory', |
254 | 255 | 'validator' => [$this, 'validateDir'] |
255 | 256 | ]); |
| 257 | + |
| 258 | + $this->addType('file', [ |
| 259 | + 'validator' => [$this, 'validateFile'] |
| 260 | + ]); |
256 | 261 | } |
257 | 262 |
|
258 | 263 | /** |
@@ -942,4 +947,30 @@ protected function validateDir($value, array $rule = []): bool |
942 | 947 |
|
943 | 948 | return $valid; |
944 | 949 | } |
| 950 | + |
| 951 | + /** |
| 952 | + * Validate the $value is a valid file. |
| 953 | + * |
| 954 | + * @param $value |
| 955 | + * @param array $rule |
| 956 | + * @return bool |
| 957 | + */ |
| 958 | + protected function validateFile($value, array $rule = []): bool |
| 959 | + { |
| 960 | + $valid = is_file($value); |
| 961 | + |
| 962 | + if ($valid && isset($rule['is_writable']) && $rule['is_writable'] == true) { |
| 963 | + if (!is_writeable($value)) { |
| 964 | + $this->setError($rule['name'], "File ($value) must be writable."); |
| 965 | + return true; |
| 966 | + } |
| 967 | + } elseif ($valid && isset($rule['is_writable']) && $rule['is_writable'] == false) { |
| 968 | + if (is_writeable($value)) { |
| 969 | + $this->setError($rule['name'], "File ($value) must not be writable."); |
| 970 | + return true; |
| 971 | + } |
| 972 | + } |
| 973 | + |
| 974 | + return $valid; |
| 975 | + } |
945 | 976 | } |
0 commit comments