mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
Apply contribution guidelines to "PrimeNumber" rule
Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
1e89dc26ea
commit
6e26ba8837
|
@ -13,8 +13,16 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Respect\Validation\Exceptions;
|
namespace Respect\Validation\Exceptions;
|
||||||
|
|
||||||
class PrimeNumberException extends ValidationException
|
/**
|
||||||
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||||
|
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||||
|
* @author Kleber Hamada Sato <kleberhs007@yahoo.com>
|
||||||
|
*/
|
||||||
|
final class PrimeNumberException extends ValidationException
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public static $defaultTemplates = [
|
public static $defaultTemplates = [
|
||||||
self::MODE_DEFAULT => [
|
self::MODE_DEFAULT => [
|
||||||
self::STANDARD => '{{name}} must be a valid prime number',
|
self::STANDARD => '{{name}} must be a valid prime number',
|
||||||
|
|
|
@ -13,8 +13,24 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Respect\Validation\Rules;
|
namespace Respect\Validation\Rules;
|
||||||
|
|
||||||
class PrimeNumber extends AbstractRule
|
use function ceil;
|
||||||
|
use function is_numeric;
|
||||||
|
use function sqrt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates whether the input is a prime number.
|
||||||
|
*
|
||||||
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||||
|
* @author Camilo Teixeira de Melo <kmilotxm@users.noreply.github.com>
|
||||||
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||||
|
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||||
|
* @author Kleber Hamada Sato <kleberhs007@yahoo.com>
|
||||||
|
*/
|
||||||
|
final class PrimeNumber extends AbstractRule
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function validate($input): bool
|
public function validate($input): bool
|
||||||
{
|
{
|
||||||
if (!is_numeric($input) || $input <= 1) {
|
if (!is_numeric($input) || $input <= 1) {
|
||||||
|
|
37
tests/integration/rules/primeNumber.phpt
Normal file
37
tests/integration/rules/primeNumber.phpt
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
|
use Respect\Validation\Exceptions\NestedValidationException;
|
||||||
|
use Respect\Validation\Exceptions\PrimeNumberException;
|
||||||
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
|
try {
|
||||||
|
v::primeNumber()->check(10);
|
||||||
|
} catch (PrimeNumberException $exception) {
|
||||||
|
echo $exception->getMessage().PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
v::not(v::primeNumber())->check(3);
|
||||||
|
} catch (PrimeNumberException $exception) {
|
||||||
|
echo $exception->getMessage().PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
v::primeNumber()->assert('Foo');
|
||||||
|
} catch (NestedValidationException $exception) {
|
||||||
|
echo $exception->getFullMessage().PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
v::not(v::primeNumber())->assert('+7');
|
||||||
|
} catch (NestedValidationException $exception) {
|
||||||
|
echo $exception->getFullMessage().PHP_EOL;
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
--EXPECTF--
|
||||||
|
10 must be a valid prime number
|
||||||
|
3 must not be a valid prime number
|
||||||
|
- "Foo" must be a valid prime number
|
||||||
|
- "+7" must not be a valid prime number
|
|
@ -13,70 +13,58 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Respect\Validation\Rules;
|
namespace Respect\Validation\Rules;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use Respect\Validation\Test\RuleTestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group rule
|
* @group rule
|
||||||
|
*
|
||||||
* @covers \Respect\Validation\Rules\PrimeNumber
|
* @covers \Respect\Validation\Rules\PrimeNumber
|
||||||
* @covers \Respect\Validation\Exceptions\PrimeNumberException
|
*
|
||||||
|
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||||
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||||
|
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||||
|
* @author Kleber Hamada Sato <kleberhs007@yahoo.com>
|
||||||
*/
|
*/
|
||||||
class PrimeNumberTest extends TestCase
|
final class PrimeNumberTest extends RuleTestCase
|
||||||
{
|
{
|
||||||
protected $object;
|
/*
|
||||||
|
* {@inheritdoc}
|
||||||
protected function setUp(): void
|
*/
|
||||||
|
public function providerForValidInput(): array
|
||||||
{
|
{
|
||||||
$this->object = new PrimeNumber();
|
$rule = new PrimeNumber();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForPrimeNumber
|
|
||||||
*/
|
|
||||||
public function testPrimeNumber($input): void
|
|
||||||
{
|
|
||||||
self::assertTrue($this->object->__invoke($input));
|
|
||||||
$this->object->check($input);
|
|
||||||
$this->object->assert($input);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerForNotPrimeNumber
|
|
||||||
* @expectedException \Respect\Validation\Exceptions\PrimeNumberException
|
|
||||||
*/
|
|
||||||
public function testNotPrimeNumber($input): void
|
|
||||||
{
|
|
||||||
self::assertFalse($this->object->__invoke($input));
|
|
||||||
$this->object->assert($input);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function providerForPrimeNumber()
|
|
||||||
{
|
|
||||||
return [
|
return [
|
||||||
[3],
|
[$rule, 3],
|
||||||
[5],
|
[$rule, 5],
|
||||||
[7],
|
[$rule, 7],
|
||||||
['3'],
|
[$rule, '3'],
|
||||||
['5'],
|
[$rule, '5'],
|
||||||
['+7'],
|
[$rule, '+7'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function providerForNotPrimeNumber()
|
/*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function providerForInvalidInput(): array
|
||||||
{
|
{
|
||||||
|
$rule = new PrimeNumber();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
[''],
|
[$rule, ''],
|
||||||
[null],
|
[$rule, null],
|
||||||
[0],
|
[$rule, 0],
|
||||||
[10],
|
[$rule, 10],
|
||||||
[25],
|
[$rule, 25],
|
||||||
[36],
|
[$rule, 36],
|
||||||
[-1],
|
[$rule, -1],
|
||||||
['-1'],
|
[$rule, '-1'],
|
||||||
['25'],
|
[$rule, '25'],
|
||||||
['0'],
|
[$rule, '0'],
|
||||||
['a'],
|
[$rule, 'a'],
|
||||||
[' '],
|
[$rule, ' '],
|
||||||
['Foo'],
|
[$rule, 'Foo'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue