mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 06:42:22 +02:00
Apply contribution guidelines to "LeapYear" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com> Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
5b6184d4e2
commit
083ccec068
|
@ -13,6 +13,21 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class LeapYearException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class LeapYearException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a leap year',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be a leap year',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -14,23 +14,40 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use DateTimeInterface;
|
||||
use function date;
|
||||
use function is_numeric;
|
||||
use function is_scalar;
|
||||
use function sprintf;
|
||||
use function strtotime;
|
||||
|
||||
class LeapYear extends AbstractRule
|
||||
/**
|
||||
* Validates if a year is leap.
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jayson Reis <santosdosreis@gmail.com>
|
||||
*/
|
||||
final class LeapYear extends AbstractRule
|
||||
{
|
||||
public function validate($year): bool
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (is_numeric($year)) {
|
||||
$year = (int) $year;
|
||||
} elseif (is_string($year)) {
|
||||
$year = (int) date('Y', (int) strtotime($year));
|
||||
} elseif ($year instanceof DateTimeInterface) {
|
||||
$year = (int) $year->format('Y');
|
||||
} else {
|
||||
return false;
|
||||
if (is_numeric($input)) {
|
||||
$date = strtotime(sprintf('%d-02-29', (int) $input));
|
||||
|
||||
return (bool) date('L', (int) $date);
|
||||
}
|
||||
|
||||
$date = strtotime(sprintf('%d-02-29', $year));
|
||||
if (is_scalar($input)) {
|
||||
return $this->validate((int) date('Y', (int) strtotime((string) $input)));
|
||||
}
|
||||
|
||||
return (bool) date('L', (int) $date);
|
||||
if ($input instanceof DateTimeInterface) {
|
||||
return $this->validate($input->format('Y'));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
38
tests/integration/rules/leapYear.phpt
Normal file
38
tests/integration/rules/leapYear.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\LeapYearException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::leapYear()->check('2009');
|
||||
} catch (LeapYearException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::leapYear())->check('2008');
|
||||
} catch (LeapYearException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::leapYear()->assert('2009-02-29');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::leapYear())->assert('2008');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
"2009" must be a leap year
|
||||
"2008" must not be a leap year
|
||||
- "2009-02-29" must be a leap year
|
||||
- "2008" must not be a leap year
|
|
@ -14,45 +14,49 @@ 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\LeapYearException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\LeapYear
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jayson Reis <santosdosreis@gmail.com>
|
||||
*/
|
||||
class LeapYearTest extends TestCase
|
||||
final class LeapYearTest extends RuleTestCase
|
||||
{
|
||||
protected $leapYearValidator;
|
||||
|
||||
protected function setUp(): void
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$this->leapYearValidator = new LeapYear();
|
||||
$rule = new LeapYear();
|
||||
|
||||
return [
|
||||
[$rule, '2008'],
|
||||
[$rule, '2008-02-29'],
|
||||
[$rule, 2008],
|
||||
[$rule, 2008],
|
||||
[$rule, new DateTime('2008-02-29')],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validLeapDate(): void
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
self::assertTrue($this->leapYearValidator->__invoke('2008'));
|
||||
self::assertTrue($this->leapYearValidator->__invoke('2008-02-29'));
|
||||
self::assertTrue($this->leapYearValidator->__invoke(2008));
|
||||
self::assertTrue($this->leapYearValidator->__invoke(
|
||||
new DateTime('2008-02-29')));
|
||||
}
|
||||
$rule = new LeapYear();
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function invalidLeapDate(): void
|
||||
{
|
||||
self::assertFalse($this->leapYearValidator->__invoke(''));
|
||||
self::assertFalse($this->leapYearValidator->__invoke('2009'));
|
||||
self::assertFalse($this->leapYearValidator->__invoke('2009-02-29'));
|
||||
self::assertFalse($this->leapYearValidator->__invoke(2009));
|
||||
self::assertFalse($this->leapYearValidator->__invoke(
|
||||
new DateTime('2009-02-29')));
|
||||
self::assertFalse($this->leapYearValidator->__invoke([]));
|
||||
return [
|
||||
[$rule, ''],
|
||||
[$rule, '2009'],
|
||||
[$rule, '2009-02-29'],
|
||||
[$rule, 2009],
|
||||
[$rule, new DateTime('2009-02-29')],
|
||||
[$rule, []],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue