2010-09-27 23:02:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2010-10-18 06:26:02 +02:00
|
|
|
use DateTime;
|
2010-09-27 23:02:30 +02:00
|
|
|
use Respect\Validation\Rules\AbstractDate;
|
2010-11-30 16:36:04 +01:00
|
|
|
use Respect\Validation\Exceptions\DateException;
|
2010-09-27 23:02:30 +02:00
|
|
|
|
2010-10-18 06:26:02 +02:00
|
|
|
class Date extends AbstractDate
|
2010-09-27 23:02:30 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
public function __construct($format=null)
|
|
|
|
{
|
|
|
|
$this->format = $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validate($input)
|
|
|
|
{
|
2010-10-18 06:26:02 +02:00
|
|
|
if ($input instanceof DateTime)
|
|
|
|
return true;
|
2010-10-18 07:05:37 +02:00
|
|
|
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-02 22:04:10 +01:00
|
|
|
public function createException()
|
|
|
|
{
|
|
|
|
return new DateException;
|
|
|
|
}
|
|
|
|
|
2010-09-27 23:02:30 +02:00
|
|
|
|
|
|
|
public function assert($input)
|
|
|
|
{
|
|
|
|
if (!$this->validate($input))
|
2010-12-02 22:04:10 +01:00
|
|
|
throw $this
|
|
|
|
->getException()
|
|
|
|
->setParams($input, $this->format);
|
2010-09-27 23:02:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-18 11:15:44 +02:00
|
|
|
public function check($input)
|
|
|
|
{
|
|
|
|
return $this->assert($input);
|
|
|
|
}
|
|
|
|
|
2010-09-27 23:02:30 +02:00
|
|
|
}
|