Rename rule "Date" to "DateTime"

Also change rules to use `DateTimeInterface` and `DateTimeImmutable`
when possible.
This commit is contained in:
Henrique Moody 2017-05-01 08:54:18 +02:00
parent 14d87da7d8
commit 0338e18e83
No known key found for this signature in database
GPG key ID: 221E9281655813A6
28 changed files with 114 additions and 112 deletions

View file

@ -47,6 +47,6 @@ Version | Description
See also:
- [Between](Between.md)
- [Date](Date.md)
- [DateTime](DateTime.md)
- [Max](Max.md)
- [Min](Min.md)

View file

@ -19,19 +19,19 @@ v::stringType()->between('a', 'f')->validate('c'); // true
Also very powerful with dates:
```php
v::date()->between('2009-01-01', '2013-01-01')->validate('2010-01-01'); // true
v::dateTime()->between('2009-01-01', '2013-01-01')->validate('2010-01-01'); // true
```
Date ranges accept strtotime values:
```php
v::date()->between('yesterday', 'tomorrow')->validate('now'); // true
v::dateTime()->between('yesterday', 'tomorrow')->validate('now'); // true
```
A third parameter may be passed to validate the passed values inclusive:
```php
v::date()->between(10, 20, true)->validate(20); // true
v::dateTime()->between(10, 20, true)->validate(20); // true
```
Message template for this validator includes `{{minValue}}` and `{{maxValue}}`.

View file

@ -1,46 +0,0 @@
# Date
- `Date()`
- `Date(string $format)`
Validates if input is a date:
```php
v::date()->validate('2009-01-01'); // true
```
Also accepts strtotime values:
```php
v::date()->validate('now'); // true
```
And DateTime instances:
```php
v::date()->validate(new DateTime); // true
```
You can pass a format when validating strings:
```php
v::date('Y-m-d')->validate('01-01-2009'); // false
```
Format has no effect when validating DateTime instances.
Message template for this validator includes `{{format}}`.
## Changelog
Version | Description
--------|-------------
0.3.9 | Created
***
See also:
- [Between](Between.md)
- [MinimumAge](MinimumAge.md)
- [LeapDate](LeapDate.md)
- [LeapYear](LeapYear.md)

49
docs/DateTime.md Normal file
View file

@ -0,0 +1,49 @@
# DateTime
- `DateTime()`
- `DateTime(string $format)`
Validates if input is a date. The `$format` argument should be in accordance to
PHP's [date()](http://php.net/date) function.
```php
v::dateTime()->validate('2009-01-01'); // true
```
Also accepts strtotime values:
```php
v::dateTime()->validate('now'); // true
```
And `DateTimeInterface` instances:
```php
v::dateTime()->validate(new DateTime()); // true
v::dateTime()->validate(new DateTimeImmutable()); // true
```
You can pass a format when validating strings:
```php
v::dateTime('Y-m-d')->validate('01-01-2009'); // false
```
Format has no effect when validating DateTime instances.
Message template for this validator includes `{{format}}`.
## Changelog
Version | Description
--------|-------------
2.0.0 | Renamed from `Date` to `DateTime`
0.3.9 | Created as `Date`
***
See also:
- [Between](Between.md)
- [MinimumAge](MinimumAge.md)
- [LeapDate](LeapDate.md)
- [LeapYear](LeapYear.md)

View file

@ -14,8 +14,8 @@ $releaseDates = [
'relational' => '2011-02-05',
];
v::arrayVal()->each(v::date())->validate($releaseDates); // true
v::arrayVal()->each(v::date(), v::stringType()->lowercase())->validate($releaseDates); // true
v::arrayVal()->each(v::dateTime())->validate($releaseDates); // true
v::arrayVal()->each(v::dateTime(), v::stringType()->lowercase())->validate($releaseDates); // true
```
Using `arrayVal()` before `each()` is a best practice.

View file

@ -20,5 +20,5 @@ Version | Description
***
See also:
- [Date](Date.md)
- [DateTime](DateTime.md)
- [LeapYear](LeapYear.md)

View file

@ -19,5 +19,5 @@ Version | Description
***
See also:
- [Date](Date.md)
- [DateTime](DateTime.md)
- [LeapDate](LeapDate.md)

View file

@ -14,14 +14,14 @@ v::intVal()->max(20, true)->validate(20); // true
Also accepts dates:
```php
v::date()->max('2012-01-01')->validate('2010-01-01'); // true
v::dateTime()->max('2012-01-01')->validate('2010-01-01'); // true
```
Also date intervals:
```php
// Same of minimum age validation
v::date()->max('-18 years')->validate('1988-09-09'); // true
v::dateTime()->max('-18 years')->validate('1988-09-09'); // true
```
`true` may be passed as a parameter to indicate that inclusive

View file

@ -14,7 +14,7 @@ v::intVal()->min(5, true)->validate(5); // true
Also accepts dates:
```php
v::date()->min('2012-01-01')->validate('2015-01-01'); // true
v::dateTime()->min('2012-01-01')->validate('2015-01-01'); // true
```
`true` may be passed as a parameter to indicate that inclusive

View file

@ -12,7 +12,7 @@ v::minimumAge(18)->validate('1987-01-01'); // true
v::minimumAge(18, 'd/m/Y')->validate('01/01/1987'); // true
```
Using `date()` before is a best-practice.
Using [DateTime](DateTime.md) before is a best-practice.
Message template for this validator includes `{{age}}`.
@ -26,4 +26,4 @@ Version | Description
See also:
- [Age](Age.md)
- [Date](Date.md)
- [DateTime](DateTime.md)

