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
29 const PRETTY_DATE = 1;
32 * Whether to cast objects with a "__toString()" method to strings.
36 const OBJECT_TO_STRING = 2;
39 * @var ExecutionContextInterface
46 public function initialize(ExecutionContextInterface $context)
48 $this->context = $context;
52 * Returns a string representation of the type of the value.
54 * This method should be used if you pass the type of a value as
55 * message parameter to a constraint violation. Note that such
56 * parameters should usually not be included in messages aimed at
57 * non-technical people.
59 * @param mixed $value The value to return the type of
61 * @return string The type of the value
63 protected function formatTypeOf($value)
65 return is_object($value) ? get_class($value) : gettype($value);
69 * Returns a string representation of the value.
71 * This method returns the equivalent PHP tokens for most scalar types
72 * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
73 * in double quotes ("). Objects, arrays and resources are formatted as
74 * "object", "array" and "resource". If the $format bitmask contains
75 * the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted
76 * as RFC-3339 dates ("Y-m-d H:i:s").
78 * Be careful when passing message parameters to a constraint violation
79 * that (may) contain objects, arrays or resources. These parameters
80 * should only be displayed for technical users. Non-technical users
81 * won't know what an "object", "array" or "resource" is and will be
82 * confused by the violation message.
84 * @param mixed $value The value to format as string
85 * @param int $format A bitwise combination of the format
86 * constants in this class
88 * @return string The string representation of the passed value
90 protected function formatValue($value, $format = 0)
92 $isDateTime = $value instanceof \DateTimeInterface;
94 if (($format & self::PRETTY_DATE) && $isDateTime) {
95 if (class_exists('IntlDateFormatter')) {
96 $locale = \Locale::getDefault();
97 $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
99 // neither the native nor the stub IntlDateFormatter support
100 // DateTimeImmutable as of yet
101 if (!$value instanceof \DateTime) {
102 $value = new \DateTime(
103 $value->format('Y-m-d H:i:s.u e'),
104 $value->getTimezone()
108 return $formatter->format($value);
111 return $value->format('Y-m-d H:i:s');
114 if (is_object($value)) {
115 if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
116 return $value->__toString();
122 if (is_array($value)) {
126 if (is_string($value)) {
127 return '"'.$value.'"';
130 if (is_resource($value)) {
134 if (null === $value) {
138 if (false === $value) {
142 if (true === $value) {
146 return (string) $value;
150 * Returns a string representation of a list of values.
152 * Each of the values is converted to a string using
153 * {@link formatValue()}. The values are then concatenated with commas.
155 * @param array $values A list of values
156 * @param int $format A bitwise combination of the format
157 * constants in this class
159 * @return string The string representation of the value list
163 protected function formatValues(array $values, $format = 0)
165 foreach ($values as $key => $value) {
166 $values[$key] = $this->formatValue($value, $format);
169 return implode(', ', $values);