Passed
Push — master ( 8b102c...8b3bfd )
by Pieter
01:46
created

ValidationException::getExceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
     * @var Throwable[][] | null
20
     */
21
    private $exceptions;
22
23
    /**
24
     * @param string[][]|ErrorBag $errors
25
     * @param Throwable|null $previous
26
     */
27
    public function __construct($errors, Throwable $previous = null)
28
    {
29
        $this->errors = $errors instanceof ErrorBag ? $errors->getErrors() : (array) $errors;
30
        if (!$previous && $errors instanceof ErrorBag && $errors->hasErrors()) {
31
            $this->exceptions = $errors->getExceptions();
32
            $tmp = reset($this->exceptions);
33
            if ($tmp) {
34
                $previous = reset($tmp) ? : null;
35
            }
36
        }
37
        parent::__construct(422, 'A validation error occurred', $previous);
38
    }
39
40
    /**
41
     * Returns the validation errors.
42
     *
43
     * @return string[][]
44
     */
45
    public function getErrors(): array
46
    {
47
        return $this->errors;
48
    }
49
50
    /**
51
     * @return Throwable[][] | null
52
     */
53
    public function getExceptions(): array
54
    {
55
        return $this->exceptions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->exceptions could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
56
    }
57
58
    public function getI18n(): LocalizationInfo
59
    {
60
        return new LocalizationInfo('general.validation', ['errors' => $this->getErrors()]);
61
    }
62
}
63