mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
Create "IntType" rule
This commit is contained in:
parent
8cab57052e
commit
60568eac62
|
@ -14,6 +14,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
|
|||
- Create "Finite" rule (#397)
|
||||
- Create "Identical" rule (#442)
|
||||
- Create "Infinite" rule (#397)
|
||||
- Create "IntType" rule (#451)
|
||||
- Create "KeyNested" rule (#429)
|
||||
- Create "KeySet" rule (#374)
|
||||
- Create "KeyValue" rule (#441)
|
||||
|
|
19
docs/IntType.md
Normal file
19
docs/IntType.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
# IntType
|
||||
|
||||
- `v::intType()`
|
||||
|
||||
Validates whether the type of a value is integer.
|
||||
|
||||
```php
|
||||
v::intType()->validate(42); // true
|
||||
v::intType()->validate('10'); // false
|
||||
```
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
* [Digit](Digit.md)
|
||||
* [Finite](Finite.md)
|
||||
* [Infinite](Infinite.md)
|
||||
* [IntVal](IntVal.md)
|
||||
* [Numeric](Numeric.md)
|
|
@ -10,6 +10,7 @@
|
|||
* [FloatVal](FloatVal.md)
|
||||
* [Instance](Instance.md)
|
||||
* [IntVal](IntVal.md)
|
||||
* [IntType](IntType.md)
|
||||
* [NullType](NullType.md)
|
||||
* [Numeric](Numeric.md)
|
||||
* [ObjectType](ObjectType.md)
|
||||
|
@ -50,6 +51,7 @@
|
|||
* [FloatVal](FloatVal.md)
|
||||
* [Infinite](Infinite.md)
|
||||
* [IntVal](IntVal.md)
|
||||
* [IntType](IntType.md)
|
||||
* [Multiple](Multiple.md)
|
||||
* [Negative](Negative.md)
|
||||
* [NotEmpty](NotEmpty.md)
|
||||
|
@ -231,6 +233,7 @@
|
|||
* [Infinite](Infinite.md)
|
||||
* [Instance](Instance.md)
|
||||
* [IntVal](IntVal.md)
|
||||
* [IntType](IntType.md)
|
||||
* [Ip](Ip.md)
|
||||
* [Json](Json.md)
|
||||
* [Key](Key.md)
|
||||
|
|
24
library/Exceptions/IntTypeException.php
Normal file
24
library/Exceptions/IntTypeException.php
Normal 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 IntTypeException extends ValidationException
|
||||
{
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be of the type integer',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be of the type integer',
|
||||
),
|
||||
);
|
||||
}
|
20
library/Rules/IntType.php
Normal file
20
library/Rules/IntType.php
Normal 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 IntType extends AbstractRule
|
||||
{
|
||||
public function validate($input)
|
||||
{
|
||||
return is_int($input);
|
||||
}
|
||||
}
|
|
@ -68,6 +68,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator infinite()
|
||||
* @method static Validator instance(string $instanceName)
|
||||
* @method static Validator intVal()
|
||||
* @method static Validator intType()
|
||||
* @method static Validator ip(mixed $ipOptions = null)
|
||||
* @method static Validator json()
|
||||
* @method static Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
|
||||
|
|
10
tests/integration/intType_1.phpt
Normal file
10
tests/integration/intType_1.phpt
Normal file
|
@ -0,0 +1,10 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::intType()->assert(42);
|
||||
v::intType()->check(1984);
|
||||
?>
|
||||
--EXPECTF--
|
15
tests/integration/intType_2.phpt
Normal file
15
tests/integration/intType_2.phpt
Normal file
|
@ -0,0 +1,15 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\IntTypeException;
|
||||
|
||||
try {
|
||||
v::intType()->check('42');
|
||||
} catch (IntTypeException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"42" must be of the type integer
|
15
tests/integration/intType_3.phpt
Normal file
15
tests/integration/intType_3.phpt
Normal file
|
@ -0,0 +1,15 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
|
||||
try {
|
||||
v::intType()->assert('1984');
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
\-"1984" must be of the type integer
|
15
tests/integration/intType_4.phpt
Normal file
15
tests/integration/intType_4.phpt
Normal file
|
@ -0,0 +1,15 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\IntTypeException;
|
||||
|
||||
try {
|
||||
v::not(v::intType())->check(42);
|
||||
} catch (IntTypeException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
42 must not be of the type integer
|
15
tests/integration/intType_5.phpt
Normal file
15
tests/integration/intType_5.phpt
Normal file
|
@ -0,0 +1,15 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
|
||||
try {
|
||||
v::not(v::intType())->assert(1984);
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
\-1984 must not be of the type integer
|
59
tests/unit/Rules/IntTypeTest.php
Normal file
59
tests/unit/Rules/IntTypeTest.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?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\IntType
|
||||
*/
|
||||
class IntTypeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function providerForValidIntType()
|
||||
{
|
||||
return array(
|
||||
array(0),
|
||||
array(123456),
|
||||
array(PHP_INT_MAX),
|
||||
array(PHP_INT_MAX * -1),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidIntType
|
||||
*/
|
||||
public function testShouldValidateInputWhenItIsAValidIntType($input)
|
||||
{
|
||||
$rule = new IntType();
|
||||
|
||||
$this->assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
public function providerForInvalidIntType()
|
||||
{
|
||||
return array(
|
||||
array('1'),
|
||||
array(1.0),
|
||||
array(PHP_INT_MAX + 1),
|
||||
array(true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidIntType
|
||||
*/
|
||||
public function testShouldInvalidateInputWhenItIsNotAValidIntType($input)
|
||||
{
|
||||
$rule = new IntType();
|
||||
|
||||
$this->assertFalse($rule->validate($input));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue