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