Passed
Push — master ( 9da6fd...0354d3 )
by Rafael
05:30
created

ConstraintViolation::setInvalidValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Model;
12
13
use Ynlo\GraphQLBundle\Annotation as GraphQL;
14
15
/**
16
 * @GraphQL\ObjectType(description="A violation of a constraint that happened during validation.
17
18
For each constraint that fails during validation one or more violations are
19
created. The violations store the violation message, the path to the failing
20
element in the validation graph and other helpful information,
21
like template and arguments to use with a translation engine")
22
 */
23
class ConstraintViolation
24
{
25
    /**
26
     * @var string
27
     *
28
     * @GraphQL\Field(type="string",
29
     *     description="Returns the property path from the root element to the violation.")
30
     */
31
    protected $propertyPath;
32
33
    /**
34
     * @var string
35
     *
36
     * @GraphQL\Field(type="string!",
37
     *     description="Returns the violation message.")
38
     */
39
    protected $message;
40
41
    /**
42
     * @var string
43
     *
44
     * @GraphQL\Field(type="string!", description="Returns the raw violation message.
45
    The raw violation message contains placeholders for the parameters returned by parameters.
46
    Typically you'll pass the message template and parameters to a translation engine.")
47
     */
48
    protected $messageTemplate;
49
50
    /**
51
     * @var ConstraintViolationParameter[]
52
     *
53
     * @GraphQL\Field(type="[Ynlo\GraphQLBundle\Model\ConstraintViolationParameter]",
54
     *     description="Returns the parameters to be inserted into the raw violation message.")
55
     */
56
    protected $parameters;
57
58
    /**
59
     * @var int
60
     *
61
     * @GraphQL\Field(type="int",
62
     *     description="Returns a number for pluralizing the violation message")
63
     */
64
    protected $plural = 0;
65
66
    /**
67
     * @var string
68
     *
69
     * @GraphQL\Field(type="string",
70
     *     description="Returns the value that caused the violation.")
71
     */
72
    protected $invalidValue;
73
74
    /**
75
     * @var string
76
     *
77
     * @GraphQL\Field(type="string!",
78
     *     description="Returns a machine-digestible error code for the violation.")
79
     */
80
    protected $code;
81
82
83
    /**
84
     * @return string
85
     */
86 1
    public function getMessage(): string
87
    {
88 1
        return $this->message;
89
    }
90
91
    /**
92
     * @param string $message
93
     *
94
     * @return ConstraintViolation
95
     */
96 1
    public function setMessage(string $message): ConstraintViolation
97
    {
98 1
        $this->message = $message;
99
100 1
        return $this;
101
    }
102
103
    /**
104
     * @return string
105
     */
106 1
    public function getPropertyPath(): ?string
107
    {
108 1
        return $this->propertyPath;
109
    }
110
111
    /**
112
     * @param string $path
113
     *
114
     * @return ConstraintViolation
115
     */
116 1
    public function setPropertyPath(?string $path): ConstraintViolation
117
    {
118 1
        $this->propertyPath = $path;
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126 1
    public function getInvalidValue()
127
    {
128 1
        return $this->invalidValue;
129
    }
130
131
    /**
132
     * @param mixed $invalidValue
133
     *
134
     * @return ConstraintViolation
135
     */
136 1
    public function setInvalidValue($invalidValue): ConstraintViolation
137
    {
138 1
        $this->invalidValue = $invalidValue;
139
140 1
        return $this;
141
    }
142
143
    /**
144
     * @return ConstraintViolationParameter[]
145
     */
146 1
    public function getParameters(): array
147
    {
148 1
        return $this->parameters;
149
    }
150
151
    /**
152
     * @param string $name
153
     * @param mixed  $value
154
     *
155
     * @return ConstraintViolation
156
     */
157 1
    public function addParameter($name, $value): ConstraintViolation
158
    {
159 1
        $this->parameters[] = new ConstraintViolationParameter($name, $value);
160
161 1
        return $this;
162
    }
163
164
    /**
165
     * @return string
166
     */
167 1
    public function getMessageTemplate(): string
168
    {
169 1
        return $this->messageTemplate;
170
    }
171
172
    /**
173
     * @param string $messageTemplate
174
     *
175
     * @return ConstraintViolation
176
     */
177 1
    public function setMessageTemplate(string $messageTemplate): ConstraintViolation
178
    {
179 1
        $this->messageTemplate = $messageTemplate;
180
181 1
        return $this;
182
    }
183
184
    /**
185
     * @return int
186
     */
187
    public function getPlural(): ?int
188
    {
189
        return $this->plural;
190
    }
191
192
    /**
193
     * @param int $plural
194
     *
195
     * @return ConstraintViolation
196
     */
197 1
    public function setPlural(?int $plural): ConstraintViolation
198
    {
199 1
        $this->plural = $plural;
200
201 1
        return $this;
202
    }
203
204
    /**
205
     * @return string
206
     */
207 1
    public function getCode(): string
208
    {
209 1
        return $this->code;
210
    }
211
212
    /**
213
     * @param string $code
214
     *
215
     * @return ConstraintViolation
216
     */
217 1
    public function setCode(string $code): ConstraintViolation
218
    {
219 1
        $this->code = $code;
220
221 1
        return $this;
222
    }
223
224
    /**
225
     * @return array
226
     */
227 1
    public function toArray()
228
    {
229 1
        $params = [];
230 1
        foreach ($this->getParameters() as $parameter) {
231 1
            $params[$parameter->getName()] = $parameter->getValue();
232
        }
233
234
        return [
235 1
            'code' => $this->getCode(),
236 1
            'message' => $this->getMessage(),
237 1
            'messageTemplate' => $this->getMessageTemplate(),
238 1
            'propertyPath' => $this->getPropertyPath(),
239 1
            'parameters' => $params,
240 1
            'invalidValue' => $this->getInvalidValue(),
241
        ];
242
    }
243
}
244