SelfValidatableTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 4
b 0
f 0
dl 0
loc 18
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getViolations() 0 9 2
A isValid() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Sprain\SwissQrBill\Validator;
4
5
use Symfony\Component\Validator\ConstraintViolationListInterface;
6
use Symfony\Component\Validator\Validation;
7
use Symfony\Component\Validator\Validator\ValidatorInterface;
8
9
/**
10
 * @internal
11
 */
12
trait SelfValidatableTrait
13
{
14
    private ?ValidatorInterface $validator = null;
15
16
    public function getViolations(): ConstraintViolationListInterface
17
    {
18
        if (null === $this->validator) {
19
            $this->validator = Validation::createValidatorBuilder()
20
                ->addMethodMapping('loadValidatorMetadata')
21
                ->getValidator();
22
        }
23
24
        return $this->validator->validate($this);
25
    }
26
27
    public function isValid(): bool
28
    {
29
        return (0 === $this->getViolations()->count());
30
    }
31
}
32