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;
|
|
|
|
|
2012-04-17 15:54:40 +01:00
|
|
|
use Symfony\Component\Validator\Exception\ValidatorException;
|
|
|
|
|
2012-01-30 19:56:10 +00:00
|
|
|
/**
|
|
|
|
* Base class for constraint validators
|
|
|
|
*
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*
|
2011-07-20 09:37:57 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-06-24 09:40:05 +01:00
|
|
|
abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|
|
|
{
|
2011-05-17 22:04:13 +01:00
|
|
|
/**
|
|
|
|
* @var ExecutionContext
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $context;
|
2012-02-01 12:53:45 +00:00
|
|
|
|
2011-05-17 22:04:13 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
2012-02-01 12:53:45 +00:00
|
|
|
*
|
|
|
|
* @deprecated
|
2011-05-17 22:04:13 +01:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
private $messageTemplate;
|
2012-02-01 12:53:45 +00:00
|
|
|
|
2011-05-17 22:04:13 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
2012-02-01 12:53:45 +00:00
|
|
|
*
|
|
|
|
* @deprecated
|
2011-05-17 22:04:13 +01:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
private $messageParameters;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2011-01-19 14:02:00 +00:00
|
|
|
public function initialize(ExecutionContext $context)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$this->context = $context;
|
|
|
|
$this->messageTemplate = '';
|
|
|
|
$this->messageParameters = array();
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
2012-04-17 15:54:40 +01:00
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getMessageTemplate()
|
|
|
|
{
|
|
|
|
return $this->messageTemplate;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
2012-04-17 15:54:40 +01:00
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getMessageParameters()
|
|
|
|
{
|
|
|
|
return $this->messageParameters;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-07-20 09:37:57 +01:00
|
|
|
/**
|
2012-02-01 12:53:45 +00:00
|
|
|
* Wrapper for $this->context->addViolation()
|
|
|
|
*
|
2012-04-17 15:54:40 +01:00
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
2011-07-20 09:37:57 +01:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
protected function setMessage($template, array $parameters = array())
|
|
|
|
{
|
|
|
|
$this->messageTemplate = $template;
|
|
|
|
$this->messageParameters = $parameters;
|
2012-02-01 12:53:45 +00:00
|
|
|
|
2012-02-05 09:03:22 +00:00
|
|
|
if (!$this->context instanceof ExecutionContext) {
|
|
|
|
throw new ValidatorException('ConstraintValidator::initialize() must be called before setting violation messages');
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->addViolation($template, $parameters);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2012-04-17 15:54:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stub implementation delegating to the deprecated isValid method.
|
|
|
|
*
|
|
|
|
* This stub exists for BC and will be dropped in Symfony 2.3.
|
|
|
|
*
|
|
|
|
* @see ConstraintValidatorInterface::validate
|
|
|
|
*/
|
|
|
|
public function validate($value, Constraint $constraint)
|
|
|
|
{
|
2012-04-17 16:19:12 +01:00
|
|
|
return $this->isValid($value, $constraint);
|
2012-04-17 15:54:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BC variant of validate.
|
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
|
|
|
*/
|
|
|
|
protected function isValid($value, Constraint $constraint)
|
|
|
|
{
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|