804a9b2a193ec444b63494a52f39ebf7e6506104
[yaffs-website] / vendor / symfony / validator / ConstraintValidator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator;
13
14 use Symfony\Component\Validator\Context\ExecutionContextInterface as ExecutionContextInterface2Dot5;
15 use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
16 use Symfony\Component\Validator\Violation\LegacyConstraintViolationBuilder;
17
18 /**
19  * Base class for constraint validators.
20  *
21  * @author Bernhard Schussek <bschussek@gmail.com>
22  */
23 abstract class ConstraintValidator implements ConstraintValidatorInterface
24 {
25     /**
26      * Whether to format {@link \DateTime} objects as RFC-3339 dates
27      * ("Y-m-d H:i:s").
28      *
29      * @var int
30      */
31     const PRETTY_DATE = 1;
32
33     /**
34      * Whether to cast objects with a "__toString()" method to strings.
35      *
36      * @var int
37      */
38     const OBJECT_TO_STRING = 2;
39
40     /**
41      * @var ExecutionContextInterface2Dot5
42      */
43     protected $context;
44
45     /**
46      * {@inheritdoc}
47      */
48     public function initialize(ExecutionContextInterface $context)
49     {
50         $this->context = $context;
51     }
52
53     /**
54      * Wrapper for {@link ExecutionContextInterface::buildViolation} that
55      * supports the 2.4 context API.
56      *
57      * @param string $message    The violation message
58      * @param array  $parameters The message parameters
59      *
60      * @return ConstraintViolationBuilderInterface The violation builder
61      *
62      * @deprecated since version 2.5, to be removed in 3.0.
63      */
64     protected function buildViolation($message, array $parameters = array())
65     {
66         @trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
67
68         if ($this->context instanceof ExecutionContextInterface2Dot5) {
69             return $this->context->buildViolation($message, $parameters);
70         }
71
72         return new LegacyConstraintViolationBuilder($this->context, $message, $parameters);
73     }
74
75     /**
76      * Wrapper for {@link ExecutionContextInterface::buildViolation} that
77      * supports the 2.4 context API.
78      *
79      * @param ExecutionContextInterface $context    The context to use
80      * @param string                    $message    The violation message
81      * @param array                     $parameters The message parameters
82      *
83      * @return ConstraintViolationBuilderInterface The violation builder
84      *
85      * @deprecated since version 2.5, to be removed in 3.0.
86      */
87     protected function buildViolationInContext(ExecutionContextInterface $context, $message, array $parameters = array())
88     {
89         @trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
90
91         if ($context instanceof ExecutionContextInterface2Dot5) {
92             return $context->buildViolation($message, $parameters);
93         }
94
95         return new LegacyConstraintViolationBuilder($context, $message, $parameters);
96     }
97
98     /**
99      * Returns a string representation of the type of the value.
100      *
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.
105      *
106      * @param mixed $value The value to return the type of
107      *
108      * @return string The type of the value
109      */
110     protected function formatTypeOf($value)
111     {
112         return is_object($value) ? get_class($value) : gettype($value);
113     }
114
115     /**
116      * Returns a string representation of the value.
117      *
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").
124      *
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.
130      *
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
134      *
135      * @return string The string representation of the passed value
136      */
137     protected function formatValue($value, $format = 0)
138     {
139         $isDateTime = $value instanceof \DateTime || $value instanceof \DateTimeInterface;
140
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);
145
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()
152                     );
153                 }
154
155                 return $formatter->format($value);
156             }
157
158             return $value->format('Y-m-d H:i:s');
159         }
160
161         if (is_object($value)) {
162             if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
163                 return $value->__toString();
164             }
165
166             return 'object';
167         }
168
169         if (is_array($value)) {
170             return 'array';
171         }
172
173         if (is_string($value)) {
174             return '"'.$value.'"';
175         }
176
177         if (is_resource($value)) {
178             return 'resource';
179         }
180
181         if (null === $value) {
182             return 'null';
183         }
184
185         if (false === $value) {
186             return 'false';
187         }
188
189         if (true === $value) {
190             return 'true';
191         }
192
193         return (string) $value;
194     }
195
196     /**
197      * Returns a string representation of a list of values.
198      *
199      * Each of the values is converted to a string using
200      * {@link formatValue()}. The values are then concatenated with commas.
201      *
202      * @param array $values A list of values
203      * @param int   $format A bitwise combination of the format
204      *                      constants in this class
205      *
206      * @return string The string representation of the value list
207      *
208      * @see formatValue()
209      */
210     protected function formatValues(array $values, $format = 0)
211     {
212         foreach ($values as $key => $value) {
213             $values[$key] = $this->formatValue($value, $format);
214         }
215
216         return implode(', ', $values);
217     }
218 }