|
34 | 34 | * @method static bool isIPv6(mixed $value, array $rule = []) Checks if value is valid IPv6
|
35 | 35 | * @method static bool isDate(mixed $value, array $rule = []) Checks if value is date/datetime
|
36 | 36 | * @method static bool isDateTime(mixed $value, array $rule = []) Checks if value is date/datetime
|
| 37 | + * @method static bool isResource(mixed $value, array $rule = []) Checks if value is resource |
| 38 | + * @method static bool isStream(mixed $value, array $rule = []) Checks if value is resource |
| 39 | + * @method static bool isDir(mixed $value, array $rule = []) Checks if value is directory |
| 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 |
37 | 42 | */
|
38 | 43 | class Validator
|
39 | 44 | {
|
@@ -235,6 +240,24 @@ protected function registerBuiltInTypes()
|
235 | 240 | 'alias' => 'datetime',
|
236 | 241 | 'validator' => [$this, 'validateDateTime']
|
237 | 242 | ]);
|
| 243 | + |
| 244 | + $this->addType('resource', [ |
| 245 | + 'validator' => [$this, 'validateResource'] |
| 246 | + ]); |
| 247 | + |
| 248 | + $this->addType('stream', [ |
| 249 | + 'extends' => 'resource', |
| 250 | + 'validator' => [$this, 'validateStream'] |
| 251 | + ]); |
| 252 | + |
| 253 | + $this->addType('dir', [ |
| 254 | + 'alias' => 'directory', |
| 255 | + 'validator' => [$this, 'validateDir'] |
| 256 | + ]); |
| 257 | + |
| 258 | + $this->addType('file', [ |
| 259 | + 'validator' => [$this, 'validateFile'] |
| 260 | + ]); |
238 | 261 | }
|
239 | 262 |
|
240 | 263 | /**
|
@@ -864,4 +887,90 @@ protected function validateDateTime($value, array $rule = []): bool
|
864 | 887 |
|
865 | 888 | return true;
|
866 | 889 | }
|
| 890 | + |
| 891 | + /** |
| 892 | + * Validate that $value is a valid resource. |
| 893 | + * |
| 894 | + * @param mixed $value |
| 895 | + * @param array $rule |
| 896 | + * @return bool |
| 897 | + */ |
| 898 | + protected function validateResource($value, array $rule = []): bool |
| 899 | + { |
| 900 | + $valid = is_resource($value); |
| 901 | + |
| 902 | + if ($valid && isset($rule['resource_type']) && is_string($rule['resource_type'])) { |
| 903 | + $valid = Str::equals(get_resource_type($value), $rule['resource_type'], false); |
| 904 | + if (!$valid) { |
| 905 | + $this->setError($rule['name'], "Value ($value) failed to validate as resource type of '{$rule['resource_type']}'"); |
| 906 | + return true; |
| 907 | + } |
| 908 | + } |
| 909 | + |
| 910 | + return $valid; |
| 911 | + } |
| 912 | + |
| 913 | + /** |
| 914 | + * Validate that $value is a valid stream. |
| 915 | + * |
| 916 | + * @param mixed $value |
| 917 | + * @param array $rule |
| 918 | + * @return bool |
| 919 | + */ |
| 920 | + protected function validateStream($value, array $rule = []): bool |
| 921 | + { |
| 922 | + return is_resource($value) && get_resource_type($value) == 'stream'; |
| 923 | + } |
| 924 | + |
| 925 | + /** |
| 926 | + * Validate the $value is a valid directory. |
| 927 | + * |
| 928 | + * @param $value |
| 929 | + * @param array $rule |
| 930 | + * @return bool |
| 931 | + */ |
| 932 | + protected function validateDir($value, array $rule = []): bool |
| 933 | + { |
| 934 | + $valid = is_dir($value); |
| 935 | + |
| 936 | + if ($valid && isset($rule['is_writable']) && $rule['is_writable'] == true) { |
| 937 | + if (!is_writeable($value)) { |
| 938 | + $this->setError($rule['name'], "Directory ($value) must be writable."); |
| 939 | + return true; |
| 940 | + } |
| 941 | + } elseif ($valid && isset($rule['is_writable']) && $rule['is_writable'] == false) { |
| 942 | + if (is_writeable($value)) { |
| 943 | + $this->setError($rule['name'], "Directory ($value) must not be writable."); |
| 944 | + return true; |
| 945 | + } |
| 946 | + } |
| 947 | + |
| 948 | + return $valid; |
| 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 | + } |
867 | 976 | }
|
0 commit comments