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

Eleven::extractBaseNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents\StateRegistration\Tocantins;
4
5
use Brazanation\Documents\DigitCalculator;
6
use Brazanation\Documents\StateRegistration\State;
7
8
class Eleven extends State
9
{
10
    const LABEL = 'Tocantins';
11
12
    const REGEX = '/^(\d{2})(\d{2})(\d{3})(\d{3})(\d{1})$/';
13
14
    const FORMAT = '$1.$2.$3.$4-$5';
15
16
    const LENGTH = 11;
17
18
    const DIGITS_COUNT = 1;
19
20 10
    public function __construct()
21
    {
22 10
        parent::__construct(self::LABEL, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
23 10
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 2
    public function extractBaseNumber($number)
29
    {
30 2
        $baseNumber = parent::extractBaseNumber($number);
31
32 2
        return $this->removeFixedDigits($baseNumber);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     *
38
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_TO.html
39
     */
40 2
    public function calculateDigit($baseNumber)
41
    {
42 2
        $calculator = new DigitCalculator($baseNumber);
43 2
        $calculator->useComplementaryInsteadOfModule();
44 2
        $calculator->replaceWhen('0', 10, 11);
45
46 2
        $digit = $calculator->calculate();
47
48 2
        return "{$digit}";
49
    }
50
51
    /**
52
     * Removes digits should not goes to calculate digit.
53
     *
54
     * They digits are in position 3 and 4.
55
     *
56
     * @param string $baseNumber Base number.
57
     *
58
     * @return string Returns base number without fixed digits.
59
     */
60 2
    private function removeFixedDigits($baseNumber)
61
    {
62 2
        return substr($baseNumber, 0, 2) . substr($baseNumber, 4);
63
    }
64
}
65