comparison vendor/symfony/validator/ConstraintViolation.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 /**
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 public function getParameters()
146 {
147 return $this->parameters;
148 }
149
150 /**
151 * {@inheritdoc}
152 */
153 public function getPlural()
154 {
155 return $this->plural;
156 }
157
158 /**
159 * {@inheritdoc}
160 */
161 public function getMessage()
162 {
163 return $this->message;
164 }
165
166 /**
167 * {@inheritdoc}
168 */
169 public function getRoot()
170 {
171 return $this->root;
172 }
173
174 /**
175 * {@inheritdoc}
176 */
177 public function getPropertyPath()
178 {
179 return $this->propertyPath;
180 }
181
182 /**
183 * {@inheritdoc}
184 */
185 public function getInvalidValue()
186 {
187 return $this->invalidValue;
188 }
189
190 /**
191 * Returns the constraint whose validation caused the violation.
192 *
193 * @return Constraint|null The constraint or null if it is not known
194 */
195 public function getConstraint()
196 {
197 return $this->constraint;
198 }
199
200 /**
201 * Returns the cause of the violation.
202 *
203 * @return mixed
204 */
205 public function getCause()
206 {
207 return $this->cause;
208 }
209
210 /**
211 * {@inheritdoc}
212 */
213 public function getCode()
214 {
215 return $this->code;
216 }
217 }