View file

@ -42,14 +42,14 @@ Is possible to validate its attributes in a single chain:
```php
$userValidator = v::attribute('name', v::stringType()->length(1,32))
->attribute('birthdate', v::date()->age(18));
->attribute('birthdate', v::dateTime()->age(18));
$userValidator->validate($user); // true
```
Validating array keys is also possible using `v::key()`
Note that we used `v::stringType()` and `v::date()` in the beginning of the validator.
Note that we used `v::stringType()` and `v::dateTime()` in the beginning of the validator.
Although is not mandatory, it is a good practice to use the type of the
validated object as the first node in the chain.
@ -305,7 +305,7 @@ On `v::attribute()` and `v::key()`, `{{name}}` is the attribute/key name. For ot
is the same as the input. You can customize a validator name using:
```php
v::date('Y-m-d')->between('1980-02-02', 'now')->setName('Member Since');
v::dateTime('Y-m-d')->between('1980-02-02', 'now')->setName('Member Since');
```
## Zend/Symfony validators

View file

@ -8,7 +8,7 @@
- [BoolType](BoolType.md)
- [CallableType](CallableType.md)
- [Countable](Countable.md)
- [Date](Date.md)
- [DateTime](DateTime.md)
- [FalseVal](FalseVal.md)
- [FloatVal](FloatVal.md)
- [FloatType](FloatType.md)
@ -130,7 +130,7 @@
- [Age](Age.md)
- [Between](Between.md)
- [Date](Date.md)
- [DateTime](DateTime.md)
- [LeapDate](LeapDate.md)
- [LeapYear](LeapYear.md)
- [MinimumAge](MinimumAge.md)
@ -239,7 +239,7 @@
- [Cpf](Cpf.md)
- [CreditCard](CreditCard.md)
- [CurrencyCode](CurrencyCode.md)
- [Date](Date.md)
- [DateTime](DateTime.md)
- [Digit](Digit.md)
- [Directory](Directory.md)
- [Domain](Domain.md)

View file

@ -11,18 +11,18 @@
namespace Respect\Validation\Exceptions;
class DateException extends ValidationException
class DateTimeException extends ValidationException
{
const FORMAT = 1;
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid date',
self::FORMAT => '{{name}} must be a valid date. Sample format: {{format}}',
self::STANDARD => '{{name}} must be a valid date/time',
self::FORMAT => '{{name}} must be a valid date/time. Sample format: {{format}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be a valid date',
self::FORMAT => '{{name}} must not be a valid date in the format {{format}}',
self::STANDARD => '{{name}} must not be a valid date/time',
self::FORMAT => '{{name}} must not be a valid date/time in the format {{format}}',
],
];

View file

@ -11,7 +11,7 @@
namespace Respect\Validation\Rules;
use DateTime;
use DateTimeImmutable;
use Exception;
abstract class AbstractInterval extends AbstractRule
@ -36,7 +36,7 @@ abstract class AbstractInterval extends AbstractRule
}
try {
return new DateTime($value);
return new DateTimeImmutable($value);
} catch (Exception $e) {
// Pokémon Exception Handling
}

View file

@ -11,7 +11,7 @@
namespace Respect\Validation\Rules;
use DateTime;
use DateTime as DateTimeMutable;
use Respect\Validation\Exceptions\ComponentException;
class Age extends AllOf
@ -37,7 +37,7 @@ class Age extends AllOf
{
$interval = sprintf('-%d years', $age);
return new DateTime($interval);
return new DateTimeMutable($interval);
}
private function setMaxAge($maxAge)

