respect-validation/library/Respect/Validation/Rules/Date.php

38 lines
871 B
PHP
Raw Normal View History

2010-09-27 23:02:30 +02:00
<?php
namespace Respect\Validation\Rules;
2010-12-03 01:23:02 +01:00
use DateTime;
2010-09-27 23:02:30 +02:00
2010-12-03 01:23:02 +01:00
class Date extends AbstractRule
2010-09-27 23:02:30 +02:00
{
2010-12-03 01:23:02 +01:00
const FORMAT_DEFAULT = DateTime::RFC1036;
protected $format = self::FORMAT_DEFAULT;
protected function formatDate(DateTime $date)
{
return $date->format($this->format);
}
2010-09-27 23:02:30 +02:00
public function __construct($format=null)
{
$this->format = $format;
}
public function reportError($input, array $related=array())
{
return parent::reportError($input, $related, $this->format);
}
2010-09-27 23:02:30 +02:00
public function validate($input)
{
if ($input instanceof DateTime)
return true;
if (!is_string($input))
return false;
2010-09-27 23:02:30 +02:00
if (is_null($this->format))
return (boolean) strtotime($input);
else
return date($this->format, strtotime($input)) == $input;
}
2010-12-03 01:23:02 +01:00
2010-09-27 23:02:30 +02:00
}