2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class DateValidator extends ConstraintValidator
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2})$/';
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function isValid($value, Constraint $constraint)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-12-21 02:59:17 +00:00
|
|
|
if (null === $value || '' === $value) {
|
2010-06-24 10:24:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-03-01 22:35:57 +00:00
|
|
|
if ($value instanceof \DateTime) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-07 20:01:44 +01:00
|
|
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
2010-06-24 10:24:08 +01:00
|
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-03-31 13:02:00 +01:00
|
|
|
$value = (string) $value;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-03-01 22:37:10 +00:00
|
|
|
if (!preg_match(static::PATTERN, $value, $matches)) {
|
2010-09-29 06:59:45 +01:00
|
|
|
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
return checkdate($matches[2], $matches[3], $matches[1]);
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|