Skip to content

Commit

Permalink
Accept any DateTimeInterface as valid.
Browse files Browse the repository at this point in the history
This fixes validators not accepting immutable datetime objects.

Refs #8333
  • Loading branch information
markstory committed Feb 25, 2016
1 parent bca53dc commit f3462c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Validation/Validation.php
Expand Up @@ -15,7 +15,7 @@
namespace Cake\Validation;

use Cake\Utility\Text;
use DateTime;
use DateTimeInterface;
use LogicException;
use NumberFormatter;
use RuntimeException;
Expand Down Expand Up @@ -347,15 +347,15 @@ public static function custom($check, $regex = null)
* - `ym` 2006/12 or 06/12 separators can be a space, period, dash, forward slash
* - `y` 2006 just the year without any separators
*
* @param string|\DateTime $check a valid date string/object
* @param string|\DateTimeInterface $check a valid date string/object
* @param string|array $format Use a string or an array of the keys above.
* Arrays should be passed as ['dmy', 'mdy', etc]
* @param string|null $regex If a custom regular expression is used this is the only validation that will occur.
* @return bool Success
*/
public static function date($check, $format = 'ymd', $regex = null)
{
if ($check instanceof DateTime) {
if ($check instanceof DateTimeInterface) {
return true;
}

Expand Down Expand Up @@ -411,7 +411,7 @@ public static function date($check, $format = 'ymd', $regex = null)
*
* All values matching the "date" core validation rule, and the "time" one will be valid
*
* @param string|\DateTime $check Value to check
* @param string|\DateTimeInterface $check Value to check
* @param string|array $dateFormat Format of the date part. See Validation::date() for more information.
* @param string|null $regex Regex for the date part. If a custom regular expression is used this is the only validation that will occur.
* @return bool True if the value is valid, false otherwise
Expand All @@ -420,7 +420,7 @@ public static function date($check, $format = 'ymd', $regex = null)
*/
public static function datetime($check, $dateFormat = 'ymd', $regex = null)
{
if ($check instanceof DateTime) {
if ($check instanceof DateTimeInterface) {
return true;
}
$valid = false;
Expand All @@ -442,12 +442,12 @@ public static function datetime($check, $dateFormat = 'ymd', $regex = null)
* Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m)
* Does not allow/validate seconds.
*
* @param string|\DateTime $check a valid time string/object
* @param string|\DateTimeInterface $check a valid time string/object
* @return bool Success
*/
public static function time($check)
{
if ($check instanceof DateTime) {
if ($check instanceof DateTimeInterface) {
return true;
}
if (is_array($check)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -900,6 +900,11 @@ public function testDateTimeObject()
$this->assertTrue(Validation::date($dateTime));
$this->assertTrue(Validation::time($dateTime));
$this->assertTrue(Validation::dateTime($dateTime));

$dateTime = new \DateTimeImmutable();
$this->assertTrue(Validation::date($dateTime));
$this->assertTrue(Validation::time($dateTime));
$this->assertTrue(Validation::dateTime($dateTime));
}

/**
Expand Down

0 comments on commit f3462c0

Please sign in to comment.