Lines
95.23%
20 / 21
Methods
66.66%
2 / 3
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| setValidationError | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| setSchemaName | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| setMessages | 93.75% | 15 / 16 | 0.00% | 0 / 1 | 7.01 | ||
| 8 | class SchemaValidation extends \Exception | |
| 9 | { | |
| 10 | protected $code = 400; | |
| 11 | ||
| 12 | public array $data = []; | |
| 13 | ||
| 14 | protected string $schemaName = ''; | |
| 15 | ||
| 16 | public string $template = ''; | |
| 17 | ||
| 18 | public function setValidationError(array $validationErrorList): static | |
| 19 | { | |
| 20 | $this->setMessages($validationErrorList); | |
| 21 | return $this; | |
| 22 | } | |
| 23 | ||
| 24 | public function setSchemaName(string $schemaName): static | |
| 25 | { | |
| 26 | $this->schemaName = $schemaName . '.json'; | |
| 27 | $this->template = $schemaName; | |
| 28 | return $this; | |
| 29 | } | |
| 30 | ||
| 31 | protected function setMessages(array $validationErrorList): static | |
| 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 . "}", (string) 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 | $encodedMessages = json_encode($this->data[$pointer]['messages'], JSON_UNESCAPED_SLASHES); | |
| 49 | $this->message .= ($this->message ? " | " : "") | |
| 50 | . '[property ' . $pointer . '] ' | |
| 51 | . ($encodedMessages !== false ? $encodedMessages : ''); | |
| 52 | } | |
| 53 | return $this; | |
| 54 | } | |
| 55 | } |