Passed
Pull Request — master (#32)
by Antonio Oertel
04:01
created

PisPasep::format()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
final class PisPasep extends AbstractDocument
6
{
7
    const LENGTH = 11;
8
9
    const LABEL = 'PisPasep';
10
11
    const REGEX = '/^([\d]{3})([\d]{5})([\d]{2})([\d]{1})$/';
12
13
    /**
14
     * PisPasep constructor.
15
     *
16
     * @param $number
17
     */
18 11
    public function __construct($number)
19
    {
20 11
        $number = preg_replace('/\D/', '', $number);
21 11
        parent::__construct($number, self::LENGTH, 1, self::LABEL);
22 4
    }
23
24
    /**
25
     * @return string Returns formatted number, such as: 00.00000.00-0
26
     */
27 2
    public function format()
28
    {
29 2
        return preg_replace(self::REGEX, '$1.$2.$3-$4', "{$this}");
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 6
    public function calculateDigit($baseNumber)
36
    {
37 6
        $calculator = new DigitCalculator($baseNumber);
38 6
        $calculator->withMultipliersInterval(2, 9);
39 6
        $calculator->useComplementaryInsteadOfModule();
40 6
        $calculator->replaceWhen('0', 10, 11);
41 6
        $calculator->withModule(DigitCalculator::MODULE_11);
42 6
        $digit = $calculator->calculate();
43
44 6
        return "{$digit}";
45
    }
46
}
47