Passed
Branch release/v3.0.0 (c409f3)
by Anatoly
05:44
created

ValidatorConstraintViolationAdapter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 54
ccs 20
cts 20
cp 1
rs 10
c 2
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A getInvalidValue() 0 3 1
A __construct() 0 3 1
A getCode() 0 3 1
A adaptPropertyPath() 0 3 1
A getPropertyPath() 0 3 1
A getMessagePlaceholders() 0 4 1
A getMessage() 0 3 1
A getMessageTemplate() 0 3 1
A getTranslationDomain() 0 3 1
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\Validation\ConstraintViolation;
15
16
use Sunrise\Http\Router\Dictionary\TranslationDomain;
17
use Sunrise\Http\Router\Validation\ConstraintViolationInterface as RouterConstraintViolationInterface;
18
use Symfony\Component\Validator\ConstraintViolationInterface as ValidatorConstraintViolationInterface;
19
20
use function preg_replace;
21
22
/**
23
 * @since 3.0.0
24
 */
25
final class ValidatorConstraintViolationAdapter implements RouterConstraintViolationInterface
26
{
27 6
    public function __construct(
28
        private readonly ValidatorConstraintViolationInterface $validatorConstraintViolation,
29
    ) {
30 6
    }
31
32 5
    public static function create(ValidatorConstraintViolationInterface $validatorConstraintViolation): self
33
    {
34 5
        return new self($validatorConstraintViolation);
35
    }
36
37 6
    public function getMessage(): string
38
    {
39 6
        return (string) $this->validatorConstraintViolation->getMessage();
40
    }
41
42 1
    public function getMessageTemplate(): string
43
    {
44 1
        return $this->validatorConstraintViolation->getMessageTemplate();
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50 1
    public function getMessagePlaceholders(): array
51
    {
52
        /** @var array<string, mixed> */
53 1
        return $this->validatorConstraintViolation->getParameters();
54
    }
55
56 6
    public function getPropertyPath(): string
57
    {
58 6
        return self::adaptPropertyPath($this->validatorConstraintViolation->getPropertyPath());
59
    }
60
61 6
    public function getCode(): ?string
62
    {
63 6
        return $this->validatorConstraintViolation->getCode();
64
    }
65
66 6
    public function getInvalidValue(): mixed
67
    {
68 6
        return $this->validatorConstraintViolation->getInvalidValue();
69
    }
70
71 6
    private static function adaptPropertyPath(string $propertyPath): string
72
    {
73 6
        return (string) preg_replace(['/\x5b([^\x5b\x5d]+)\x5d/', '/^\x2e/'], ['.$1'], $propertyPath);
74
    }
75
76 1
    public function getTranslationDomain(): string
77
    {
78 1
        return TranslationDomain::VALIDATOR;
79
    }
80
}
81