Created rule "Exists"

This rule checks if the given data is a path of a regular file or
directory.
This commit is contained in:
Henrique Moody 2013-01-11 06:34:26 -02:00
parent 879f08aad1
commit fa641ef175
5 changed files with 123 additions and 0 deletions

View file

@ -287,6 +287,7 @@ Reference
* v::directory()
* v::domain()
* v::email()
* v::exists()
* v::file()
* v::ip()
* v::json()
@ -719,11 +720,17 @@ See also:
Validates directories.
v::directory()->validate(__DIR__); //true
v::directory()->validate(__FILE__); //false
This validator will consider SplFileInfo instances, so you can do something like:
v::directory()->validate(new \SplFileInfo($directory));
See also
* v::exists()
* v::file()
#### v::each(v $validatorForValue)
#### v::each(null, v $validatorForKey)
#### v::each(v $validatorForValue, v $validatorForKey)
@ -753,6 +760,22 @@ Validates an email address.
v::email()->validate('alexandre@gaigalas.net'); //true
#### v::exists()
Validates files or directories.
v::exists()->validate(__FILE__); //true
v::exists()->validate(__DIR__); //true
This validator will consider SplFileInfo instances, so you can do something like:
v::exists()->validate(new \SplFileInfo($file));
See also
* v::directory()
* v::file()
#### v::endsWith($value)
#### v::endsWith($value, boolean $identical=false)
@ -814,11 +837,17 @@ See also
Validates files.
v::file()->validate(__FILE__); //true
v::file()->validate(__DIR__); //false
This validator will consider SplFileInfo instances, so you can do something like:
v::file()->validate(new \SplFileInfo($file));
See also
* v::directory()
* v::exists()
#### v::float()
Validates a floating point number.

View file

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

View file

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

View file

@ -38,6 +38,7 @@ use Respect\Validation\Rules\AllOf;
* @method \Respect\Validation\Validator equals(mixed $compareTo, bool $compareIdentical=false)
* @method \Respect\Validation\Validator even()
* @method \Respect\Validation\Validator file()
* @method \Respect\Validation\Validator exists()
* @method \Respect\Validation\Validator float()
* @method \Respect\Validation\Validator graph(string $additionalChars = null)
* @method \Respect\Validation\Validator in(array $haystack, bool $compareIdentical = false)

View file

@ -0,0 +1,58 @@
<?php
namespace Respect\Validation\Rules;
$GLOBALS['file_exists'] = null;
function file_exists($file)
{
$return = \file_exists($file); // Running the real function
if (null !== $GLOBALS['file_exists']) {
$return = $GLOBALS['file_exists'];
$GLOBALS['file_exists'] = null;
}
return $return;
}
class ExistsTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Respect\Validation\Rules\Exists::validate
*/
public function testExistentFileShouldReturnTrue()
{
$GLOBALS['file_exists'] = true;
$rule = new Exists();
$input = '/path/of/an/existent/file';
$this->assertTrue($rule->validate($input));
}
/**
* @covers Respect\Validation\Rules\Exists::validate
*/
public function testNonExistentFileShouldReturnFalse()
{
$GLOBALS['file_exists'] = false;
$rule = new Exists();
$input = '/path/of/a/non-existent/file';
$this->assertFalse($rule->validate($input));
}
/**
* @covers Respect\Validation\Rules\Exists::validate
*/
public function testShouldValidateObjects()
{
$GLOBALS['file_exists'] = true;
$rule = new Exists();
$object = new \SplFileInfo('/path/of/an/existent/file');
$this->assertTrue($rule->validate($object));
}
}