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 $template; |
| 17 | |
| 18 | public function setValidationError(array $validationErrorList) |
| 19 | { |
| 20 | $this->setMessages($validationErrorList); |
| 21 | return $this; |
| 22 | } |
| 23 | |
| 24 | public function setSchemaName($schemaName) |
| 25 | { |
| 26 | $this->schemaName = $schemaName . '.json'; |
| 27 | $this->template = $schemaName; |
| 28 | return $this; |
| 29 | } |
| 30 | |
| 31 | protected function setMessages($validationErrorList) |
| 32 | { |
| 33 | foreach ($validationErrorList as $error) { |
| 34 | $pointer = is_array($error->data()->path()) |
| 35 | ? "/" . implode("/", $error->data()->path()) |
| 36 | : (string) ($error->data()->path() ?? "(root)"); |
| 37 | |
| 38 | $message = $error->message(); |
| 39 | foreach ($error->args() as $key => $value) { |
| 40 | $message = str_replace("{" . $key . "}", json_encode($value, JSON_UNESCAPED_SLASHES), $message); |
| 41 | } |
| 42 | |
| 43 | $this->data[$pointer]['messages'][$error->keyword()] = $message; |
| 44 | $this->data[$pointer]['headline'] = $pointer; |
| 45 | $this->data[$pointer]['failed'] = 1; |
| 46 | $this->data[$pointer]['data'] = $error->data() !== null ? $error->data()->value() : null; |
| 47 | |
| 48 | $this->message .= ($this->message ? " | " : "") |
| 49 | . '[property ' . $pointer . '] ' |
| 50 | . json_encode($this->data[$pointer]['messages'], JSON_UNESCAPED_SLASHES); |
| 51 | } |
| 52 | return $this; |
| 53 | } |
| 54 | } |