|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FRZB\Component\RequestMapper\ValueObject; |
|
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
|
|
|
public function __construct( |
|
16
|
|
|
private readonly string $type, |
|
17
|
|
|
private readonly string $field, |
|
18
|
|
|
private readonly string $message, |
|
19
|
|
|
) { |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function __toString() |
|
23
|
|
|
{ |
|
24
|
|
|
return sprintf('type: "%s", field: "%s", message: "%s"', $this->type, $this->field, $this->message); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
#[Pure] |
|
28
|
|
|
public static function fromTypeAndClassExtractorException(string $type, ClassExtractorException $e): self |
|
29
|
|
|
{ |
|
30
|
|
|
return new self($type, $e->getProperty(), $e->getMessage()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function fromConstraint(ConstraintViolation $violation): self |
|
34
|
|
|
{ |
|
35
|
|
|
return new self( |
|
36
|
|
|
$violation->getConstraint()::class, |
|
37
|
|
|
$violation->getPropertyPath(), |
|
38
|
|
|
(string) $violation->getMessage() |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getType(): string |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->type; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getField(): string |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->field; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getMessage(): ?string |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->message; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|