Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Base class for constraint validators.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
20 */
|
Chris@0
|
21 abstract class ConstraintValidator implements ConstraintValidatorInterface
|
Chris@0
|
22 {
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Whether to format {@link \DateTime} objects as RFC-3339 dates
|
Chris@0
|
25 * ("Y-m-d H:i:s").
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var int
|
Chris@0
|
28 */
|
Chris@0
|
29 const PRETTY_DATE = 1;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Whether to cast objects with a "__toString()" method to strings.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var int
|
Chris@0
|
35 */
|
Chris@0
|
36 const OBJECT_TO_STRING = 2;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * @var ExecutionContextInterface
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $context;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * {@inheritdoc}
|
Chris@0
|
45 */
|
Chris@0
|
46 public function initialize(ExecutionContextInterface $context)
|
Chris@0
|
47 {
|
Chris@0
|
48 $this->context = $context;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Returns a string representation of the type of the value.
|
Chris@0
|
53 *
|
Chris@0
|
54 * This method should be used if you pass the type of a value as
|
Chris@0
|
55 * message parameter to a constraint violation. Note that such
|
Chris@0
|
56 * parameters should usually not be included in messages aimed at
|
Chris@0
|
57 * non-technical people.
|
Chris@0
|
58 *
|
Chris@0
|
59 * @param mixed $value The value to return the type of
|
Chris@0
|
60 *
|
Chris@0
|
61 * @return string The type of the value
|
Chris@0
|
62 */
|
Chris@0
|
63 protected function formatTypeOf($value)
|
Chris@0
|
64 {
|
Chris@0
|
65 return is_object($value) ? get_class($value) : gettype($value);
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Returns a string representation of the value.
|
Chris@0
|
70 *
|
Chris@0
|
71 * This method returns the equivalent PHP tokens for most scalar types
|
Chris@0
|
72 * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
|
Chris@0
|
73 * in double quotes ("). Objects, arrays and resources are formatted as
|
Chris@0
|
74 * "object", "array" and "resource". If the $format bitmask contains
|
Chris@0
|
75 * the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted
|
Chris@0
|
76 * as RFC-3339 dates ("Y-m-d H:i:s").
|
Chris@0
|
77 *
|
Chris@0
|
78 * Be careful when passing message parameters to a constraint violation
|
Chris@0
|
79 * that (may) contain objects, arrays or resources. These parameters
|
Chris@0
|
80 * should only be displayed for technical users. Non-technical users
|
Chris@0
|
81 * won't know what an "object", "array" or "resource" is and will be
|
Chris@0
|
82 * confused by the violation message.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @param mixed $value The value to format as string
|
Chris@0
|
85 * @param int $format A bitwise combination of the format
|
Chris@0
|
86 * constants in this class
|
Chris@0
|
87 *
|
Chris@0
|
88 * @return string The string representation of the passed value
|
Chris@0
|
89 */
|
Chris@0
|
90 protected function formatValue($value, $format = 0)
|
Chris@0
|
91 {
|
Chris@0
|
92 $isDateTime = $value instanceof \DateTimeInterface;
|
Chris@0
|
93
|
Chris@0
|
94 if (($format & self::PRETTY_DATE) && $isDateTime) {
|
Chris@0
|
95 if (class_exists('IntlDateFormatter')) {
|
Chris@0
|
96 $locale = \Locale::getDefault();
|
Chris@0
|
97 $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
|
Chris@0
|
98
|
Chris@0
|
99 // neither the native nor the stub IntlDateFormatter support
|
Chris@0
|
100 // DateTimeImmutable as of yet
|
Chris@0
|
101 if (!$value instanceof \DateTime) {
|
Chris@0
|
102 $value = new \DateTime(
|
Chris@0
|
103 $value->format('Y-m-d H:i:s.u e'),
|
Chris@0
|
104 $value->getTimezone()
|
Chris@0
|
105 );
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 return $formatter->format($value);
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 return $value->format('Y-m-d H:i:s');
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 if (is_object($value)) {
|
Chris@0
|
115 if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
|
Chris@0
|
116 return $value->__toString();
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 return 'object';
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 if (is_array($value)) {
|
Chris@0
|
123 return 'array';
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 if (is_string($value)) {
|
Chris@0
|
127 return '"'.$value.'"';
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 if (is_resource($value)) {
|
Chris@0
|
131 return 'resource';
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 if (null === $value) {
|
Chris@0
|
135 return 'null';
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 if (false === $value) {
|
Chris@0
|
139 return 'false';
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 if (true === $value) {
|
Chris@0
|
143 return 'true';
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 return (string) $value;
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 /**
|
Chris@0
|
150 * Returns a string representation of a list of values.
|
Chris@0
|
151 *
|
Chris@0
|
152 * Each of the values is converted to a string using
|
Chris@0
|
153 * {@link formatValue()}. The values are then concatenated with commas.
|
Chris@0
|
154 *
|
Chris@0
|
155 * @param array $values A list of values
|
Chris@0
|
156 * @param int $format A bitwise combination of the format
|
Chris@0
|
157 * constants in this class
|
Chris@0
|
158 *
|
Chris@0
|
159 * @return string The string representation of the value list
|
Chris@0
|
160 *
|
Chris@0
|
161 * @see formatValue()
|
Chris@0
|
162 */
|
Chris@0
|
163 protected function formatValues(array $values, $format = 0)
|
Chris@0
|
164 {
|
Chris@0
|
165 foreach ($values as $key => $value) {
|
Chris@0
|
166 $values[$key] = $this->formatValue($value, $format);
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 return implode(', ', $values);
|
Chris@0
|
170 }
|
Chris@0
|
171 }
|