Version 1
[yaffs-website] / vendor / symfony / validator / ConstraintViolation.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 /**
15  * Default implementation of {@ConstraintViolationInterface}.
16  *
17  * @author Bernhard Schussek <bschussek@gmail.com>
18  */
19 class ConstraintViolation implements ConstraintViolationInterface
20 {
21     /**
22      * @var string
23      */
24     private $message;
25
26     /**
27      * @var string
28      */
29     private $messageTemplate;
30
31     /**
32      * @var array
33      */
34     private $parameters;
35
36     /**
37      * @var int|null
38      */
39     private $plural;
40
41     /**
42      * @var mixed
43      */
44     private $root;
45
46     /**
47      * @var string
48      */
49     private $propertyPath;
50
51     /**
52      * @var mixed
53      */
54     private $invalidValue;
55
56     /**
57      * @var Constraint|null
58      */
59     private $constraint;
60
61     /**
62      * @var mixed
63      */
64     private $code;
65
66     /**
67      * @var mixed
68      */
69     private $cause;
70
71     /**
72      * Creates a new constraint violation.
73      *
74      * @param string          $message         The violation message
75      * @param string          $messageTemplate The raw violation message
76      * @param array           $parameters      The parameters to substitute in the
77      *                                         raw violation message
78      * @param mixed           $root            The value originally passed to the
79      *                                         validator
80      * @param string          $propertyPath    The property path from the root
81      *                                         value to the invalid value
82      * @param mixed           $invalidValue    The invalid value that caused this
83      *                                         violation
84      * @param int|null        $plural          The number for determining the plural
85      *                                         form when translating the message
86      * @param mixed           $code            The error code of the violation
87      * @param Constraint|null $constraint      The constraint whose validation
88      *                                         caused the violation
89      * @param mixed           $cause           The cause of the violation
90      */
91     public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null, $cause = null)
92     {
93         $this->message = $message;
94         $this->messageTemplate = $messageTemplate;
95         $this->parameters = $parameters;
96         $this->plural = $plural;
97         $this->root = $root;
98         $this->propertyPath = $propertyPath;
99         $this->invalidValue = $invalidValue;
100         $this->constraint = $constraint;
101         $this->code = $code;
102         $this->cause = $cause;
103     }
104
105     /**
106      * Converts the violation into a string for debugging purposes.
107      *
108      * @return string The violation as string
109      */
110     public function __toString()
111     {
112         if (is_object($this->root)) {
113             $class = 'Object('.get_class($this->root).')';
114         } elseif (is_array($this->root)) {
115             $class = 'Array';
116         } else {
117             $class = (string) $this->root;
118         }
119
120         $propertyPath = (string) $this->propertyPath;
121         $code = $this->code;
122
123         if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
124             $class .= '.';
125         }
126
127         if (!empty($code)) {
128             $code = ' (code '.$code.')';
129         }
130
131         return $class.$propertyPath.":\n    ".$this->getMessage().$code;
132     }
133
134     /**
135      * {@inheritdoc}
136      */
137     public function getMessageTemplate()
138     {
139         return $this->messageTemplate;
140     }
141
142     /**
143      * {@inheritdoc}
144      *
145      * @deprecated since version 2.7, to be removed in 3.0.
146      *             Use getParameters() instead
147      */
148     public function getMessageParameters()
149     {
150         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7, to be removed in 3.0. Use the ConstraintViolation::getParameters() method instead.', E_USER_DEPRECATED);
151
152         return $this->parameters;
153     }
154
155     /**
156      * Alias of {@link getMessageParameters()}.
157      */
158     public function getParameters()
159     {
160         return $this->parameters;
161     }
162
163     /**
164      * {@inheritdoc}
165      *
166      * @deprecated since version 2.7, to be removed in 3.0.
167      *             Use getPlural() instead
168      */
169     public function getMessagePluralization()
170     {
171         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7, to be removed in 3.0. Use the ConstraintViolation::getPlural() method instead.', E_USER_DEPRECATED);
172
173         return $this->plural;
174     }
175
176     /**
177      * Alias of {@link getMessagePluralization()}.
178      */
179     public function getPlural()
180     {
181         return $this->plural;
182     }
183
184     /**
185      * {@inheritdoc}
186      */
187     public function getMessage()
188     {
189         return $this->message;
190     }
191
192     /**
193      * {@inheritdoc}
194      */
195     public function getRoot()
196     {
197         return $this->root;
198     }
199
200     /**
201      * {@inheritdoc}
202      */
203     public function getPropertyPath()
204     {
205         return $this->propertyPath;
206     }
207
208     /**
209      * {@inheritdoc}
210      */
211     public function getInvalidValue()
212     {
213         return $this->invalidValue;
214     }
215
216     /**
217      * Returns the constraint whose validation caused the violation.
218      *
219      * @return Constraint|null The constraint or null if it is not known
220      */
221     public function getConstraint()
222     {
223         return $this->constraint;
224     }
225
226     /**
227      * Returns the cause of the violation.
228      *
229      * @return mixed
230      */
231     public function getCause()
232     {
233         return $this->cause;
234     }
235
236     /**
237      * {@inheritdoc}
238      */
239     public function getCode()
240     {
241         return $this->code;
242     }
243 }