Skip to content

Commit

Permalink
Create "FloatType" rule
Browse files Browse the repository at this point in the history
  • Loading branch information
reginaldojunior authored and henriquemoody committed Oct 18, 2015
1 parent c84020f commit 7398588
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions docs/FloatType.md
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions docs/FloatVal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions docs/VALIDATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions library/Exceptions/FloatTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <[email protected]>
*
* 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',
]
];
}
20 changes: 20 additions & 0 deletions library/Rules/FloatType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <[email protected]>
*
* 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);
}
}
1 change: 1 addition & 0 deletions library/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* @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)
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/floatType_1.phpt
Original file line number Diff line number Diff line change
@@ -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--
15 changes: 15 additions & 0 deletions tests/integration/floatType_2.phpt
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/integration/floatType_3.phpt
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/integration/floatType_4.phpt
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/integration/floatType_5.phpt
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions tests/unit/Rules/FloatTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <[email protected]>
*
* 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]
];
}
}

0 comments on commit 7398588

Please sign in to comment.