Create "Resource" rule

This commit is contained in:
Henrique Moody 2015-08-20 00:55:32 -03:00
parent a7aa5f8ec0
commit 954c257fc9
7 changed files with 147 additions and 0 deletions

14
docs/Resource.md Normal file
View file

@ -0,0 +1,14 @@
# Resource
- `v::resource()`
Validates if the input is a resource.
```php
v::resource()->validate(fopen('/path/to/file.txt', 'w')); //true
```
See also:
* [Type](Type.md)
* [Digit](Digit.md)

View file

@ -20,4 +20,5 @@ See also
* [Instance](Instance.md)
* [Int](Int.md)
* [Object](Object.md)
* [Resource](Resource.md)
* [String](String.md)

View file

@ -12,6 +12,7 @@
* [NullValue](NullValue.md)
* [Numeric](Numeric.md)
* [Object](Object.md)
* [Resource](Resource.md)
* [String](String.md)
* [True](True.md)
* [Type](Type.md)
@ -76,6 +77,7 @@
* [Prnt](Prnt.md)
* [Punct](Punct.md)
* [Regex](Regex.md)
* [Resource](Resource.md)
* [Slug](Slug.md)
* [Space](Space.md)
* [StartsWith](StartsWith.md)

View file

@ -0,0 +1,30 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Exceptions;
/**
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class ResourceException extends ValidationException
{
/**
* @var array
*/
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a resource',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a resource',
),
);
}

View file

@ -0,0 +1,26 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Rules;
/**
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class Resource extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input)
{
return is_resource($input);
}
}

View file

@ -99,6 +99,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator punct(string $additionalChars = null)
* @method static Validator readable()
* @method static Validator regex(string $regex)
* @method static Validator resource()
* @method static Validator roman()
* @method static Validator sf(string $name, array $params = null)
* @method static Validator size(string $minSize = null, string $maxSize = null)

View file

@ -0,0 +1,73 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Rules;
/**
* @group rule
* @covers Respect\Validation\Rules\Resource
* @covers Respect\Validation\Exceptions\ResourceException
*/
class ResourceTest extends \PHPUnit_Framework_TestCase
{
protected $rule;
protected function setUp()
{
$this->rule = new Resource();
}
/**
* @dataProvider providerForResource
*/
public function testShouldValidateResourceNumbers($input)
{
$this->assertTrue($this->rule->validate($input));
}
/**
* @dataProvider providerForNonResource
*/
public function testShouldNotValidateNonResourceNumbers($input)
{
$this->assertFalse($this->rule->validate($input));
}
/**
* @expectedException Respect\Validation\Exceptions\ResourceException
* @expectedExceptionMessage "Something" must be a resource
*/
public function testShouldThrowResourceExceptionWhenChecking()
{
$this->rule->check('Something');
}
public function providerForResource()
{
return array(
array(stream_context_create()),
array(tmpfile()),
array(xml_parser_create()),
);
}
public function providerForNonResource()
{
return array(
array('String'),
array(123),
array(array()),
array(function () {}),
array(new \stdClass()),
array(null),
);
}
}