Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.00% |
19 / 20 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
SchemaValidation | |
95.00% |
19 / 20 |
|
66.67% |
2 / 3 |
8 | |
0.00% |
0 / 1 |
setValidationError | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setSchemaName | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setMessages | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
6.01 |
1 | <?php |
2 | |
3 | namespace BO\Zmsentities\Exception; |
4 | |
5 | use BO\Zmsentities\Schema\Validator; |
6 | use Opis\JsonSchema\ValidationError; |
7 | |
8 | class SchemaValidation extends \Exception |
9 | { |
10 | protected $code = 400; |
11 | |
12 | public $data = []; |
13 | |
14 | protected $schemaName = ''; |
15 | |
16 | public function setValidationError(array $validationErrorList) |
17 | { |
18 | $this->setMessages($validationErrorList); |
19 | return $this; |
20 | } |
21 | |
22 | public function setSchemaName($schemaName) |
23 | { |
24 | $this->schemaName = $schemaName . '.json'; |
25 | $this->template = $schemaName; |
26 | return $this; |
27 | } |
28 | |
29 | protected function setMessages($validationErrorList) |
30 | { |
31 | foreach ($validationErrorList as $error) { |
32 | $pointer = is_array($error->data()->path()) |
33 | ? "/" . implode("/", $error->data()->path()) |
34 | : (string) ($error->data()->path() ?? "(root)"); |
35 | |
36 | $message = $error->message(); |
37 | foreach ($error->args() as $key => $value) { |
38 | $message = str_replace("{" . $key . "}", json_encode($value, JSON_UNESCAPED_SLASHES), $message); |
39 | } |
40 | |
41 | $this->data[$pointer]['messages'][$error->keyword()] = $message; |
42 | $this->data[$pointer]['headline'] = $pointer; |
43 | $this->data[$pointer]['failed'] = 1; |
44 | $this->data[$pointer]['data'] = $error->data() !== null ? $error->data()->value() : null; |
45 | |
46 | $this->message .= ($this->message ? " | " : "") |
47 | . '[property ' . $pointer . '] ' |
48 | . json_encode($this->data[$pointer]['messages'], JSON_UNESCAPED_SLASHES); |
49 | } |
50 | return $this; |
51 | } |
52 | } |