Passed
Branch scrutinizer (997b85)
by Antonio Oertel
02:25
created

Rural::extractBaseNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 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 = '/^P(\d{8})(\d{1})(\d{3})$/';
12
13
    const FORMAT = 'P-$1.$2/$3';
14
15
    const LENGTH = 12;
16
17
    const NUMBER_OF_DIGITS = 1;
18
19 4
    public function __construct()
20
    {
21 4
        parent::__construct(SaoPaulo::LONG_NAME, self::LENGTH, self::NUMBER_OF_DIGITS, self::REGEX, self::FORMAT);
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 4
    public function normalizeNumber(string $number) : string
28
    {
29 4
        return 'P' . preg_replace('/[\D]/i', '', $number);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 4
    public function extractBaseNumber(string $number) : string
36
    {
37 4
        return substr($number, 1, self::LENGTH - 4);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 4
    public function extractCheckerDigit(string $number) : string
44
    {
45 4
        return substr($number, -4, 1);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_SP.html
52
     */
53 4
    public function calculateDigit(string $baseNumber) : string
54
    {
55 4
        $calculator = new DigitCalculator($baseNumber);
56 4
        $calculator->withMultipliers([10, 8, 7, 6, 5, 4, 3, 1]);
57 4
        $calculator->replaceWhen('0', 10);
58 4
        $calculator->replaceWhen('1', 11);
59 4
        $calculator->withModule(DigitCalculator::MODULE_11);
60 4
        $digit = $calculator->calculate();
61
62 4
        return "{$digit}";
63
    }
64
}
65