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 JetBrains\PhpStorm\Immutable; |
9
|
|
|
use JetBrains\PhpStorm\Pure; |
10
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface as ConstraintViolation; |
11
|
|
|
|
12
|
|
|
#[Immutable] |
13
|
|
|
final class ValidationError implements ErrorInterface |
14
|
|
|
{ |
15
|
7 |
|
public function __construct( |
16
|
|
|
private readonly string $type, |
17
|
|
|
private readonly string $field, |
18
|
|
|
private readonly string $message, |
19
|
|
|
) { |
20
|
|
|
} |
21
|
|
|
|
22
|
1 |
|
public function __toString() |
23
|
|
|
{ |
24
|
1 |
|
return sprintf('type: "%s", field: "%s", message: "%s"', $this->type, $this->field, $this->message); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
#[Pure] |
28
|
|
|
public static function fromTypeAndClassExtractorException(string $type, ClassExtractorException $e): self |
29
|
|
|
{ |
30
|
1 |
|
return new self($type, $e->getProperty(), $e->getMessage()); |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public static function fromConstraint(ConstraintViolation $violation): self |
34
|
|
|
{ |
35
|
2 |
|
return new self( |
36
|
2 |
|
$violation->getConstraint()::class, |
37
|
2 |
|
$violation->getPropertyPath(), |
38
|
2 |
|
(string) $violation->getMessage() |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function getType(): string |
43
|
|
|
{ |
44
|
2 |
|
return $this->type; |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
public function getField(): string |
48
|
|
|
{ |
49
|
6 |
|
return $this->field; |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
public function getMessage(): ?string |
53
|
|
|
{ |
54
|
6 |
|
return $this->message; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|