Passed
Push — master ( e0b9fb...8b102c )
by Pieter
03:24
created

ValidationException   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 34
rs 10
c 1
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrors() 0 3 1
B __construct() 0 11 7
A getI18n() 0 3 1
1
<?php
2
3
namespace W2w\Lib\ApieObjectAccessNormalizer\Exceptions;
4
5
use Throwable;
6
use W2w\Lib\ApieObjectAccessNormalizer\Errors\ErrorBag;
7
use W2w\Lib\ApieObjectAccessNormalizer\Normalizers\ApieObjectAccessNormalizer;
8
9
/**
10
 * Exception thrown if the constructor could not be called or if a setter threw an error.
11
 *
12
 * @see ApieObjectAccessNormalizer::denormalize()
13
 */
14
class ValidationException extends ApieException implements LocalizationableException
15
{
16
    private $errors;
17
18
    /**
19
     * @param string[][]|ErrorBag $errors
20
     * @param Throwable|null $previous
21
     */
22
    public function __construct($errors, Throwable $previous = null)
23
    {
24
        $this->errors = $errors instanceof ErrorBag ? $errors->getErrors() : (array) $errors;
25
        if (!$previous && $errors instanceof ErrorBag && $errors->hasErrors()) {
26
            $tmp = $errors->getExceptions();
27
            $tmp = reset($tmp);
28
            if ($tmp) {
29
                $previous = reset($tmp) ? : null;
30
            }
31
        }
32
        parent::__construct(422, 'A validation error occurred', $previous);
33
    }
34
35
    /**
36
     * Returns the validation errors.
37
     *
38
     * @return string[][]
39
     */
40
    public function getErrors(): array
41
    {
42
        return $this->errors;
43
    }
44
45
    public function getI18n(): LocalizationInfo
46
    {
47
        return new LocalizationInfo('general.validation', ['errors' => $this->getErrors()]);
48
    }
49
}
50