mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
Apply contribution guidelines to "Positive" rule
Also does not allow validation of non-numeric values. Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
c422dc39e4
commit
72933a718f
|
@ -2,16 +2,19 @@
|
||||||
|
|
||||||
- `Positive()`
|
- `Positive()`
|
||||||
|
|
||||||
Validates if a number is higher than zero
|
Validates whether the input is a positive number.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
v::numericVal()->positive()->validate(-15); // false
|
v::positive()->validate(1); // true
|
||||||
|
v::positive()->validate(0); // false
|
||||||
|
v::positive()->validate(-15); // false
|
||||||
```
|
```
|
||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
|
||||||
Version | Description
|
Version | Description
|
||||||
--------|-------------
|
--------|-------------
|
||||||
|
2.0.0 | Does not validate non-numeric values
|
||||||
0.3.9 | Created
|
0.3.9 | Created
|
||||||
|
|
||||||
***
|
***
|
||||||
|
|
|
@ -13,8 +13,16 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Respect\Validation\Exceptions;
|
namespace Respect\Validation\Exceptions;
|
||||||
|
|
||||||
class PositiveException extends ValidationException
|
/**
|
||||||
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||||
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||||
|
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||||
|
*/
|
||||||
|
final class PositiveException extends ValidationException
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public static $defaultTemplates = [
|
public static $defaultTemplates = [
|
||||||
self::MODE_DEFAULT => [
|
self::MODE_DEFAULT => [
|
||||||
self::STANDARD => '{{name}} must be positive',
|
self::STANDARD => '{{name}} must be positive',
|
||||||
|
|
|
@ -13,10 +13,26 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Respect\Validation\Rules;
|
namespace Respect\Validation\Rules;
|
||||||
|
|
||||||
class Positive extends AbstractRule
|
use function is_numeric;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates whether the input is a positive number.
|
||||||
|
*
|
||||||
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||||
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||||
|
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||||
|
*/
|
||||||
|
final class Positive extends AbstractRule
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function validate($input): bool
|
public function validate($input): bool
|
||||||
{
|
{
|
||||||
|
if (!is_numeric($input)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return $input > 0;
|
return $input > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
38
tests/integration/rules/positive.phpt
Normal file
38
tests/integration/rules/positive.phpt
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
|
use Respect\Validation\Exceptions\NestedValidationException;
|
||||||
|
use Respect\Validation\Exceptions\PositiveException;
|
||||||
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
|
try {
|
||||||
|
v::positive()->check(-10);
|
||||||
|
} catch (PositiveException $exception) {
|
||||||
|
echo $exception->getMessage().PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
v::not(v::positive())->check(16);
|
||||||
|
} catch (PositiveException $exception) {
|
||||||
|
echo $exception->getMessage().PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
v::positive()->assert('a');
|
||||||
|
} catch (NestedValidationException $exception) {
|
||||||
|
echo $exception->getFullMessage().PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
v::not(v::positive())->assert('165');
|
||||||
|
} catch (NestedValidationException $exception) {
|
||||||
|
echo $exception->getFullMessage().PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
--EXPECTF--
|
||||||
|
-10 must be positive
|
||||||
|
16 must not be positive
|
||||||
|
- "a" must be positive
|
||||||
|
- "165" must not be positive
|
|
@ -13,65 +13,56 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Respect\Validation\Rules;
|
namespace Respect\Validation\Rules;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use Respect\Validation\Test\RuleTestCase;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group rule
|
* @group rule
|
||||||
|
*
|
||||||
* @covers \Respect\Validation\Rules\Positive
|
* @covers \Respect\Validation\Rules\Positive
|
||||||
* @covers \Respect\Validation\Exceptions\PositiveException
|
*
|
||||||
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||||
|
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||||
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||||
|
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||||
*/
|
*/
|
||||||
class PositiveTest extends TestCase
|
final class PositiveTest extends RuleTestCase
|
||||||
{
|
{
|
||||||
protected $object;
|
/*
|
||||||
|
* {@inheritdoc}
|
||||||
protected function setUp(): void
|
*/
|
||||||
|
public function providerForValidInput(): array
|
||||||
{
|
{
|
||||||
$this->object = new Positive();
|
$rule = new Positive();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForPositive
|
|
||||||
*/
|
|
||||||
public function testPositive($input): void
|
|
||||||
{
|
|
||||||
self::assertTrue($this->object->__invoke($input));
|
|
||||||
$this->object->check($input);
|
|
||||||
$this->object->assert($input);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForNotPositive
|
|
||||||
* @expectedException \Respect\Validation\Exceptions\PositiveException
|
|
||||||
*/
|
|
||||||
public function testNotPositive($input): void
|
|
||||||
{
|
|
||||||
self::assertFalse($this->object->__invoke($input));
|
|
||||||
$this->object->assert($input);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function providerForPositive()
|
|
||||||
{
|
|
||||||
return [
|
return [
|
||||||
[16],
|
[$rule, 16],
|
||||||
['165'],
|
[$rule, '165'],
|
||||||
[123456],
|
[$rule, 123456],
|
||||||
[1e10],
|
[$rule, 1e10],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function providerForNotPositive()
|
/*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function providerForInvalidInput(): array
|
||||||
{
|
{
|
||||||
|
$rule = new Positive();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
[''],
|
[$rule, ''],
|
||||||
[null],
|
[$rule, []],
|
||||||
['a'],
|
[$rule, new stdClass()],
|
||||||
[' '],
|
[$rule, null],
|
||||||
['Foo'],
|
[$rule, 'a'],
|
||||||
['-1.44'],
|
[$rule, ' '],
|
||||||
[-1e-5],
|
[$rule, 'Foo'],
|
||||||
[0],
|
[$rule, '-1.44'],
|
||||||
[-0],
|
[$rule, -1e-5],
|
||||||
[-10],
|
[$rule, 0],
|
||||||
|
[$rule, -0],
|
||||||
|
[$rule, -10],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue