-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConstraintViolationBuilder.php
169 lines (140 loc) · 3.47 KB
/
ConstraintViolationBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
declare(strict_types=1);
namespace Chubbyphp\Validation\Constraint\Symfony;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
final class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
{
/**
* @var ConstraintViolationListInterface<int, ConstraintViolationInterface>
*/
private ConstraintViolationListInterface $violations;
private string $path;
private string $message;
/**
* @var array<mixed>
*/
private array $parameters;
/**
* @var mixed
*/
private $invalidValue;
private ?int $plural = null;
private ?string $code = null;
/**
* @var mixed
*/
private $cause;
/**
* @param ConstraintViolationListInterface<int, ConstraintViolationInterface> $violations
* @param array<mixed> $parameters
*/
public function __construct(
ConstraintViolationListInterface $violations,
string $message,
array $parameters,
string $path
) {
$this->violations = $violations;
$this->message = $message;
$this->parameters = $parameters;
$this->path = $path;
}
/**
* @param string $path
*
* @return ConstraintViolationBuilder
*/
public function atPath($path): self
{
$this->path = $path;
return $this;
}
/**
* @param string $key
* @param string $value
*
* @return ConstraintViolationBuilder
*/
public function setParameter($key, $value): self
{
$this->parameters[$key] = $value;
return $this;
}
/**
* @param array<mixed> $parameters
*
* @return ConstraintViolationBuilder
*/
public function setParameters(array $parameters): self
{
$this->parameters = $parameters;
return $this;
}
/**
* @param string $translationDomain
*
* @return ConstraintViolationBuilder
*/
public function setTranslationDomain($translationDomain): self
{
return $this;
}
/**
* @param mixed $invalidValue
*
* @return ConstraintViolationBuilder
*/
public function setInvalidValue($invalidValue): self
{
$this->invalidValue = $invalidValue;
return $this;
}
/**
* @param int $number
*
* @return ConstraintViolationBuilder
*/
public function setPlural($number): self
{
$this->plural = $number;
return $this;
}
/**
* @param null|string $code
*
* @return ConstraintViolationBuilder
*/
public function setCode($code): self
{
$this->code = $code;
return $this;
}
/**
* @param mixed $cause
*
* @return ConstraintViolationBuilder
*/
public function setCause($cause): self
{
$this->cause = $cause;
return $this;
}
public function addViolation(): void
{
$this->violations->add(new ConstraintViolation(
$this->message,
$this->message,
$this->parameters,
null,
$this->path,
$this->invalidValue,
$this->plural,
$this->code,
null,
$this->cause
));
}
}