From 3f41cf6058977f3a47defb1ca5555c028729fbe4 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Mon, 4 Feb 2013 22:41:35 -0200 Subject: [PATCH] Created rule "Uploaded" This rule checks if the given data is a file was uploaded via HTTP POST. --- README.md | 7 +++ .../Exceptions/UploadedException.php | 17 ++++++ library/Respect/Validation/Rules/Uploaded.php | 18 ++++++ library/Respect/Validation/Validator.php | 1 + .../Respect/Validation/Rules/UploadedTest.php | 58 +++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 library/Respect/Validation/Exceptions/UploadedException.php create mode 100644 library/Respect/Validation/Rules/Uploaded.php create mode 100644 tests/library/Respect/Validation/Rules/UploadedTest.php diff --git a/README.md b/README.md index 952719e6..fed8c314 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,7 @@ Reference * v::file() * v::readable() * v::symbolicLink() + * v::uploaded() * v::writable() ### Other @@ -1441,6 +1442,12 @@ See also * v::domain() - Validates domain names * v::countryCode() - Validates ISO country codes +#### v::uploaded() + +Validates if the given data is a file was uploaded via HTTP POST. + + v::uploaded()->validate('/path/of/an/uploaded/file'); //true + #### v::uppercase() Validates if string characters are uppercase in the input: diff --git a/library/Respect/Validation/Exceptions/UploadedException.php b/library/Respect/Validation/Exceptions/UploadedException.php new file mode 100644 index 00000000..910ccb3c --- /dev/null +++ b/library/Respect/Validation/Exceptions/UploadedException.php @@ -0,0 +1,17 @@ + array( + self::STANDARD => '{{name}} must be an uploaded file', + ), + self::MODE_NEGATIVE => array( + self::STANDARD => '{{name}} must not be an uploaded file', + ) + ); + +} diff --git a/library/Respect/Validation/Rules/Uploaded.php b/library/Respect/Validation/Rules/Uploaded.php new file mode 100644 index 00000000..a54db8ed --- /dev/null +++ b/library/Respect/Validation/Rules/Uploaded.php @@ -0,0 +1,18 @@ +getPathname(); + } + + return (is_string($input) && is_uploaded_file($input)); + } + +} + diff --git a/library/Respect/Validation/Validator.php b/library/Respect/Validation/Validator.php index 2f2adfc3..1ed893fc 100644 --- a/library/Respect/Validation/Validator.php +++ b/library/Respect/Validation/Validator.php @@ -81,6 +81,7 @@ use Respect\Validation\Rules\AllOf; * @method \Respect\Validation\Validator string() * @method \Respect\Validation\Validator symbolicLink() * @method \Respect\Validation\Validator tld() + * @method \Respect\Validation\Validator uploaded() * @method \Respect\Validation\Validator uppercase() * @method \Respect\Validation\Validator version() * @method \Respect\Validation\Validator vowel() diff --git a/tests/library/Respect/Validation/Rules/UploadedTest.php b/tests/library/Respect/Validation/Rules/UploadedTest.php new file mode 100644 index 00000000..dff26b5c --- /dev/null +++ b/tests/library/Respect/Validation/Rules/UploadedTest.php @@ -0,0 +1,58 @@ +assertTrue($rule->validate($input)); + } + + /** + * @covers Respect\Validation\Rules\Uploaded::validate + */ + public function testInvalidUploadedFileShouldReturnFalse() + { + $GLOBALS['is_uploaded_file'] = false; + + $rule = new Uploaded(); + $input = '/path/of/an/invalid/uploaded/file.txt'; + $this->assertFalse($rule->validate($input)); + } + + /** + * @covers Respect\Validation\Rules\Uploaded::validate + */ + public function testShouldValidateObjects() + { + $GLOBALS['is_uploaded_file'] = true; + + $rule = new Uploaded(); + $object = new \SplFileInfo('/path/of/an/uploaded/file'); + + $this->assertTrue($rule->validate($object)); + } + +}