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

ValidationError   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 43
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessage() 0 3 1
A getField() 0 3 1
A fromTypeAndClassExtractorException() 0 4 1
A getType() 0 3 1
A __construct() 0 5 1
A fromConstraint() 0 6 1
A __toString() 0 3 1
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