mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
Apply contribution guidelines to "Odd" rule
Also does not allow validation of non-numeric values. Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
c2f6876e4f
commit
5fc04558d6
|
@ -2,18 +2,20 @@
|
|||
|
||||
- `Odd()`
|
||||
|
||||
Validates an odd number.
|
||||
Validates whether the input is an odd number or not.
|
||||
|
||||
```php
|
||||
v::intVal()->odd()->validate(3); // true
|
||||
v::odd()->validate(0); // false
|
||||
v::odd()->validate(3); // true
|
||||
```
|
||||
|
||||
Using `int()` before `odd()` is a best practice.
|
||||
Using `intVal()` before `odd()` is a best practice.
|
||||
|
||||
## Changelog
|
||||
|
||||
Version | Description
|
||||
--------|-------------
|
||||
2.0.0 | Only validates integers
|
||||
0.3.9 | Created
|
||||
|
||||
***
|
||||
|
|
|
@ -13,8 +13,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class OddException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
*/
|
||||
final class OddException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be an odd number',
|
||||
|
|
|
@ -13,10 +13,28 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Odd extends AbstractRule
|
||||
use function filter_var;
|
||||
use function is_numeric;
|
||||
|
||||
/**
|
||||
* Validates whether the input is an odd number or not.
|
||||
*
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
*/
|
||||
final class Odd extends AbstractRule
|
||||
{
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (!is_numeric($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!filter_var($input, FILTER_VALIDATE_INT)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return 0 !== (int) $input % 2;
|
||||
}
|
||||
}
|
||||
|
|
38
tests/integration/rules/odd.phpt
Normal file
38
tests/integration/rules/odd.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\OddException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::odd()->check(2);
|
||||
} catch (OddException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::odd())->check(7);
|
||||
} catch (OddException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::odd()->assert(2);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::odd())->assert(9);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
2 must be an odd number
|
||||
7 must not be an odd number
|
||||
- 2 must be an odd number
|
||||
- 9 must not be an odd number
|
|
@ -13,60 +13,54 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
use function tmpfile;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Rules\Odd
|
||||
* @covers \Respect\Validation\Exceptions\OddException
|
||||
*
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
*/
|
||||
class OddTest extends TestCase
|
||||
final class OddTest extends RuleTestCase
|
||||
{
|
||||
protected $object;
|
||||
|
||||
protected function setUp(): void
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$this->object = new Odd();
|
||||
}
|
||||
$rule = new Odd();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForOdd
|
||||
*/
|
||||
public function testOdd($input): void
|
||||
{
|
||||
$this->object->assert($input);
|
||||
self::assertTrue($this->object->__invoke($input));
|
||||
$this->object->check($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotOdd
|
||||
* @expectedException \Respect\Validation\Exceptions\OddException
|
||||
*/
|
||||
public function testNotOdd($input): void
|
||||
{
|
||||
self::assertFalse($this->object->__invoke($input));
|
||||
$this->object->assert($input);
|
||||
}
|
||||
|
||||
public function providerForOdd()
|
||||
{
|
||||
return [
|
||||
[-5],
|
||||
[-1],
|
||||
[1],
|
||||
[13],
|
||||
[$rule, -5],
|
||||
[$rule, -1],
|
||||
[$rule, 1],
|
||||
[$rule, 13],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotOdd()
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new Odd();
|
||||
|
||||
return [
|
||||
[''],
|
||||
[-2],
|
||||
[-0],
|
||||
[0],
|
||||
[32],
|
||||
[$rule, []],
|
||||
[$rule, new stdClass()],
|
||||
[$rule, tmpfile()],
|
||||
[$rule, true],
|
||||
[$rule, false],
|
||||
[$rule, ''],
|
||||
[$rule, -2],
|
||||
[$rule, -0],
|
||||
[$rule, 0],
|
||||
[$rule, 32],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue