Completed
Pull Request — master (#13)
by
unknown
05:32
created

Format::has_validator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
3
namespace byrokrat\banking;
4
5
use byrokrat\banking\Exception\LogicException;
6
use byrokrat\banking\Exception\InvalidStructureException;
7
8
/**
9
 * Account number parsing format
10
 */
11
class Format
12
{
13
    /**
14
     * @var string Name of bank parsed account belongs to
15
     */
16
    private $bankName;
17
18
    /**
19
     * @var string Parsing regular expression
20
     */
21
    private $structure;
22
23
    /**
24
     * @var string Name of class to create
25
     */
26
    private $classname;
27
28
    /**
29
     * @var Validator[] Loaded validators
30
     */
31
    private $validators;
32
33
    /**
34
     * Load parser data
35
     *
36
     * @param string      $bankName   Name of bank parsed account belongs to
37
     * @param string      $structure  Parsing regular expression
38
     * @param string      $classname  Name of class to create
39
     * @param Validator[] $validators Validators to apply
40
     */
41 109
    public function __construct($bankName, $structure, $classname, array $validators)
42
    {
43 109
        $this->bankName = $bankName;
44 109
        $this->structure = $structure;
45 109
        $this->classname = $classname;
46 109
        $this->validators = $validators;
47 109
    }
48
49
    /**
50
     * Parse raw account number
51
     *
52
     * @param  string $number
53
     * @return AccountNumber
54
     * @throws LogicException            If regexp does not grep the correct number of values
55
     * @throws InvalidStructureException If structure is invalid
56
     */
57 232
    public function parse($number)
58
    {
59 232
        if (!preg_match($this->structure, $number, $matches)) {
60 153
            throw new InvalidStructureException("Invalid account number structure $number");
61
        }
62
63 178
        if (count($matches) != 5) {
64 1
            throw new LogicException('Parsing regexp must grep 4 values from number');
65
        }
66
67
        /** @var AccountNumber $account */
68 177
        $account = new $this->classname($this->bankName, $number, $matches[1], $matches[2], $matches[3], $matches[4]);
69
70 177
        foreach ($this->validators as $validator) {
71 177
            $validator->validate($account);
72
        }
73
74 100
        return $account;
75
    }
76
77
    public function has_validator($class_name)
78
    {
79
        foreach($this->validators as $validator) {
80
            if(is_a($validator, $class_name)) {
81
                return TRUE;
82
            }
83
        }
84
85
        return FALSE;
86
    }
87
88
    public function get_validator($class_name)
89
    {
90
        foreach($this->validators as $validator) {
91
            if(is_a($validator, $class_name)) {
92
                return $validator;
93
            }
94
        }
95
96
        return FALSE;
97
    }
98
}
99