mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
Most changes was made by php-cs-fixer. Also removes unused `RecursiveTreeIterator` class.
39 lines
976 B
PHP
39 lines
976 B
PHP
<?php
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use DateTime;
|
|
|
|
class Date extends AbstractRule
|
|
{
|
|
public $format = null;
|
|
|
|
public function __construct($format = null)
|
|
{
|
|
$this->format = $format;
|
|
}
|
|
|
|
public function validate($input)
|
|
{
|
|
if ($input instanceof DateTime) {
|
|
return true;
|
|
} elseif (!is_string($input)) {
|
|
return false;
|
|
} elseif (is_null($this->format)) {
|
|
return false !== strtotime($input);
|
|
}
|
|
|
|
$exceptionalFormats = array(
|
|
'c' => 'Y-m-d\TH:i:sP',
|
|
'r' => 'D, d M Y H:i:s O',
|
|
);
|
|
|
|
if (in_array($this->format, array_keys($exceptionalFormats))) {
|
|
$this->format = $exceptionalFormats[ $this->format ];
|
|
}
|
|
|
|
$dateFromFormat = DateTime::createFromFormat($this->format, $input);
|
|
|
|
return $dateFromFormat
|
|
&& $input === date($this->format, $dateFromFormat->getTimestamp());
|
|
}
|
|
}
|