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