mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
Created rule "File"
This rule checks if the given data is a path of a regular file or not.
This commit is contained in:
parent
010d2f3829
commit
879f08aad1
13
README.md
13
README.md
|
@ -284,9 +284,10 @@ Reference
|
|||
* v::cnh()
|
||||
* v::cnpj()
|
||||
* v::cpf()
|
||||
* v::domain()
|
||||
* v::directory()
|
||||
* v::domain()
|
||||
* v::email()
|
||||
* v::file()
|
||||
* v::ip()
|
||||
* v::json()
|
||||
* v::macAddress()
|
||||
|
@ -808,6 +809,16 @@ See also
|
|||
* v::odd()
|
||||
* v::multiple()
|
||||
|
||||
#### v::file()
|
||||
|
||||
Validates files.
|
||||
|
||||
v::file()->validate(__FILE__); //true
|
||||
|
||||
This validator will consider SplFileInfo instances, so you can do something like:
|
||||
|
||||
v::file()->validate(new \SplFileInfo($file));
|
||||
|
||||
#### v::float()
|
||||
|
||||
Validates a floating point number.
|
||||
|
|
17
library/Respect/Validation/Exceptions/FileException.php
Normal file
17
library/Respect/Validation/Exceptions/FileException.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class FileException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be a file',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be a file',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
18
library/Respect/Validation/Rules/File.php
Normal file
18
library/Respect/Validation/Rules/File.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class File extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($input instanceof \SplFileInfo) {
|
||||
return $input->isFile();
|
||||
}
|
||||
|
||||
return (is_string($input) && is_file($input));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -37,6 +37,7 @@ use Respect\Validation\Rules\AllOf;
|
|||
* @method \Respect\Validation\Validator endsWith(mixed $endValue, bool $identical = false)
|
||||
* @method \Respect\Validation\Validator equals(mixed $compareTo, bool $compareIdentical=false)
|
||||
* @method \Respect\Validation\Validator even()
|
||||
* @method \Respect\Validation\Validator file()
|
||||
* @method \Respect\Validation\Validator float()
|
||||
* @method \Respect\Validation\Validator graph(string $additionalChars = null)
|
||||
* @method \Respect\Validation\Validator in(array $haystack, bool $compareIdentical = false)
|
||||
|
|
59
tests/library/Respect/Validation/Rules/FileTest.php
Normal file
59
tests/library/Respect/Validation/Rules/FileTest.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
$GLOBALS['is_file'] = null;
|
||||
|
||||
function is_file($file)
|
||||
{
|
||||
$return = \is_file($file); // Running the real function
|
||||
if (null !== $GLOBALS['is_file']) {
|
||||
$return = $GLOBALS['is_file'];
|
||||
$GLOBALS['is_file'] = null;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
class FileTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\File::validate
|
||||
*/
|
||||
public function testValidFileShouldReturnTrue()
|
||||
{
|
||||
$GLOBALS['is_file'] = true;
|
||||
|
||||
$rule = new File();
|
||||
$input = '/path/of/a/valid/file.txt';
|
||||
$this->assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\File::validate
|
||||
*/
|
||||
public function testInvalidFileShouldReturnFalse()
|
||||
{
|
||||
$GLOBALS['is_file'] = false;
|
||||
|
||||
$rule = new File();
|
||||
$input = '/path/of/an/invalid/file.txt';
|
||||
$this->assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\File::validate
|
||||
*/
|
||||
public function testShouldValidateObjects()
|
||||
{
|
||||
$rule = new File();
|
||||
$object = $this->getMock('SplFileInfo', array('isFile'), array('somefile.txt'));
|
||||
$object->expects($this->once())
|
||||
->method('isFile')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue($rule->validate($object));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue