Skip to content

Commit 425ce7c

Browse files
authored
Merge pull request #45 from nonamephp/feature/validator-type-resource
Add validator for resource, stream, dir, and file
2 parents f4400ee + 57ee19b commit 425ce7c

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ if ($valid) {
231231
* `ipv4` Validate that value is IPv4
232232
* `ipv6` Validate that value is IPv6
233233
* `date`, `datetime` Validate that value is date/datetime
234+
* `resource` Validate that value is a resource
235+
* `stream` Validate that value is a stream
236+
* `dir`, `directory` Validate that value is a directory
237+
* `file` Validate that value is a file
234238

235239
**Hint:** Adding `[]` to any type (e.g. `int[]`) will validate an array of values.
236240

src/Validator.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
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
41+
* @method static bool isFile(mixed $value, array $rule = []) Checks if value is file
3742
*/
3843
class Validator
3944
{
@@ -235,6 +240,24 @@ protected function registerBuiltInTypes()
235240
'alias' => 'datetime',
236241
'validator' => [$this, 'validateDateTime']
237242
]);
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+
]);
238261
}
239262

240263
/**
@@ -864,4 +887,90 @@ protected function validateDateTime($value, array $rule = []): bool
864887

865888
return true;
866889
}
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+
}
867976
}

tests/ValidatorTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
class ValidatorTest extends \PHPUnit_Framework_TestCase
55
{
6+
protected $dir;
7+
protected $file;
68
protected $resource;
79
protected $nullValues;
810
protected $booleanValues;
@@ -30,6 +32,8 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
3032
protected function setUp()
3133
{
3234
// Create resource/stream
35+
$this->dir = __DIR__ . '/fixtures';
36+
$this->file = $this->dir . '/resource.txt';
3337
$this->resource = fopen(__DIR__ . '/fixtures/resource.txt', 'r');
3438

3539
// Set values for each data type
@@ -741,6 +745,43 @@ public function testValidateDateTime()
741745
}
742746
}
743747

748+
/**
749+
* @covers Validator::validateResource
750+
*/
751+
public function testValidateResource()
752+
{
753+
$this->assertTrue(Validator::isResource($this->resource));
754+
$this->assertTrue(Validator::isResource($this->resource, ['resource_type' => 'stream']));
755+
}
756+
757+
/**
758+
* @covers Validator::validateStream
759+
*/
760+
public function testValidateStream()
761+
{
762+
$this->assertTrue(Validator::isStream($this->resource));
763+
}
764+
765+
/**
766+
* @covers Validator::validateDir
767+
*/
768+
public function testValidateDir()
769+
{
770+
$this->assertTrue(Validator::isDir($this->dir));
771+
$this->assertTrue(Validator::isDir($this->dir, ['is_writable' => true]));
772+
$this->assertFalse(Validator::isDir($this->dir, ['is_writable' => false]));
773+
}
774+
775+
/**
776+
* @covers Validator::validateFile
777+
*/
778+
public function testValidateFile()
779+
{
780+
$this->assertTrue(Validator::isFile($this->file));
781+
$this->assertTrue(Validator::isFile($this->file, ['is_writable' => true]));
782+
$this->assertFalse(Validator::isFile($this->file, ['is_writable' => false]));
783+
}
784+
744785
/**
745786
* Test 'required' setting for rules.
746787
*/

0 commit comments

Comments
 (0)