Skip to content

Commit e8294e6

Browse files
committed
Added resource, stream, and dir validators
1 parent f4400ee commit e8294e6

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/Validator.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
* @method static bool isIPv6(mixed $value, array $rule = []) Checks if value is valid IPv6
3535
* @method static bool isDate(mixed $value, array $rule = []) Checks if value is date/datetime
3636
* @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
3741
*/
3842
class Validator
3943
{
@@ -235,6 +239,20 @@ protected function registerBuiltInTypes()
235239
'alias' => 'datetime',
236240
'validator' => [$this, 'validateDateTime']
237241
]);
242+
243+
$this->addType('resource', [
244+
'validator' => [$this, 'validateResource']
245+
]);
246+
247+
$this->addType('stream', [
248+
'extends' => 'resource',
249+
'validator' => [$this, 'validateStream']
250+
]);
251+
252+
$this->addType('dir', [
253+
'alias' => 'directory',
254+
'validator' => [$this, 'validateDir']
255+
]);
238256
}
239257

240258
/**
@@ -864,4 +882,64 @@ protected function validateDateTime($value, array $rule = []): bool
864882

865883
return true;
866884
}
885+
886+
/**
887+
* Validate that $value is a valid resource.
888+
*
889+
* @param mixed $value
890+
* @param array $rule
891+
* @return bool
892+
*/
893+
protected function validateResource($value, array $rule = []): bool
894+
{
895+
$valid = is_resource($value);
896+
897+
if ($valid && isset($rule['resource_type']) && is_string($rule['resource_type'])) {
898+
$valid = Str::equals(get_resource_type($value), $rule['resource_type'], false);
899+
if (!$valid) {
900+
$this->setError($rule['name'], "Value ($value) failed to validate as resource type of '{$rule['resource_type']}'");
901+
return true;
902+
}
903+
}
904+
905+
return $valid;
906+
}
907+
908+
/**
909+
* Validate that $value is a valid stream.
910+
*
911+
* @param mixed $value
912+
* @param array $rule
913+
* @return bool
914+
*/
915+
protected function validateStream($value, array $rule = []): bool
916+
{
917+
return is_resource($value) && get_resource_type($value) == 'stream';
918+
}
919+
920+
/**
921+
* Validate the $value is a valid directory.
922+
*
923+
* @param $value
924+
* @param array $rule
925+
* @return bool
926+
*/
927+
protected function validateDir($value, array $rule = []): bool
928+
{
929+
$valid = is_dir($value);
930+
931+
if ($valid && isset($rule['is_writable']) && $rule['is_writable'] == true) {
932+
if (!is_writeable($value)) {
933+
$this->setError($rule['name'], "Directory ($value) must be writable.");
934+
return true;
935+
}
936+
} elseif ($valid && isset($rule['is_writable']) && $rule['is_writable'] == false) {
937+
if (is_writeable($value)) {
938+
$this->setError($rule['name'], "Directory ($value) must not be writable.");
939+
return true;
940+
}
941+
}
942+
943+
return $valid;
944+
}
867945
}

0 commit comments

Comments
 (0)