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