0d96ffe894f846d7e41814374161d19e4ec9389e
[yaffs-website] / 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 since version 2.5. Code against ConstraintViolationBuilderInterface instead.
26  */
27 class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
28 {
29     private $violations;
30     private $message;
31     private $parameters;
32     private $root;
33     private $invalidValue;
34     private $propertyPath;
35     private $translator;
36     private $translationDomain;
37     private $plural;
38     private $constraint;
39     private $code;
40
41     /**
42      * @var mixed
43      */
44     private $cause;
45
46     public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
47     {
48         $this->violations = $violations;
49         $this->message = $message;
50         $this->parameters = $parameters;
51         $this->root = $root;
52         $this->propertyPath = $propertyPath;
53         $this->invalidValue = $invalidValue;
54         $this->translator = $translator;
55         $this->translationDomain = $translationDomain;
56         $this->constraint = $constraint;
57     }
58
59     /**
60      * {@inheritdoc}
61      */
62     public function atPath($path)
63     {
64         $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
65
66         return $this;
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     public function setParameter($key, $value)
73     {
74         $this->parameters[$key] = $value;
75
76         return $this;
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     public function setParameters(array $parameters)
83     {
84         $this->parameters = $parameters;
85
86         return $this;
87     }
88
89     /**
90      * {@inheritdoc}
91      */
92     public function setTranslationDomain($translationDomain)
93     {
94         $this->translationDomain = $translationDomain;
95
96         return $this;
97     }
98
99     /**
100      * {@inheritdoc}
101      */
102     public function setInvalidValue($invalidValue)
103     {
104         $this->invalidValue = $invalidValue;
105
106         return $this;
107     }
108
109     /**
110      * {@inheritdoc}
111      */
112     public function setPlural($number)
113     {
114         $this->plural = $number;
115
116         return $this;
117     }
118
119     /**
120      * {@inheritdoc}
121      */
122     public function setCode($code)
123     {
124         $this->code = $code;
125
126         return $this;
127     }
128
129     /**
130      * {@inheritdoc}
131      */
132     public function setCause($cause)
133     {
134         $this->cause = $cause;
135
136         return $this;
137     }
138
139     /**
140      * {@inheritdoc}
141      */
142     public function addViolation()
143     {
144         if (null === $this->plural) {
145             $translatedMessage = $this->translator->trans(
146                 $this->message,
147                 $this->parameters,
148                 $this->translationDomain
149             );
150         } else {
151             try {
152                 $translatedMessage = $this->translator->transChoice(
153                     $this->message,
154                     $this->plural,
155                     $this->parameters,
156                     $this->translationDomain
157                 );
158             } catch (\InvalidArgumentException $e) {
159                 $translatedMessage = $this->translator->trans(
160                     $this->message,
161                     $this->parameters,
162                     $this->translationDomain
163                 );
164             }
165         }
166
167         $this->violations->add(new ConstraintViolation(
168             $translatedMessage,
169             $this->message,
170             $this->parameters,
171             $this->root,
172             $this->propertyPath,
173             $this->invalidValue,
174             $this->plural,
175             $this->code,
176             $this->constraint,
177             $this->cause
178         ));
179     }
180 }