Completed
Pull Request — master (#19)
by Antonio Oertel
03:14
created

RioGrandeDoNorte::getRegex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Brazanation\Documents\StateRegistration;
4
5
use Brazanation\Documents\DigitCalculator;
6
7
class RioGrandeDoNorte extends State
8
{
9
    const LABEL = 'RioGrandeDoNorte';
10
11
    const REGEX = '/^(20)(\d)?(\d{3})(\d{3})(\d{1})$/';
12
13
    const REGEX_FOR_NINE_NUMBERS = '/^(20)(\d{3})(\d{3})(\d{1})$/';
14
15
    const FORMAT = '$1.$2.$3.$4-$5';
16
17
    const FORMAT_FOR_NINE_NUMBERS = '$1.$2.$3-$4';
18
19
    const LENGTH = 10;
20
21
    const DIGITS_COUNT = 1;
22
23
    /**
24
     * @var int
25
     */
26
    private $length;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
27
28
    /**
29
     * @var string
30
     */
31
    private $format;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
33
    /**
34
     * @var string
35
     */
36
    private $regex;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
37
38 13
    public function __construct()
39
    {
40 13
        $this->length = self::LENGTH;
41 13
        $this->format = self::FORMAT;
42 13
        $this->regex = self::REGEX;
43 13
        parent::__construct(self::LABEL, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
44 13
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 13
    public function normalizeNumber($number)
50
    {
51 13
        $number = parent::normalizeNumber($number);
52
53 13
        $this->applyForNineDigits($number);
54
55 13
        return $number;
56
    }
57
58 2
    public function getFormat()
59
    {
60 2
        return $this->format;
61
    }
62
63 13
    public function getLength()
64
    {
65 13
        return $this->length;
66
    }
67
68
    /**
69
     * @return string
70
     */
71 2
    public function getRegex()
72
    {
73 2
        return $this->regex;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_RN.html
80
     */
81 10
    public function calculateDigit($baseNumber)
82
    {
83 10
        $calculator = new DigitCalculator($baseNumber);
84 10
        $calculator->useComplementaryInsteadOfModule();
85 10
        $calculator->withMultipliersInterval(2, 10);
86 10
        $calculator->replaceWhen('0', 10, 11);
87 10
        $calculator->withModule(DigitCalculator::MODULE_11);
88
89 10
        $digit = $calculator->calculate();
90
91 10
        return "{$digit}";
92
    }
93
94
    /**
95
     * @param string $number
96
     */
97 13
    private function applyForNineDigits($number)
98
    {
99 13
        if (strlen($number) != self::LENGTH) {
100 5
            $this->format = self::FORMAT_FOR_NINE_NUMBERS;
101 5
            $this->regex = self::REGEX_FOR_NINE_NUMBERS;
102 5
            $this->length = 9;
103
        }
104 13
    }
105
}
106