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