Created rule "Uploaded"

This rule checks if the given data is a file was uploaded via HTTP POST.
This commit is contained in:
Henrique Moody 2013-02-04 22:41:35 -02:00
parent 48a9fad2c2
commit 3f41cf6058
5 changed files with 101 additions and 0 deletions

View file

@ -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:

View file

@ -0,0 +1,17 @@
<?php
namespace Respect\Validation\Exceptions;
class WritableException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an uploaded file',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an uploaded file',
)
);
}

View file

@ -0,0 +1,18 @@
<?php
namespace Respect\Validation\Rules;
class Uploaded extends AbstractRule
{
public function validate($input)
{
if ($input instanceof \SplFileInfo) {
$input = $input->getPathname();
}
return (is_string($input) && is_uploaded_file($input));
}
}

View file

@ -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()

View file

@ -0,0 +1,58 @@
<?php
namespace Respect\Validation\Rules;
$GLOBALS['is_uploaded_file'] = null;
function is_uploaded_file($uploaded)
{
$return = \is_uploaded_file($uploaded); // Running the real function
if (null !== $GLOBALS['is_uploaded_file']) {
$return = $GLOBALS['is_uploaded_file'];
$GLOBALS['is_uploaded_file'] = null;
}
return $return;
}
class UploadedTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Respect\Validation\Rules\Uploaded::validate
*/
public function testValidUploadedFileShouldReturnTrue()
{
$GLOBALS['is_uploaded_file'] = true;
$rule = new Uploaded();
$input = '/path/of/a/valid/uploaded/file.txt';
$this->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));
}
}