Apply contribution guidelines to "FloatType" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-03-26 22:05:54 +02:00
parent 8465180062
commit ec8cb734b3
No known key found for this signature in database
GPG key ID: 221E9281655813A6
10 changed files with 74 additions and 77 deletions

View file

@ -2,7 +2,7 @@
- `FloatType()`
Validates whether the type of a value is float.
Validates whether the type of the input is [float](http://php.net/types.float).
```php
v::floatType()->validate(1.5); // true

View file

@ -13,14 +13,23 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class FloatTypeException extends ValidationException
/**
* Exception class for FloatType rule.
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Reginaldo Junior <76regi@gmail.com>
*/
final class FloatTypeException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be of the type float',
self::STANDARD => '{{name}} must be of type float',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be of the type float',
self::STANDARD => '{{name}} must not be of type float',
],
];
}

View file

@ -13,8 +13,19 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class FloatType extends AbstractRule
use function is_float;
/**
* Validates whether the type of the input is float.
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Reginaldo Junior <76regi@gmail.com>
*/
final class FloatType extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
return is_float($input);

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\FloatTypeException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::floatType()->check('42.33');
} catch (FloatTypeException $exception) {
echo $exception->getMainMessage().PHP_EOL;
}
try {
v::not(v::floatType())->check(INF);
} catch (FloatTypeException $exception) {
echo $exception->getMainMessage().PHP_EOL;
}
try {
v::floatType()->assert(true);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::floatType())->assert(2.0);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"42.33" must be of type float
`INF` must not be of type float
- `TRUE` must be of type float
- 2.0 must not be of type float

View file

@ -1,10 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
v::floatType()->assert(42.23);
v::floatType()->check(1984.23);
?>
--EXPECTF--

View file

@ -1,15 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\FloatTypeException;
use Respect\Validation\Validator as v;
try {
v::floatType()->check('42.33');
} catch (FloatTypeException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECTF--
"42.33" must be of the type float

View file

@ -1,15 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::floatType()->assert('1984.233');
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
- "1984.233" must be of the type float

View file

@ -1,15 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\FloatTypeException;
use Respect\Validation\Validator as v;
try {
v::not(v::floatType())->check(42.33);
} catch (FloatTypeException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECTF--
42.33 must not be of the type float

View file

@ -1,15 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::not(v::floatType())->assert(1984.434);
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
- 1984.434 must not be of the type float

View file

@ -16,11 +16,18 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @group rule
*
* @covers \Respect\Validation\Rules\FloatType
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Reginaldo Junior <76regi@gmail.com>
*/
class FloatTypeTest extends RuleTestCase
final class FloatTypeTest extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$rule = new FloatType();
@ -36,6 +43,9 @@ class FloatTypeTest extends RuleTestCase
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new FloatType();