mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 16:22:16 +02:00
Create "Resource" rule
This commit is contained in:
parent
a7aa5f8ec0
commit
954c257fc9
14
docs/Resource.md
Normal file
14
docs/Resource.md
Normal 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)
|
|
@ -20,4 +20,5 @@ See also
|
|||
* [Instance](Instance.md)
|
||||
* [Int](Int.md)
|
||||
* [Object](Object.md)
|
||||
* [Resource](Resource.md)
|
||||
* [String](String.md)
|
||||
|
|
|
@ -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)
|
||||
|
|
30
library/Exceptions/ResourceException.php
Normal file
30
library/Exceptions/ResourceException.php
Normal 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',
|
||||
),
|
||||
);
|
||||
}
|
26
library/Rules/Resource.php
Normal file
26
library/Rules/Resource.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
73
tests/unit/Rules/ResourceTest.php
Normal file
73
tests/unit/Rules/ResourceTest.php
Normal 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),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue