Completed
Push — master ( 8e51ac...186982 )
by Rafael
03:50
created

ConstraintViolation::setCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
    public function getMessage(): string
87
    {
88
        return $this->message;
89
    }
90
91
    /**
92
     * @param string $message
93
     *
94
     * @return ConstraintViolation
95
     */
96
    public function setMessage(string $message): ConstraintViolation
97
    {
98
        $this->message = $message;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getPropertyPath(): ?string
107
    {
108
        return $this->propertyPath;
109
    }
110
111
    /**
112
     * @param string $path
113
     *
114
     * @return ConstraintViolation
115
     */
116
    public function setPropertyPath(?string $path): ConstraintViolation
117
    {
118
        $this->propertyPath = $path;
119
120
        return $this;
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126
    public function getInvalidValue()
127
    {
128
        return $this->invalidValue;
129
    }
130
131
    /**
132
     * @param mixed $invalidValue
133
     *
134
     * @return ConstraintViolation
135
     */
136
    public function setInvalidValue($invalidValue): ConstraintViolation
137
    {
138
        $this->invalidValue = $invalidValue;
139
140
        return $this;
141
    }
142
143
    /**
144
     * @return ConstraintViolationParameter[]
145
     */
146
    public function getParameters(): array
147
    {
148
        return $this->parameters;
149
    }
150
151
    /**
152
     * @param string $name
153
     * @param mixed  $value
154
     *
155
     * @return ConstraintViolation
156
     */
157
    public function addParameter($name, $value): ConstraintViolation
158
    {
159
        $this->parameters[] = new ConstraintViolationParameter($name, $value);
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getMessageTemplate(): string
168
    {
169
        return $this->messageTemplate;
170
    }
171
172
    /**
173
     * @param string $messageTemplate
174
     *
175
     * @return ConstraintViolation
176
     */
177
    public function setMessageTemplate(string $messageTemplate): ConstraintViolation
178
    {
179
        $this->messageTemplate = $messageTemplate;
180
181
        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
    public function setPlural(?int $plural): ConstraintViolation
198
    {
199
        $this->plural = $plural;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getCode(): string
208
    {
209
        return $this->code;
210
    }
211
212
    /**
213
     * @param string $code
214
     *
215
     * @return ConstraintViolation
216
     */
217
    public function setCode(string $code): ConstraintViolation
218
    {
219
        $this->code = $code;
220
221
        return $this;
222
    }
223
224
    /**
225
     * @return array
226
     */
227
    public function toArray()
228
    {
229
        $params = [];
230
        foreach ($this->getParameters() as $parameter) {
231
            $params[$parameter->getName()] = $parameter->getValue();
232
        }
233
234
        return [
235
            'code' => $this->getCode(),
236
            'message' => $this->getMessage(),
237
            'messageTemplate' => $this->getMessageTemplate(),
238
            'propertyPath' => $this->getPropertyPath(),
239
            'parameters' => $params,
240
            'invalidValue' => $this->getInvalidValue(),
241
        ];
242
    }
243
}
244