2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
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 TimeValidator extends ConstraintValidator
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
const PATTERN = '/(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])/';
|
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-06-24 10:24:08 +01:00
|
|
|
if ($value === null) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString()'))) {
|
|
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$value = (string)$value;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!preg_match(self::PATTERN, $value)) {
|
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 true;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|