4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator;
14 use Symfony\Component\Validator\Context\ExecutionContextInterface as ExecutionContextInterface2Dot5;
15 use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
16 use Symfony\Component\Validator\Violation\LegacyConstraintViolationBuilder;
19 * Base class for constraint validators.
21 * @author Bernhard Schussek <bschussek@gmail.com>
23 abstract class ConstraintValidator implements ConstraintValidatorInterface
26 * Whether to format {@link \DateTime} objects as RFC-3339 dates
31 const PRETTY_DATE = 1;
34 * Whether to cast objects with a "__toString()" method to strings.
38 const OBJECT_TO_STRING = 2;
41 * @var ExecutionContextInterface2Dot5
48 public function initialize(ExecutionContextInterface $context)
50 $this->context = $context;
54 * Wrapper for {@link ExecutionContextInterface::buildViolation} that
55 * supports the 2.4 context API.
57 * @param string $message The violation message
58 * @param array $parameters The message parameters
60 * @return ConstraintViolationBuilderInterface The violation builder
62 * @deprecated since version 2.5, to be removed in 3.0.
64 protected function buildViolation($message, array $parameters = array())
66 @trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
68 if ($this->context instanceof ExecutionContextInterface2Dot5) {
69 return $this->context->buildViolation($message, $parameters);
72 return new LegacyConstraintViolationBuilder($this->context, $message, $parameters);
76 * Wrapper for {@link ExecutionContextInterface::buildViolation} that
77 * supports the 2.4 context API.
79 * @param ExecutionContextInterface $context The context to use
80 * @param string $message The violation message
81 * @param array $parameters The message parameters
83 * @return ConstraintViolationBuilderInterface The violation builder
85 * @deprecated since version 2.5, to be removed in 3.0.
87 protected function buildViolationInContext(ExecutionContextInterface $context, $message, array $parameters = array())
89 @trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
91 if ($context instanceof ExecutionContextInterface2Dot5) {
92 return $context->buildViolation($message, $parameters);
95 return new LegacyConstraintViolationBuilder($context, $message, $parameters);
99 * Returns a string representation of the type of the value.
101 * This method should be used if you pass the type of a value as
102 * message parameter to a constraint violation. Note that such
103 * parameters should usually not be included in messages aimed at
104 * non-technical people.
106 * @param mixed $value The value to return the type of
108 * @return string The type of the value
110 protected function formatTypeOf($value)
112 return is_object($value) ? get_class($value) : gettype($value);
116 * Returns a string representation of the value.
118 * This method returns the equivalent PHP tokens for most scalar types
119 * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
120 * in double quotes ("). Objects, arrays and resources are formatted as
121 * "object", "array" and "resource". If the $format bitmask contains
122 * the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted
123 * as RFC-3339 dates ("Y-m-d H:i:s").
125 * Be careful when passing message parameters to a constraint violation
126 * that (may) contain objects, arrays or resources. These parameters
127 * should only be displayed for technical users. Non-technical users
128 * won't know what an "object", "array" or "resource" is and will be
129 * confused by the violation message.
131 * @param mixed $value The value to format as string
132 * @param int $format A bitwise combination of the format
133 * constants in this class
135 * @return string The string representation of the passed value
137 protected function formatValue($value, $format = 0)
139 $isDateTime = $value instanceof \DateTime || $value instanceof \DateTimeInterface;
141 if (($format & self::PRETTY_DATE) && $isDateTime) {
142 if (class_exists('IntlDateFormatter')) {
143 $locale = \Locale::getDefault();
144 $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
146 // neither the native nor the stub IntlDateFormatter support
147 // DateTimeImmutable as of yet
148 if (!$value instanceof \DateTime) {
149 $value = new \DateTime(
150 $value->format('Y-m-d H:i:s.u e'),
151 $value->getTimezone()
155 return $formatter->format($value);
158 return $value->format('Y-m-d H:i:s');
161 if (is_object($value)) {
162 if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
163 return $value->__toString();
169 if (is_array($value)) {
173 if (is_string($value)) {
174 return '"'.$value.'"';
177 if (is_resource($value)) {
181 if (null === $value) {
185 if (false === $value) {
189 if (true === $value) {
193 return (string) $value;
197 * Returns a string representation of a list of values.
199 * Each of the values is converted to a string using
200 * {@link formatValue()}. The values are then concatenated with commas.
202 * @param array $values A list of values
203 * @param int $format A bitwise combination of the format
204 * constants in this class
206 * @return string The string representation of the value list
210 protected function formatValues(array $values, $format = 0)
212 foreach ($values as $key => $value) {
213 $values[$key] = $this->formatValue($value, $format);
216 return implode(', ', $values);