Completed
Pull Request — master (#108)
by Matt
13:14
created

ValidationError::getParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard;
4
5
final class ValidationError implements \JsonSerializable
6
{
7
    const KEYWORD     = 'keyword';
8
    const PARAMETER   = 'parameter';
9
    const DATA        = 'data';
10
    const DATA_PATH   = 'data_path';
11
    const SCHEMA      = 'schema';
12
    const SCHEMA_PATH = 'schema_path';
13
    const CAUSE       = 'cause';
14
    const MESSAGE     = 'message';
15
16
    /**
17
     * @var string
18
     */
19
    private $message;
20
21
    /**
22
     * @var string|null
23
     */
24
    private $interpolatedMessage;
25
26
    /**
27
     * @var mixed
28
     */
29
    private $cause;
30
31
    /**
32
     * @var string[]|null
33
     */
34
    private $context;
35
36
    /**
37
     * @var string
38
     */
39 154
    private $keyword;
40
41 154
    /**
42 154
     * @var mixed
43 154
     */
44 154
    private $parameter;
45 154
46 154
    /**
47
     * @var mixed
48
     */
49
    private $data;
50
51
    /**
52
     * @var string
53 20
     */
54
    private $dataPath;
55 20
56
    /**
57
     * @var object
58
     */
59
    private $schema;
60
61
    /**
62
     * @var string
63
     */
64
    private $schemaPath;
65
66 20
    public function __construct(
67
        $message,
68 20
        $keyword,
69 20
        $parameter,
70 14
        $data,
71 20
        $dataPath,
72
        $schema,
73 20
        $schemaPath
74
    ) {
75
        $this->message    = $message;
76
        $this->keyword    = $keyword;
77
        $this->parameter  = $parameter;
78
        $this->data       = $data;
79
        $this->dataPath   = $dataPath;
80
        $this->schema     = $schema;
81 14
        $this->schemaPath = $schemaPath;
82
    }
83 14
84
    /**
85
     * Get the human readable error message for this error.
86
     *
87
     * @return string
88
     */
89
    public function getMessage()
90
    {
91 14
        if ($this->interpolatedMessage === null) {
92
            $this->interpolatedMessage = $this->interpolate($this->message, $this->getContext());
93 14
        }
94
95
        return $this->interpolatedMessage;
96
    }
97
98
    /**
99
     * @return string
100
     */
101 14
    public function getKeyword()
102
    {
103 14
        return $this->keyword;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getParameter()
110
    {
111 16
        return $this->parameter;
112
    }
113 16
114
    /**
115
     * @return mixed
116
     */
117
    public function getData()
118
    {
119 14
        return $this->data;
120
    }
121
122 14
    /**
123 14
     * @return string
124 14
     */
125 14
    public function getDataPath()
126 14
    {
127 14
        return $this->dataPath;
128
    }
129
130
    /**
131
     * @return object
132
     */
133
    public function getSchema()
134
    {
135
        return $this->schema;
136
    }
137 2
138
    /**
139 2
     * @return string
140
     */
141
    public function getSchemaPath()
142
    {
143
        return $this->schemaPath;
144
    }
145
146
    /**
147
     * Get the cause of the error.  The cause is either the the value itself
148
     * or the subset of the value that failed validation.  For example, the
149
     * cause of a failed minimum constraint would be the number itself, while
150
     * the cause of a failed additionalProperties constraint would be the
151
     * additional properties in the value that are not allowed.
152
     *
153
     * @return mixed
154
     */
155
    public function getCause()
156 6
    {
157
        return $this->cause !== null ? $this->cause : $this->data;
158 6
    }
159
160
    /**
161
     * @param $cause
162
     *
163
     * @return \League\JsonGuard\ValidationError
164
     */
165
    public function withCause($cause)
166
    {
167
        $error        = clone $this;
168
        $error->cause = $cause;
169
170
        return $error;
171
    }
172 12
173
    /**
174 12
     * Get the context that applied to the failed assertion.
175 12
     *
176
     * @return string[]
177
     */
178
    public function getContext()
179
    {
180
        if ($this->context === null) {
181
            $this->context = array_map(
182
                'League\JsonGuard\as_string',
183
                [
184
                    self::KEYWORD     => $this->keyword,
185
                    self::PARAMETER   => $this->parameter,
186
                    self::DATA        => $this->data,
187
                    self::DATA_PATH   => $this->dataPath,
188
                    self::SCHEMA      => $this->schema,
189
                    self::SCHEMA_PATH => $this->schemaPath,
190
                    self::CAUSE       => $this->getCause(),
191
                ]
192 2
            );
193
        }
194
195 2
        return $this->context;
196
    }
197
198
    /**
199
     * @return array
200
     */
201
    public function toArray()
202
    {
203
        return [
204
            self::MESSAGE     => $this->getMessage(),
205
            self::KEYWORD     => $this->keyword,
206
            self::PARAMETER   => $this->parameter,
207
            self::DATA        => $this->data,
208
            self::DATA_PATH   => $this->dataPath,
209 2
            self::SCHEMA      => $this->schema,
210
            self::SCHEMA_PATH => $this->schemaPath,
211
            self::CAUSE       => $this->getCause(),
212 2
        ];
213
    }
214
215
    /**
216
     * @inheritdoc
217
     */
218
    public function jsonSerialize()
219
    {
220
        return $this->toArray();
221
    }
222
223
    /**
224
     * Interpolate the context values into the message placeholders.
225
     *
226
     * @param  string $message
227
     * @param  array  $context
228
     *
229
     * @return string
230
     */
231
    private function interpolate($message, array $context = [])
232
    {
233
        $replace = [];
234
        foreach ($context as $key => $val) {
235
            $replace['{' . $key . '}'] = $val;
236
        }
237
238
        return strtr($message, $replace);
239
    }
240
}
241