View file

@ -11,10 +11,9 @@
namespace Respect\Validation\Rules;
use DateTime;
use DateTimeInterface;
class Date extends AbstractRule
class DateTime extends AbstractRule
{
public $format = null;
@ -25,8 +24,7 @@ class Date extends AbstractRule
public function validate($input)
{
if ($input instanceof DateTimeInterface
|| $input instanceof DateTime) {
if ($input instanceof DateTimeInterface) {
return true;
}

View file

@ -11,7 +11,8 @@
namespace Respect\Validation\Rules;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
class LeapDate extends AbstractRule
{
@ -25,8 +26,8 @@ class LeapDate extends AbstractRule
public function validate($input)
{
if (is_string($input)) {
$date = DateTime::createFromFormat($this->format, $input);
} elseif ($input instanceof DateTime) {
$date = DateTimeImmutable::createFromFormat($this->format, $input);
} elseif ($input instanceof DateTimeInterface) {
$date = $input;
} else {
return false;

View file

@ -11,7 +11,7 @@
namespace Respect\Validation\Rules;
use DateTime;
use DateTimeInterface;
class LeapYear extends AbstractRule
{
@ -21,7 +21,7 @@ class LeapYear extends AbstractRule
$year = (int) $year;
} elseif (is_string($year)) {
$year = (int) date('Y', strtotime($year));
} elseif ($year instanceof DateTime) {
} elseif ($year instanceof DateTimeInterface) {
$year = (int) $year->format('Y');
} else {
return false;

View file

@ -11,7 +11,8 @@
namespace Respect\Validation\Rules;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Respect\Validation\Exceptions\ComponentException;
class MinimumAge extends AbstractRule
@ -31,8 +32,8 @@ class MinimumAge extends AbstractRule
public function validate($input)
{
if ($input instanceof DateTime) {
$birthday = new \DateTime('now - '.$this->age.' year');
if ($input instanceof DateTimeInterface) {
$birthday = new DateTimeImmutable('now - '.$this->age.' year');
return $birthday > $input;
}

View file

@ -51,7 +51,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator currencyCode()
* @method static Validator cpf()
* @method static Validator creditCard(string $brand = null)
* @method static Validator date(string $format = null)
* @method static Validator dateTime(string $format = null)
* @method static Validator digit(string $additionalChars = null)
* @method static Validator directory()
* @method static Validator domain(bool $tldCheck = true)

View file

@ -18,7 +18,7 @@ try {
];
Validator::key('username', Validator::length(2, 32))
->key('birthdate', Validator::date())
->key('birthdate', Validator::dateTime())
->key('password', Validator::notEmpty())
->key('email', Validator::email())
->assert($input);
@ -30,7 +30,7 @@ try {
Array
(
[0] => username must have a length between 2 and 32
[1] => birthdate must be a valid date
[1] => birthdate must be a valid date/time
[2] => password must not be empty
[3] => Key email must be present
)

View file

@ -12,7 +12,7 @@ use Respect\Validation\Validator;
try {
Validator::key('username', Validator::length(2, 32))
->key('birthdate', Validator::date())
->key('birthdate', Validator::dateTime())
->setName('User Subscription Form')
->assert(['username' => '0', 'birthdate' => 'Whatever']);
} catch (NestedValidationException $e) {
@ -22,4 +22,4 @@ try {
--EXPECTF--
- All of the required rules must pass for User Subscription Form
- username must have a length between 2 and 32
- birthdate must be a valid date
- birthdate must be a valid date/time

View file

@ -12,7 +12,7 @@ $user->name = 'Alexandre';
$user->birthdate = '1987-07-01';
$userValidator = v::attribute('name', v::stringType()->length(1, 32))
->attribute('birthdate', v::date()->age(18));
->attribute('birthdate', v::dateTime()->age(18));
$userValidator->assert($user);
?>

View file

@ -9,7 +9,7 @@ date_default_timezone_set('UTC');
v::allOf(v::intVal(), v::positive())->assert(42);
v::allOf(v::intVal(), v::negative())->check(-42);
v::not(v::allOf(v::date(), v::between('2014-12-01', '2014-12-12')))->assert('2012-01-01');
v::not(v::allOf(v::dateTime(), v::between('2014-12-01', '2014-12-12')))->assert('2012-01-01');
v::not(v::allOf(v::stringType(), v::consonant()))->check('I am Jack\'s smirking revenge');
?>
--EXPECTF--

View file

@ -8,7 +8,7 @@ use Respect\Validation\Validator as v;
v::intType()->between(1, 42)->check(2);
v::intType()->between(1, 2)->assert(2);
v::date()->between('1989-12-20', 'tomorrow', false)->assert(new DateTime());
v::dateTime()->between('1989-12-20', 'tomorrow', false)->assert(new DateTime());
v::stringType()->between('a', 'e', false)->assert('d');
?>

View file

@ -93,7 +93,7 @@ class FactoryTest extends \PHPUnit_Framework_TestCase
public function testShouldDefineConstructorArgumentsWhenCreatingARule()
{
$factory = new Factory();
$rule = $factory->rule('date', ['Y-m-d']);
$rule = $factory->rule('dateTime', ['Y-m-d']);
$this->assertEquals('Y-m-d', $rule->format);
}

View file

@ -11,21 +11,20 @@
namespace Respect\Validation\Rules;
use DateTime;
use DateTimeImmutable;
/**
* @group rule
* @covers \Respect\Validation\Rules\Date
* @covers \Respect\Validation\Exceptions\DateException
* @covers \Respect\Validation\Rules\DateTime
* @covers \Respect\Validation\Exceptions\DateTimeException
*/
class DateTest extends \PHPUnit_Framework_TestCase
class DateTimeTest extends \PHPUnit_Framework_TestCase
{
protected $dateValidator;
protected function setUp()
{
$this->dateValidator = new Date();
$this->dateValidator = new DateTime();
}
public function testDateEmptyShouldNotValidate()
@ -34,7 +33,7 @@ class DateTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException \Respect\Validation\Exceptions\DateException
* @expectedException \Respect\Validation\Exceptions\DateTimeException
*/
public function testDateEmptyShouldNotCheck()
{
@ -42,7 +41,7 @@ class DateTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException \Respect\Validation\Exceptions\DateException
* @expectedException \Respect\Validation\Exceptions\DateTimeException
*/
public function testDateEmptyShouldNotAssert()
{
@ -56,7 +55,7 @@ class DateTest extends \PHPUnit_Framework_TestCase
public function testDateTimeInstancesShouldAlwaysValidate()
{
$this->assertTrue($this->dateValidator->__invoke(new DateTime('today')));
$this->assertTrue($this->dateValidator->__invoke(new \DateTime('today')));
}
public function testDateTimeImmutableInterfaceInstancesShouldAlwaysValidate()
@ -85,30 +84,30 @@ class DateTest extends \PHPUnit_Framework_TestCase
public function testFormatsShouldValidateDateStrings()
{
$this->dateValidator = new Date('Y-m-d');
$this->dateValidator = new DateTime('Y-m-d');
$this->assertTrue($this->dateValidator->assert('2009-09-09'));
}
public function testFormatsShouldValidateDateStrings_with_any_formats()
{
$this->dateValidator = new Date('d/m/Y');
$this->dateValidator = new DateTime('d/m/Y');
$this->assertTrue($this->dateValidator->assert('23/05/1987'));
}
/**
* @expectedException \Respect\Validation\Exceptions\DateException
* @expectedException \Respect\Validation\Exceptions\DateTimeException
*/
public function testFormatsShouldValidateDateStrings_and_throw_DateException_on_failure()
public function testFormatsShouldValidateDateStrings_and_throw_DateTimeException_on_failure()
{
$this->dateValidator = new Date('y-m-d');
$this->dateValidator = new DateTime('y-m-d');
$this->assertFalse($this->dateValidator->assert('2009-09-09'));
}
public function testDateTimeExceptionalFormatsThatShouldBeValid()
{
$this->dateValidator = new Date('c');
$this->dateValidator = new DateTime('c');
$this->assertTrue($this->dateValidator->assert('2004-02-12T15:19:21+00:00'));
$this->dateValidator = new Date('r');
$this->dateValidator = new DateTime('r');
$this->assertTrue($this->dateValidator->assert('Thu, 29 Dec 2005 01:02:03 +0000'));
}
@ -122,7 +121,7 @@ class DateTest extends \PHPUnit_Framework_TestCase
public function testDateTimeSystemTimezoneIndependent($systemTimezone, $format, $input)
{
date_default_timezone_set($systemTimezone);
$this->dateValidator = new Date($format);
$this->dateValidator = new DateTime($format);
$this->assertTrue($this->dateValidator->assert($input));
}

View file

@ -47,7 +47,7 @@ class ZendTest extends \PHPUnit_Framework_TestCase
public function testConstructorWithZendValidatorInstance()
{
$zendInstance = new Date();
$zendInstance = new ZendDate();
$v = new Zend($zendInstance);
$this->assertAttributeSame(
$expected = $zendInstance,