mirror of
https://github.com/Respect/Validation.git
synced 2024-06-22 23:36:33 +02:00
Created rule "Uploaded"
This rule checks if the given data is a file was uploaded via HTTP POST.
This commit is contained in:
parent
48a9fad2c2
commit
3f41cf6058
|
@ -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:
|
||||
|
|
17
library/Respect/Validation/Exceptions/UploadedException.php
Normal file
17
library/Respect/Validation/Exceptions/UploadedException.php
Normal 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',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
18
library/Respect/Validation/Rules/Uploaded.php
Normal file
18
library/Respect/Validation/Rules/Uploaded.php
Normal 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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()
|
||||
|
|
58
tests/library/Respect/Validation/Rules/UploadedTest.php
Normal file
58
tests/library/Respect/Validation/Rules/UploadedTest.php
Normal 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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue