Create "FloatType" rule

This commit is contained in:
Reginaldo Junior 2015-10-17 15:17:07 -03:00 committed by Henrique Moody
parent c84020f7f1
commit 7398588c56
13 changed files with 191 additions and 0 deletions

View file

@ -15,6 +15,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
- Create "Extension" rule (#360)
- Create "Factor" rule (#405)
- Create "Finite" rule (#397)
- Create "FloatType" rule (#565)
- Create "Identical" rule (#442)
- Create "Infinite" rule (#397)
- Create "IntType" rule (#451)

18
docs/FloatType.md Normal file
View file

@ -0,0 +1,18 @@
# FloatType
- `v::floatType()`
Validates whether the type of a value is float.
```php
v::floatType()->validate(1.5); // true
v::floatType()->validate('1.5'); // false
v::floatType()->validate(0e5); // true
```
***
See also:
* [FloatVal](FloatVal.md)
* [IntType](IntType.md)
* [IntVal](IntVal.md)

View file

@ -8,3 +8,10 @@ Validates a floating point number.
v::floatVal()->validate(1.5); //true
v::floatVal()->validate('1e5'); //true
```
***
See also:
* [FloatType](FloatType.md)
* [IntType](IntType.md)
* [IntVal](IntVal.md)

View file

@ -9,6 +9,7 @@
* [Date](Date.md)
* [FalseVal](FalseVal.md)
* [FloatVal](FloatVal.md)
* [FloatType](FloatType.md)
* [Instance](Instance.md)
* [IntVal](IntVal.md)
* [IntType](IntType.md)
@ -50,6 +51,7 @@
* [Factor](Factor.md)
* [Finite](Finite.md)
* [FloatVal](FloatVal.md)
* [FloatType](FloatType.md)
* [Infinite](Infinite.md)
* [IntVal](IntVal.md)
* [IntType](IntType.md)
@ -230,6 +232,7 @@
* [FilterVar](FilterVar.md)
* [Finite](Finite.md)
* [FloatVal](FloatVal.md)
* [FloatType](FloatType.md)
* [Graph](Graph.md)
* [HexRgbColor](HexRgbColor.md)
* [In](In.md)

View 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 FloatTypeException extends ValidationException
{
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be of the type float',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be of the type float',
]
];
}

View file

@ -0,0 +1,20 @@
<?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 FloatType extends AbstractRule
{
public function validate($input)
{
return is_float($input);
}
}

View file

@ -64,6 +64,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator filterVar(int $filter, mixed $options = null)
* @method static Validator finite()
* @method static Validator floatVal()
* @method static Validator floatType()
* @method static Validator graph(string $additionalChars = null)
* @method static Validator hexRgbColor()
* @method static Validator in(mixed $haystack, bool $compareIdentical = false)

View file

@ -0,0 +1,10 @@
--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

@ -0,0 +1,15 @@
--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

@ -0,0 +1,15 @@
--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

@ -0,0 +1,15 @@
--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

@ -0,0 +1,15 @@
--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

@ -0,0 +1,47 @@
<?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\FloatType
*/
class FloatTypeTest extends RuleTestCase
{
public function providerForValidInput()
{
$rule = new FloatType();
return [
[$rule, 165.23],
[$rule, 1.3e3],
[$rule, 7E-10],
[$rule, 0.0],
[$rule, -2.44],
[$rule, 10/33.33],
[$rule, PHP_INT_MAX + 1],
];
}
public function providerForInvalidInput()
{
$rule = new FloatType();
return [
[$rule, '1'],
[$rule, '1.0'],
[$rule, '7E-10'],
[$rule, 111111],
[$rule, PHP_INT_MAX * -1]
];
}
}