Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/validator/ConstraintValidator.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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; | |
15 | |
16 /** | |
17 * Base class for constraint validators. | |
18 * | |
19 * @author Bernhard Schussek <bschussek@gmail.com> | |
20 */ | |
21 abstract class ConstraintValidator implements ConstraintValidatorInterface | |
22 { | |
23 /** | |
24 * Whether to format {@link \DateTime} objects as RFC-3339 dates | |
25 * ("Y-m-d H:i:s"). | |
26 * | |
27 * @var int | |
28 */ | |
29 const PRETTY_DATE = 1; | |
30 | |
31 /** | |
32 * Whether to cast objects with a "__toString()" method to strings. | |
33 * | |
34 * @var int | |
35 */ | |
36 const OBJECT_TO_STRING = 2; | |
37 | |
38 /** | |
39 * @var ExecutionContextInterface | |
40 */ | |
41 protected $context; | |
42 | |
43 /** | |
44 * {@inheritdoc} | |
45 */ | |
46 public function initialize(ExecutionContextInterface $context) | |
47 { | |
48 $this->context = $context; | |
49 } | |
50 | |
51 /** | |
52 * Returns a string representation of the type of the value. | |
53 * | |
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. | |
58 * | |
59 * @param mixed $value The value to return the type of | |
60 * | |
61 * @return string The type of the value | |
62 */ | |
63 protected function formatTypeOf($value) | |
64 { | |
65 return is_object($value) ? get_class($value) : gettype($value); | |
66 } | |
67 | |
68 /** | |
69 * Returns a string representation of the value. | |
70 * | |
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"). | |
77 * | |
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. | |
83 * | |
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 | |
87 * | |
88 * @return string The string representation of the passed value | |
89 */ | |
90 protected function formatValue($value, $format = 0) | |
91 { | |
92 $isDateTime = $value instanceof \DateTimeInterface; | |
93 | |
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); | |
98 | |
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() | |
105 ); | |
106 } | |
107 | |
108 return $formatter->format($value); | |
109 } | |
110 | |
111 return $value->format('Y-m-d H:i:s'); | |
112 } | |
113 | |
114 if (is_object($value)) { | |
115 if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) { | |
116 return $value->__toString(); | |
117 } | |
118 | |
119 return 'object'; | |
120 } | |
121 | |
122 if (is_array($value)) { | |
123 return 'array'; | |
124 } | |
125 | |
126 if (is_string($value)) { | |
127 return '"'.$value.'"'; | |
128 } | |
129 | |
130 if (is_resource($value)) { | |
131 return 'resource'; | |
132 } | |
133 | |
134 if (null === $value) { | |
135 return 'null'; | |
136 } | |
137 | |
138 if (false === $value) { | |
139 return 'false'; | |
140 } | |
141 | |
142 if (true === $value) { | |
143 return 'true'; | |
144 } | |
145 | |
146 return (string) $value; | |
147 } | |
148 | |
149 /** | |
150 * Returns a string representation of a list of values. | |
151 * | |
152 * Each of the values is converted to a string using | |
153 * {@link formatValue()}. The values are then concatenated with commas. | |
154 * | |
155 * @param array $values A list of values | |
156 * @param int $format A bitwise combination of the format | |
157 * constants in this class | |
158 * | |
159 * @return string The string representation of the value list | |
160 * | |
161 * @see formatValue() | |
162 */ | |
163 protected function formatValues(array $values, $format = 0) | |
164 { | |
165 foreach ($values as $key => $value) { | |
166 $values[$key] = $this->formatValue($value, $format); | |
167 } | |
168 | |
169 return implode(', ', $values); | |
170 } | |
171 } |