Create "CallableType" rule

This commit is contained in:
Henrique Moody 2015-08-20 01:17:24 -03:00
parent ea71de5812
commit 77b046f907
8 changed files with 149 additions and 0 deletions

16
docs/CallableType.md Normal file
View file

@ -0,0 +1,16 @@
# CallableType
- `v::callableType()`
Validates if the input is a callable value.
```php
v::callableType()->validate(function () {}); //true
v::callableType()->validate('trim'); //true
v::callableType()->validate(array(new Object, 'methodName')); //true
```
See also:
* [Callback](Callback.md)
* [Type](Type.md)

View file

@ -16,4 +16,5 @@ As in `v::call()`, you can pass a method or closure to it.
See also:
* [Call](Call.md)
* [CallableType](CallableType.md)
* [FilterVar](FilterVar.md)

View file

@ -14,6 +14,7 @@ See also
* [Arr](Arr.md)
* [Bool](Bool.md)
* [CallableType](CallableType.md)
* [Finite](Finite.md)
* [Float](Float.md)
* [Infinite](Infinite.md)

View file

@ -4,6 +4,7 @@
* [Arr](Arr.md)
* [Bool](Bool.md)
* [CallableType](CallableType.md)
* [Date](Date.md)
* [False](False.md)
* [Float](Float.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 CallableTypeException extends ValidationException
{
/**
* @var array
*/
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a callable',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a callable',
),
);
}

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 CallableType extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input)
{
return is_callable($input);
}
}

View file

@ -33,6 +33,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator bic(string $countryCode)
* @method static Validator bool()
* @method static Validator call()
* @method static Validator callableType()
* @method static Validator callback(mixed $callback)
* @method static Validator charset(mixed $charset)
* @method static Validator cnh()

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\CallableType
* @covers Respect\Validation\Exceptions\CallableTypeException
*/
class CallableTypeTest extends \PHPUnit_Framework_TestCase
{
protected $rule;
protected function setUp()
{
$this->rule = new CallableType();
}
/**
* @dataProvider providerForCallable
*/
public function testShouldValidateCallableTypeNumbers($input)
{
$this->assertTrue($this->rule->validate($input));
}
/**
* @dataProvider providerForNonCallable
*/
public function testShouldNotValidateNonCallableTypeNumbers($input)
{
$this->assertFalse($this->rule->validate($input));
}
/**
* @expectedException Respect\Validation\Exceptions\CallableTypeException
* @expectedExceptionMessage "testShouldThrowCallableTypeExceptionWhenChecking" must be a callable
*/
public function testShouldThrowCallableTypeExceptionWhenChecking()
{
$this->rule->check(__FUNCTION__);
}
public function providerForCallable()
{
return array(
array(function () {}),
array('trim'),
array(__METHOD__),
array(array($this, __FUNCTION__)),
);
}
public function providerForNonCallable()
{
return array(
array(' '),
array(INF),
array(array()),
array(new \stdClass()),
array(null),
);
}
}