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;
17 * Base class for constraint validators.
19 * @author Bernhard Schussek <bschussek@gmail.com>
21 abstract class ConstraintValidator implements ConstraintValidatorInterface
24 * Whether to format {@link \DateTime} objects as RFC-3339 dates
27 const PRETTY_DATE = 1;
30 * Whether to cast objects with a "__toString()" method to strings.
32 const OBJECT_TO_STRING = 2;
35 * @var ExecutionContextInterface
42 public function initialize(ExecutionContextInterface $context)
44 $this->context = $context;
48 * Returns a string representation of the type of the value.
50 * This method should be used if you pass the type of a value as
51 * message parameter to a constraint violation. Note that such
52 * parameters should usually not be included in messages aimed at
53 * non-technical people.
55 * @param mixed $value The value to return the type of
57 * @return string The type of the value
59 protected function formatTypeOf($value)
61 return is_object($value) ? get_class($value) : gettype($value);
65 * Returns a string representation of the value.
67 * This method returns the equivalent PHP tokens for most scalar types
68 * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
69 * in double quotes ("). Objects, arrays and resources are formatted as
70 * "object", "array" and "resource". If the $format bitmask contains
71 * the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted
72 * as RFC-3339 dates ("Y-m-d H:i:s").
74 * Be careful when passing message parameters to a constraint violation
75 * that (may) contain objects, arrays or resources. These parameters
76 * should only be displayed for technical users. Non-technical users
77 * won't know what an "object", "array" or "resource" is and will be
78 * confused by the violation message.
80 * @param mixed $value The value to format as string
81 * @param int $format A bitwise combination of the format
82 * constants in this class
84 * @return string The string representation of the passed value
86 protected function formatValue($value, $format = 0)
88 $isDateTime = $value instanceof \DateTimeInterface;
90 if (($format & self::PRETTY_DATE) && $isDateTime) {
91 if (class_exists('IntlDateFormatter')) {
92 $locale = \Locale::getDefault();
93 $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
95 // neither the native nor the stub IntlDateFormatter support
96 // DateTimeImmutable as of yet
97 if (!$value instanceof \DateTime) {
98 $value = new \DateTime(
99 $value->format('Y-m-d H:i:s.u e'),
100 $value->getTimezone()
104 return $formatter->format($value);
107 return $value->format('Y-m-d H:i:s');
110 if (is_object($value)) {
111 if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
112 return $value->__toString();
118 if (is_array($value)) {
122 if (is_string($value)) {
123 return '"'.$value.'"';
126 if (is_resource($value)) {
130 if (null === $value) {
134 if (false === $value) {
138 if (true === $value) {
142 return (string) $value;
146 * Returns a string representation of a list of values.
148 * Each of the values is converted to a string using
149 * {@link formatValue()}. The values are then concatenated with commas.
151 * @param array $values A list of values
152 * @param int $format A bitwise combination of the format
153 * constants in this class
155 * @return string The string representation of the value list
159 protected function formatValues(array $values, $format = 0)
161 foreach ($values as $key => $value) {
162 $values[$key] = $this->formatValue($value, $format);
165 return implode(', ', $values);