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