mirror of
https://github.com/Respect/Validation.git
synced 2024-06-20 06:25:05 +02:00
Create "Number" rule
This commit is contained in:
parent
006354d48f
commit
82f53d27e1
|
@ -25,6 +25,8 @@ See also:
|
|||
- [IntType](IntType.md)
|
||||
- [IntVal](IntVal.md)
|
||||
- [NullType](NullType.md)
|
||||
- [Number](Number.md)
|
||||
- [NumericVal](NumericVal.md)
|
||||
- [ObjectType](ObjectType.md)
|
||||
- [ResourceType](ResourceType.md)
|
||||
- [StringType](StringType.md)
|
||||
|
|
36
docs/Number.md
Normal file
36
docs/Number.md
Normal file
|
@ -0,0 +1,36 @@
|
|||
# Number
|
||||
|
||||
- `Number()`
|
||||
|
||||
Validates if the input is a number.
|
||||
|
||||
```php
|
||||
v::number()->validate(42); // true
|
||||
v::number()->validate(acos(8)); // false
|
||||
```
|
||||
|
||||
> "In computing, NaN, standing for not a number, is a numeric data type value
|
||||
> representing an undefined or unrepresentable value, especially in
|
||||
> floating-point calculations." [Wikipedia](https://en.wikipedia.org/wiki/NaN)
|
||||
|
||||
## Changelog
|
||||
|
||||
Version | Description
|
||||
--------|-------------
|
||||
2.0.0 | Created
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
* [BoolType](BoolType.md)
|
||||
* [CallableType](CallableType.md)
|
||||
* [FloatType](FloatType.md)
|
||||
* [IntType](IntType.md)
|
||||
* [NotBlank](NotBlank.md)
|
||||
* [NotEmpty](NotEmpty.md)
|
||||
* [NotOptional](NotOptional.md)
|
||||
* [NullType](NullType.md)
|
||||
* [ObjectType](ObjectType.md)
|
||||
* [ResourceType](ResourceType.md)
|
||||
* [StringType](StringType.md)
|
||||
* [Type](Type.md)
|
|
@ -9,6 +9,9 @@ v::numericVal()->validate(-12); // true
|
|||
v::numericVal()->validate('135.0'); // true
|
||||
```
|
||||
|
||||
This rule doesn't validate if the data is a valid number, for that purpose
|
||||
use the [Number](Number.md) rule.
|
||||
|
||||
## Changelog
|
||||
|
||||
Version | Description
|
||||
|
|
|
@ -48,7 +48,7 @@
|
|||
- [Max](Max.md)
|
||||
- [Min](Min.md)
|
||||
|
||||
## NumericVal
|
||||
## Numeric
|
||||
|
||||
- [Base](Base.md)
|
||||
- [Between](Between.md)
|
||||
|
@ -65,6 +65,7 @@
|
|||
- [Multiple](Multiple.md)
|
||||
- [Negative](Negative.md)
|
||||
- [NotEmpty](NotEmpty.md)
|
||||
- [Number](Number.md)
|
||||
- [NumericVal](NumericVal.md)
|
||||
- [Odd](Odd.md)
|
||||
- [PerfectSquare](PerfectSquare.md)
|
||||
|
@ -299,6 +300,7 @@
|
|||
- [NotEmpty](NotEmpty.md)
|
||||
- [NotOptional](NotOptional.md)
|
||||
- [NullType](NullType.md)
|
||||
- [Number](Number.md)
|
||||
- [NumericVal](NumericVal.md)
|
||||
- [ObjectType](ObjectType.md)
|
||||
- [Odd](Odd.md)
|
||||
|
|
24
library/Exceptions/NumberException.php
Normal file
24
library/Exceptions/NumberException.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?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;
|
||||
|
||||
class NumberException extends ValidationException
|
||||
{
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a number',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be a number',
|
||||
],
|
||||
];
|
||||
}
|
24
library/Rules/Number.php
Normal file
24
library/Rules/Number.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?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;
|
||||
|
||||
class Number extends AbstractRule
|
||||
{
|
||||
public function validate($input)
|
||||
{
|
||||
if (!is_numeric($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !is_nan($input);
|
||||
}
|
||||
}
|
|
@ -110,6 +110,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator notOptional()
|
||||
* @method static Validator noWhitespace()
|
||||
* @method static Validator nullType()
|
||||
* @method static Validator number()
|
||||
* @method static Validator numericVal()
|
||||
* @method static Validator objectType()
|
||||
* @method static Validator odd()
|
||||
|
|
11
tests/integration/rules/number_1.phpt
Normal file
11
tests/integration/rules/number_1.phpt
Normal file
|
@ -0,0 +1,11 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::number()->assert(13);
|
||||
v::number()->check(42);
|
||||
?>
|
||||
--EXPECTF--
|
17
tests/integration/rules/number_2.phpt
Normal file
17
tests/integration/rules/number_2.phpt
Normal file
|
@ -0,0 +1,17 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NumberException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::number()->check(acos(1.01));
|
||||
} catch (NumberException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
NaN must be a number
|
16
tests/integration/rules/number_3.phpt
Normal file
16
tests/integration/rules/number_3.phpt
Normal file
|
@ -0,0 +1,16 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::number()->assert(NAN);
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
- NaN must be a number
|
17
tests/integration/rules/number_4.phpt
Normal file
17
tests/integration/rules/number_4.phpt
Normal file
|
@ -0,0 +1,17 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NumberException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::number())->check(42);
|
||||
} catch (NumberException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
42 must not be a number
|
17
tests/integration/rules/number_5.phpt
Normal file
17
tests/integration/rules/number_5.phpt
Normal file
|
@ -0,0 +1,17 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::number())->assert(42);
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
- 42 must not be a number
|
53
tests/unit/Rules/NumberTest.php
Normal file
53
tests/unit/Rules/NumberTest.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?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;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Rules\Number
|
||||
*/
|
||||
class NumberTest extends RuleTestCase
|
||||
{
|
||||
public function providerForValidInput()
|
||||
{
|
||||
$rule = new Number();
|
||||
|
||||
return [
|
||||
[$rule, '42'],
|
||||
[$rule, 123456],
|
||||
[$rule, 0.00000000001],
|
||||
[$rule, '0.5'],
|
||||
[$rule, PHP_INT_MAX],
|
||||
[$rule, -PHP_INT_MAX],
|
||||
[$rule, INF],
|
||||
[$rule, -INF],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForInvalidInput()
|
||||
{
|
||||
$rule = new Number();
|
||||
|
||||
return [
|
||||
[$rule, acos(1.01)],
|
||||
[$rule, sqrt(-1)],
|
||||
[$rule, NAN],
|
||||
[$rule, -NAN],
|
||||
[$rule, false],
|
||||
[$rule, true],
|
||||
[$rule, []],
|
||||
[$rule, new stdClass()],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue