Apply contribution guidelines to "Number" rule

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Ismael Elias 2018-08-07 14:38:11 -03:00 committed by Henrique Moody
parent 94d2e87b2a
commit fad0005fa0
No known key found for this signature in database
GPG key ID: 221E9281655813A6
9 changed files with 79 additions and 82 deletions

View file

@ -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',

View file

@ -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)) {

View 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

View file

@ -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--

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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();