ValidatorJitCache   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
ccs 12
cts 12
cp 1
wmc 5
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
getClearingValidator() 0 1 ?
getAdditionalValidators() 0 1 ?
A isValidClearing() 0 4 1
A validate() 0 10 2
A getValidators() 0 9 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace byrokrat\banking\Format;
6
7
use byrokrat\banking\AccountNumber;
8
use byrokrat\banking\Validator\ValidatorInterface;
9
use byrokrat\banking\Validator\ResultInterface;
10
use byrokrat\banking\Validator\ResultCollection;
11
12
/**
13
 * Helper trait that manages validator creation and cache
14
 */
15
trait ValidatorJitCache
16
{
17
    /**
18
     * @var ValidatorInterface[]
19
     */
20
    private $validators;
21
22 183
    public function isValidClearing(AccountNumber $account): bool
23
    {
24 183
        return $this->getValidators()[0]->validate($account)->isValid();
25
    }
26
27 183
    public function validate(AccountNumber $account): ResultInterface
28
    {
29 183
        $results = [];
30
31 183
        foreach ($this->getValidators() as $validator) {
32 183
            $results[] = $validator->validate($account);
33
        }
34
35 183
        return new ResultCollection(...$results);
36
    }
37
38
    /**
39
     * Get clearing number validator
40
     */
41
    abstract protected function getClearingValidator(): ValidatorInterface;
42
43
    /**
44
     * Get additional validators that definies format
45
     *
46
     * @return ValidatorInterface[]
47
     */
48
    abstract protected function getAdditionalValidators(): array;
49
50
    /**
51
     * @return ValidatorInterface[]
52
     */
53 225
    private function getValidators(): array
54
    {
55 225
        if (!$this->validators) {
56 87
            $this->validators = $this->getAdditionalValidators();
57 87
            array_unshift($this->validators, $this->getClearingValidator());
58
        }
59
60 225
        return $this->validators;
61
    }
62
}
63