ValidationException::getValidationResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

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
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Jasny;
4
5
use UnexpectedValueException;
6
7
/**
8
 * Validation exception
9
 */
10
class ValidationException extends \RuntimeException
11
{
12
    protected ValidationResult $validationResult;
13
14
    /**
15
     * ValidationException constructor.
16
     *
17
     * @throws UnexpectedValueException
18
     */
19 6
    public function __construct(ValidationResult $validation)
20
    {
21 6
        if (!$validation->failed()) {
22 1
            throw new UnexpectedValueException("Validation didn't fail, no exception should be thrown");
23
        }
24
25 5
        parent::__construct("Validation failed;\n * " . join("\n * ", $validation->getErrors()));
26
27 5
        $this->validationResult = $validation;
28
    }
29
30
    /**
31
     * Get the validation result with the validation errors.
32
     */
33 1
    public function getValidationResult(): ValidationResult
34
    {
35 1
        return $this->validationResult;
36
    }
37
38
    /**
39
     * Get the (first) validation error
40
     */
41 2
    public function getError(): ?string
42
    {
43 2
        return $this->validationResult->getError();
44
    }
45
46
    /**
47
     * Get the validation errors
48
     *
49
     * @return string[]
50
     */
51 1
    public function getErrors(): array
52
    {
53 1
        return $this->validationResult->getErrors();
54
    }
55
56
57
    /**
58
     * Factory method for failed validation
59
     */
60 1
    public static function error(string $message, mixed ...$args): static
61
    {
62 1
        $error = ValidationResult::error($message, ...$args);
63
64 1
        return new static($error);
65
    }
66
}
67