mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Create "BoolVal" rule
This commit is contained in:
parent
a281ad03d7
commit
29bdb0a9f6
|
@ -8,6 +8,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
|
|||
|
||||
- Add "alpha-3" and "numeric" formats for "CountryCode" rule (#530)
|
||||
- Add support for PHP 7 (#426)
|
||||
- Create "BoolVal" rule (#583)
|
||||
- Create "Bsn" rule (#450)
|
||||
- Create "CallableType" rule (#397)
|
||||
- Create "Countable" rule (#566)
|
||||
|
|
31
docs/BoolVal.md
Normal file
31
docs/BoolVal.md
Normal file
|
@ -0,0 +1,31 @@
|
|||
# BoolType
|
||||
|
||||
- `v::boolVal()`
|
||||
|
||||
Validates if the input results in a boolean value:
|
||||
|
||||
```php
|
||||
v::boolVal()->validate('on'); // true
|
||||
v::boolVal()->validate('off'); // true
|
||||
v::boolVal()->validate('yes'); // true
|
||||
v::boolVal()->validate('no'); // true
|
||||
v::boolVal()->validate(1); // true
|
||||
v::boolVal()->validate(0); // true
|
||||
```
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
* [BoolType](BoolType.md)
|
||||
* [CallableType](CallableType.md)
|
||||
* [FloatType](FloatType.md)
|
||||
* [FloatVal](FloatVal.md)
|
||||
* [IntType](IntType.md)
|
||||
* [No](No.md)
|
||||
* [NullType](NullType.md)
|
||||
* [ObjectType](ObjectType.md)
|
||||
* [ResourceType](ResourceType.md)
|
||||
* [StringType](StringType.md)
|
||||
* [TrueVal](TrueVal.md)
|
||||
* [Type](Type.md)
|
||||
* [Yes](Yes.md)
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
* [ArrayVal](ArrayVal.md)
|
||||
* [ArrayType](ArrayType.md)
|
||||
* [BoolVal](BoolVal.md)
|
||||
* [BoolType](BoolType.md)
|
||||
* [CallableType](CallableType.md)
|
||||
* [Countable](Countable.md)
|
||||
|
|
23
library/Exceptions/BoolValException.php
Normal file
23
library/Exceptions/BoolValException.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?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 BoolValException extends ValidationException
|
||||
{
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a boolean value',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be a boolean value',
|
||||
]
|
||||
];
|
||||
}
|
19
library/Rules/BoolVal.php
Normal file
19
library/Rules/BoolVal.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?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 BoolVal extends AbstractRule
|
||||
{
|
||||
public function validate($input)
|
||||
{
|
||||
return is_bool(filter_var($input, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
|
||||
}
|
||||
}
|
|
@ -33,6 +33,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator between(mixed $min = null, mixed $max = null, bool $inclusive = true)
|
||||
* @method static Validator bic(string $countryCode)
|
||||
* @method static Validator boolType()
|
||||
* @method static Validator boolVal()
|
||||
* @method static Validator bsn()
|
||||
* @method static Validator call()
|
||||
* @method static Validator callableType()
|
||||
|
|
23
tests/integration/boolval_1.phpt
Normal file
23
tests/integration/boolval_1.phpt
Normal file
|
@ -0,0 +1,23 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::boolVal()->assert(1);
|
||||
v::boolVal()->assert('on');
|
||||
v::boolVal()->assert('off');
|
||||
v::boolVal()->assert('yes');
|
||||
v::boolVal()->assert('no');
|
||||
v::boolVal()->assert(true);
|
||||
v::boolVal()->assert(false);
|
||||
|
||||
v::boolVal()->check(1);
|
||||
v::boolVal()->check('on');
|
||||
v::boolVal()->check('off');
|
||||
v::boolVal()->check('yes');
|
||||
v::boolVal()->check('no');
|
||||
v::boolVal()->check(true);
|
||||
v::boolVal()->check(false);
|
||||
?>
|
||||
--EXPECTF--
|
37
tests/integration/boolval_2.phpt
Normal file
37
tests/integration/boolval_2.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Exceptions\BoolValException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::boolVal()->check('ok');
|
||||
} catch (BoolValException $e) {
|
||||
echo $e->getMainMessage() . PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::boolVal())->check('yes');
|
||||
} catch (BoolValException $e) {
|
||||
echo $e->getMainMessage() . PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::boolVal()->assert('yep');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage() . PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::boolVal())->assert('on');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage() . PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"ok" must be a boolean value
|
||||
"yes" must not be a boolean value
|
||||
\-"yep" must be a boolean value
|
||||
\-"on" must not be a boolean value
|
46
tests/unit/Rules/BoolValTest.php
Normal file
46
tests/unit/Rules/BoolValTest.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?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\BoolVal
|
||||
*/
|
||||
class BoolValTest extends RuleTestCase
|
||||
{
|
||||
public function providerForValidInput()
|
||||
{
|
||||
$rule = new BoolVal();
|
||||
|
||||
return [
|
||||
[$rule, true],
|
||||
[$rule, 1],
|
||||
[$rule, 'on'],
|
||||
[$rule, 'yes'],
|
||||
[$rule, 0],
|
||||
[$rule, false],
|
||||
[$rule, 'off'],
|
||||
[$rule, 'no '],
|
||||
[$rule, ''],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForInvalidInput()
|
||||
{
|
||||
$rule = new BoolVal();
|
||||
|
||||
return [
|
||||
[$rule, 'ok'],
|
||||
[$rule, 'yep'],
|
||||
[$rule, 10],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue