Version 1
[yaffs-website] / vendor / symfony / validator / Violation / ConstraintViolationBuilder.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\Violation;
13
14 use Symfony\Component\Translation\TranslatorInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintViolation;
17 use Symfony\Component\Validator\ConstraintViolationList;
18 use Symfony\Component\Validator\Util\PropertyPath;
19
20 /**
21  * Default implementation of {@link ConstraintViolationBuilderInterface}.
22  *
23  * @author Bernhard Schussek <bschussek@gmail.com>
24  *
25  * @internal You should not instantiate or use this class. Code against
26  *           {@link ConstraintViolationBuilderInterface} instead.
27  */
28 class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
29 {
30     /**
31      * @var ConstraintViolationList
32      */
33     private $violations;
34
35     /**
36      * @var string
37      */
38     private $message;
39
40     /**
41      * @var array
42      */
43     private $parameters;
44
45     /**
46      * @var mixed
47      */
48     private $root;
49
50     /**
51      * @var mixed
52      */
53     private $invalidValue;
54
55     /**
56      * @var string
57      */
58     private $propertyPath;
59
60     /**
61      * @var TranslatorInterface
62      */
63     private $translator;
64
65     /**
66      * @var string|null
67      */
68     private $translationDomain;
69
70     /**
71      * @var int|null
72      */
73     private $plural;
74
75     /**
76      * @var Constraint
77      */
78     private $constraint;
79
80     /**
81      * @var mixed
82      */
83     private $code;
84
85     /**
86      * @var mixed
87      */
88     private $cause;
89
90     public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
91     {
92         $this->violations = $violations;
93         $this->message = $message;
94         $this->parameters = $parameters;
95         $this->root = $root;
96         $this->propertyPath = $propertyPath;
97         $this->invalidValue = $invalidValue;
98         $this->translator = $translator;
99         $this->translationDomain = $translationDomain;
100         $this->constraint = $constraint;
101     }
102
103     /**
104      * {@inheritdoc}
105      */
106     public function atPath($path)
107     {
108         $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
109
110         return $this;
111     }
112
113     /**
114      * {@inheritdoc}
115      */
116     public function setParameter($key, $value)
117     {
118         $this->parameters[$key] = $value;
119
120         return $this;
121     }
122
123     /**
124      * {@inheritdoc}
125      */
126     public function setParameters(array $parameters)
127     {
128         $this->parameters = $parameters;
129
130         return $this;
131     }
132
133     /**
134      * {@inheritdoc}
135      */
136     public function setTranslationDomain($translationDomain)
137     {
138         $this->translationDomain = $translationDomain;
139
140         return $this;
141     }
142
143     /**
144      * {@inheritdoc}
145      */
146     public function setInvalidValue($invalidValue)
147     {
148         $this->invalidValue = $invalidValue;
149
150         return $this;
151     }
152
153     /**
154      * {@inheritdoc}
155      */
156     public function setPlural($number)
157     {
158         $this->plural = $number;
159
160         return $this;
161     }
162
163     /**
164      * {@inheritdoc}
165      */
166     public function setCode($code)
167     {
168         $this->code = $code;
169
170         return $this;
171     }
172
173     /**
174      * {@inheritdoc}
175      */
176     public function setCause($cause)
177     {
178         $this->cause = $cause;
179
180         return $this;
181     }
182
183     /**
184      * {@inheritdoc}
185      */
186     public function addViolation()
187     {
188         if (null === $this->plural) {
189             $translatedMessage = $this->translator->trans(
190                 $this->message,
191                 $this->parameters,
192                 $this->translationDomain
193             );
194         } else {
195             try {
196                 $translatedMessage = $this->translator->transChoice(
197                     $this->message,
198                     $this->plural,
199                     $this->parameters,
200                     $this->translationDomain
201                 );
202             } catch (\InvalidArgumentException $e) {
203                 $translatedMessage = $this->translator->trans(
204                     $this->message,
205                     $this->parameters,
206                     $this->translationDomain
207                 );
208             }
209         }
210
211         $this->violations->add(new ConstraintViolation(
212             $translatedMessage,
213             $this->message,
214             $this->parameters,
215             $this->root,
216             $this->propertyPath,
217             $this->invalidValue,
218             $this->plural,
219             $this->code,
220             $this->constraint,
221             $this->cause
222         ));
223     }
224 }