mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 11:52:34 +02:00
Create "CallableType" rule
This commit is contained in:
parent
ea71de5812
commit
77b046f907
16
docs/CallableType.md
Normal file
16
docs/CallableType.md
Normal 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)
|
|
@ -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)
|
||||
|
|
|
@ -14,6 +14,7 @@ See also
|
|||
|
||||
* [Arr](Arr.md)
|
||||
* [Bool](Bool.md)
|
||||
* [CallableType](CallableType.md)
|
||||
* [Finite](Finite.md)
|
||||
* [Float](Float.md)
|
||||
* [Infinite](Infinite.md)
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
* [Arr](Arr.md)
|
||||
* [Bool](Bool.md)
|
||||
* [CallableType](CallableType.md)
|
||||
* [Date](Date.md)
|
||||
* [False](False.md)
|
||||
* [Float](Float.md)
|
||||
|
|
30
library/Exceptions/CallableTypeException.php
Normal file
30
library/Exceptions/CallableTypeException.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 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',
|
||||
),
|
||||
);
|
||||
}
|
26
library/Rules/CallableType.php
Normal file
26
library/Rules/CallableType.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 CallableType extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input)
|
||||
{
|
||||
return is_callable($input);
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
73
tests/unit/Rules/CallableTypeTest.php
Normal file
73
tests/unit/Rules/CallableTypeTest.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\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),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue