mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 11:52:34 +02:00
Create "Infinite" rule
This commit is contained in:
parent
c80ed1bb85
commit
a7aa5f8ec0
|
@ -12,5 +12,6 @@ v::finite()->validate(10); //true
|
|||
See also:
|
||||
|
||||
* [Digit](Digit.md)
|
||||
* [Infinite](Infinite.md)
|
||||
* [Int](Int.md)
|
||||
* [Numeric](Numeric.md)
|
||||
|
|
16
docs/Infinite.md
Normal file
16
docs/Infinite.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
# Infinite
|
||||
|
||||
- `v::infinite()`
|
||||
|
||||
Validates if the input is an infinite number.
|
||||
|
||||
```php
|
||||
v::infinite()->validate(INF); //true
|
||||
```
|
||||
|
||||
See also:
|
||||
|
||||
* [Digit](Digit.md)
|
||||
* [Finite](Finite.md)
|
||||
* [Int](Int.md)
|
||||
* [Numeric](Numeric.md)
|
|
@ -13,4 +13,5 @@ See also:
|
|||
|
||||
* [Digit](Digit.md)
|
||||
* [Finite](Finite.md)
|
||||
* [Infinite](Infinite.md)
|
||||
* [Numeric](Numeric.md)
|
||||
|
|
|
@ -13,4 +13,5 @@ See also:
|
|||
|
||||
* [Digit](Digit.md)
|
||||
* [Finite](Finite.md)
|
||||
* [Infinite](Infinite.md)
|
||||
* [Int](Int.md)
|
||||
|
|
|
@ -16,6 +16,7 @@ See also
|
|||
* [Bool](Bool.md)
|
||||
* [Finite](Finite.md)
|
||||
* [Float](Float.md)
|
||||
* [Infinite](Infinite.md)
|
||||
* [Instance](Instance.md)
|
||||
* [Int](Int.md)
|
||||
* [Object](Object.md)
|
||||
|
|
|
@ -43,6 +43,7 @@
|
|||
* [Even](Even.md)
|
||||
* [Finite](Finite.md)
|
||||
* [Float](Float.md)
|
||||
* [Infinite](Infinite.md)
|
||||
* [Int](Int.md)
|
||||
* [Multiple](Multiple.md)
|
||||
* [Negative](Negative.md)
|
||||
|
@ -211,6 +212,7 @@
|
|||
* [Graph](Graph.md)
|
||||
* [HexRgbColor](HexRgbColor.md)
|
||||
* [In](In.md)
|
||||
* [Infinite](Infinite.md)
|
||||
* [Instance](Instance.md)
|
||||
* [Int](Int.md)
|
||||
* [Ip](Ip.md)
|
||||
|
|
30
library/Exceptions/InfiniteException.php
Normal file
30
library/Exceptions/InfiniteException.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 InfiniteException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be an infinite number',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be an infinite number',
|
||||
),
|
||||
);
|
||||
}
|
26
library/Rules/Infinite.php
Normal file
26
library/Rules/Infinite.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 Infinite extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input)
|
||||
{
|
||||
return is_numeric($input) && is_infinite($input);
|
||||
}
|
||||
}
|
|
@ -62,6 +62,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator graph(string $additionalChars = null)
|
||||
* @method static Validator hexRgbColor()
|
||||
* @method static Validator in(mixed $haystack, bool $compareIdentical = false)
|
||||
* @method static Validator infinite()
|
||||
* @method static Validator instance(string $instanceName)
|
||||
* @method static Validator int()
|
||||
* @method static Validator ip(mixed $ipOptions = null)
|
||||
|
|
75
tests/unit/Rules/InfiniteTest.php
Normal file
75
tests/unit/Rules/InfiniteTest.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?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\Infinite
|
||||
* @covers Respect\Validation\Exceptions\InfiniteException
|
||||
*/
|
||||
class InfiniteTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $rule;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->rule = new Infinite();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInfinite
|
||||
*/
|
||||
public function testShouldValidateInfiniteNumbers($input)
|
||||
{
|
||||
$this->assertTrue($this->rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNonInfinite
|
||||
*/
|
||||
public function testShouldNotValidateNonInfiniteNumbers($input)
|
||||
{
|
||||
$this->assertFalse($this->rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\InfiniteException
|
||||
* @expectedExceptionMessage "123456" must be an infinite number
|
||||
*/
|
||||
public function testShouldThrowInfiniteExceptionWhenChecking()
|
||||
{
|
||||
$this->rule->check(123456);
|
||||
}
|
||||
|
||||
public function providerForInfinite()
|
||||
{
|
||||
return array(
|
||||
array(INF),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForNonInfinite()
|
||||
{
|
||||
return array(
|
||||
array(' '),
|
||||
array(array()),
|
||||
array(new \stdClass()),
|
||||
array(null),
|
||||
array('123456'),
|
||||
array(-9),
|
||||
array(0),
|
||||
array(16),
|
||||
array(2),
|
||||
array(PHP_INT_MAX),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue