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

Rural::calculateDigit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents\StateRegistration\SaoPaulo;
4
5
use Brazanation\Documents\DigitCalculator;
6
use Brazanation\Documents\StateRegistration\SaoPaulo;
7
use Brazanation\Documents\StateRegistration\State;
8
9
class Rural extends State
10
{
11
    const REGEX = '/^(\d{8})(\d{1})(\d{3})$/';
12
13
    const FORMAT = 'P-$1.$2/$3';
14
15
    const LENGTH = 12;
16
17
    const DIGITS_COUNT = 1;
18
19 2
    public function __construct()
20
    {
21 2
        parent::__construct(SaoPaulo::LONG_NAME, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
22 2
    }
23
24 2
    public function extractBaseNumber($number)
25
    {
26 2
        return substr($number, 0, self::LENGTH - 4);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function extractCheckerDigit($number)
33
    {
34 2
        return substr($number, self::LENGTH - 4, 1);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_SP.html
41
     */
42 2
    public function calculateDigit($baseNumber)
43
    {
44 2
        $calculator = new DigitCalculator($baseNumber);
45 2
        $calculator->withMultipliers([10, 8, 7, 6, 5, 4, 3, 1]);
46 2
        $calculator->replaceWhen('0', 10);
47 2
        $calculator->replaceWhen('1', 11);
48 2
        $calculator->withModule(DigitCalculator::MODULE_11);
49 2
        $digit = $calculator->calculate();
50
51 2
        return "{$digit}";
52
    }
53
}
54