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