Passed
Push — master ( b678cc...5a9a7b )
by Pieter
03:14
created

ValidationException::__construct()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 7
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 11
rs 8.8333
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
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