mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 01:30:05 +02:00
Apply contribution guidelines to "Number" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
94d2e87b2a
commit
fad0005fa0
|
@ -13,8 +13,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NumberException extends ValidationException
|
||||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||
* @author Vitaliy <reboot.m@gmail.com>
|
||||
*/
|
||||
final class NumberException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a number',
|
||||
|
|
|
@ -13,8 +13,21 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Number extends AbstractRule
|
||||
use function is_nan;
|
||||
use function is_numeric;
|
||||
|
||||
/**
|
||||
* Validates if the input is a number.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||
* @author Vitaliy <reboot.m@gmail.com>
|
||||
*/
|
||||
final class Number extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (!is_numeric($input)) {
|
||||
|
|
38
tests/integration/rules/number.phpt
Normal file
38
tests/integration/rules/number.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\NumberException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::number()->check(acos(1.01));
|
||||
} catch (NumberException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::number())->check(42);
|
||||
} catch (NumberException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::number()->assert(NAN);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::number())->assert(42);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
`NaN` must be a number
|
||||
42 must not be a number
|
||||
- `NaN` must be a number
|
||||
- 42 must not be a number
|
|
@ -1,11 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::number()->assert(13);
|
||||
v::number()->check(42);
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,17 +0,0 @@
|
|||
--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->getMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
`NaN` must be a number
|
|
@ -1,16 +0,0 @@
|
|||
--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
|
|
@ -1,17 +0,0 @@
|
|||
--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->getMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
42 must not be a number
|
|
@ -1,17 +0,0 @@
|
|||
--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
|
|
@ -15,13 +15,26 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
use const INF;
|
||||
use const NAN;
|
||||
use const PHP_INT_MAX;
|
||||
use function acos;
|
||||
use function sqrt;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Number
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||
* @author Vitaliy <reboot.m@gmail.com>
|
||||
*/
|
||||
class NumberTest extends RuleTestCase
|
||||
final class NumberTest extends RuleTestCase
|
||||
{
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$rule = new Number();
|
||||
|
@ -38,6 +51,9 @@ class NumberTest extends RuleTestCase
|
|||
];
|
||||
}
|
||||
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new Number();
|
||||
|
|
Loading…
Reference in a new issue