mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 03:42:25 +02:00
Apply contribution guidelines to "LeapDate" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
73a0107349
commit
2bb6f66251
|
@ -13,6 +13,21 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class LeapDateException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class LeapDateException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be leap date',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be leap date',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -15,27 +15,45 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use function is_scalar;
|
||||
|
||||
class LeapDate extends AbstractRule
|
||||
/**
|
||||
* Validates if a date is leap.
|
||||
*
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jayson Reis <santosdosreis@gmail.com>
|
||||
*/
|
||||
final class LeapDate extends AbstractRule
|
||||
{
|
||||
public $format = null;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $format;
|
||||
|
||||
public function __construct($format)
|
||||
/**
|
||||
* Initializes the rule with the expected format.
|
||||
*
|
||||
* @param string $format
|
||||
*/
|
||||
public function __construct(string $format)
|
||||
{
|
||||
$this->format = $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (is_string($input)) {
|
||||
$date = DateTimeImmutable::createFromFormat($this->format, $input);
|
||||
} elseif ($input instanceof DateTimeInterface) {
|
||||
$date = $input;
|
||||
} else {
|
||||
return false;
|
||||
if ($input instanceof DateTimeInterface) {
|
||||
return '02-29' === $input->format('m-d');
|
||||
}
|
||||
|
||||
// Dates that aren't leap will aways be rounded
|
||||
return '02-29' == $date->format('m-d');
|
||||
if (is_scalar($input)) {
|
||||
return $this->validate(DateTimeImmutable::createFromFormat($this->format, (string) $input));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
37
tests/integration/rules/leapDate.phpt
Normal file
37
tests/integration/rules/leapDate.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\LeapDateException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::leapDate('Y-m-d')->check('1989-02-29');
|
||||
} catch (LeapDateException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::leapDate('Y-m-d'))->check('1988-02-29');
|
||||
} catch (LeapDateException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::leapDate('Y-m-d')->assert('1990-02-29');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::leapDate('Y-m-d'))->assert('1992-02-29');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"1989-02-29" must be leap date
|
||||
"1988-02-29" must not be leap date
|
||||
- "1990-02-29" must be leap date
|
||||
- "1992-02-29" must not be leap date
|
|
@ -14,61 +14,44 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use DateTime;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\LeapDateException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\LeapDate
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jayson Reis <santosdosreis@gmail.com>
|
||||
*/
|
||||
class LeapDateTest extends TestCase
|
||||
final class LeapDateTest extends RuleTestCase
|
||||
{
|
||||
protected $leapDateValidator;
|
||||
|
||||
protected function setUp(): void
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$this->leapDateValidator = new LeapDate('Y-m-d');
|
||||
return [
|
||||
[new LeapDate('Y-m-d'), '1988-02-29'],
|
||||
[new LeapDate('Y-m-d'), '1992-02-29'],
|
||||
[new LeapDate('Y-m-d'), new DateTime('1988-02-29')],
|
||||
[new LeapDate('Y-m-d'), new DateTime('1992-02-29')],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validLeapDate_with_string(): void
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
self::assertTrue($this->leapDateValidator->validate('1988-02-29'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function validLeapDate_with_date_time(): void
|
||||
{
|
||||
self::assertTrue($this->leapDateValidator->validate(
|
||||
new DateTime('1988-02-29')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function invalidLeapDate_with_string(): void
|
||||
{
|
||||
self::assertFalse($this->leapDateValidator->validate('1989-02-29'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function invalidLeapDate_with_date_time(): void
|
||||
{
|
||||
self::assertFalse($this->leapDateValidator->validate(
|
||||
new DateTime('1989-02-29')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function invalidLeapDate_input(): void
|
||||
{
|
||||
self::assertFalse($this->leapDateValidator->validate([]));
|
||||
return [
|
||||
[new LeapDate('Y-m-d'), '1989-02-29'],
|
||||
[new LeapDate('Y-m-d'), '1993-02-29'],
|
||||
[new LeapDate('Y-m-d'), new DateTime('1989-02-29')],
|
||||
[new LeapDate('Y-m-d'), new DateTime('1993-02-29')],
|
||||
[new LeapDate('Y-m-d'), []],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue