1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FRZB\Component\RequestMapper\Data; |
6
|
|
|
|
7
|
|
|
use FRZB\Component\RequestMapper\Exception\ClassExtractorException; |
8
|
|
|
use FRZB\Component\RequestMapper\Exception\ErrorInvalidArgumentException; |
9
|
|
|
use JetBrains\PhpStorm\Immutable; |
10
|
|
|
use JetBrains\PhpStorm\Pure; |
11
|
|
|
use Symfony\Component\Validator\ConstraintViolation as Constraint; |
12
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface as ConstraintViolation; |
13
|
|
|
|
14
|
|
|
#[Immutable] |
15
|
|
|
final class ValidationError implements ErrorInterface |
16
|
|
|
{ |
17
|
7 |
|
public function __construct( |
18
|
|
|
private string $type, |
19
|
|
|
private string $field, |
20
|
|
|
private string $message |
21
|
|
|
) { |
22
|
7 |
|
} |
23
|
|
|
|
24
|
1 |
|
public function __toString() |
25
|
|
|
{ |
26
|
1 |
|
return sprintf('type: "%s", field: "%s", message: "%s"', $this->type, $this->field, $this->message); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
#[Pure] |
30
|
|
|
public static function fromTypeAndClassExtractorException(string $type, ClassExtractorException $e): self |
31
|
|
|
{ |
32
|
1 |
|
return new self($type, $e->getProperty(), $e->getMessage()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** @noinspection PhpConditionAlreadyCheckedInspection */ |
36
|
2 |
|
public static function fromConstraint(ConstraintViolation $violation): self |
37
|
|
|
{ |
38
|
|
|
return match (true) { |
39
|
2 |
|
$violation instanceof Constraint => new self( |
40
|
2 |
|
$violation->getConstraint()::class, |
41
|
2 |
|
$violation->getPropertyPath(), |
42
|
2 |
|
(string) $violation->getMessage() |
43
|
|
|
), |
44
|
2 |
|
default => throw ErrorInvalidArgumentException::fromConstraintValidation($violation), |
45
|
|
|
}; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
public function getType(): string |
49
|
|
|
{ |
50
|
2 |
|
return $this->type; |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
public function getField(): string |
54
|
|
|
{ |
55
|
6 |
|
return $this->field; |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
public function getMessage(): ?string |
59
|
|
|
{ |
60
|
6 |
|
return $this->message; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|