Passed
Push — main ( 141d13...7af829 )
by Breno
02:14
created

ValidationException::setFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Exception;
5
6
use BrenoRoosevelt\Validation\Error;
7
use Exception;
8
use Throwable;
9
10
class ValidationException extends Exception implements ValidationExceptionInterface
11
{
12
    private static ?ValidationExceptionFactoryInterface $userDefinedFactory = null;
13
14
    /** @var Error[] */
15
    private array $errors;
16
17
    public function __construct(array $errors, ?string $message = "", int $code = 422, ?Throwable $previous = null)
18
    {
19
        $this->errors = array_filter($errors, fn($e) => $e instanceof Error);
20
        parent::__construct($message ?? '', $code, $previous);
21
    }
22
23
    /** @inheritDoc */
24
    public function getErrors(): array
25
    {
26
        return $this->errors;
27
    }
28
29
    public static function setFactory(?ValidationExceptionFactoryInterface $factory): void
30
    {
31
        self::$userDefinedFactory = $factory;
32
    }
33
34
    public static function hasFactory(): bool
35
    {
36
        return self::$userDefinedFactory instanceof ValidationExceptionFactoryInterface;
37
    }
38
39
    public static function getFactory(): ?ValidationExceptionFactoryInterface
40
    {
41
        return self::$userDefinedFactory;
42
    }
43
}
44