ValidatorJitCache::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 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