Test Failed
Pull Request — main (#22)
by Fractal
13:00
created

ValidationError::getField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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