Passed
Push — master ( 000227...5ee16c )
by Antonio Oertel
32s
created

Cpf::calculateDigit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
final class Cpf extends AbstractDocument implements DocumentInterface
6
{
7
    const LENGTH = 11;
8
9
    const LABEL = 'CPF';
10
11
    const REGEX = '/^([\d]{3})([\d]{3})([\d]{3})([\d]{2})$/';
12
13
    /**
14
     * Cpf constructor.
15
     *
16
     * @param string $number Only accept numbers
17
     */
18 11
    public function __construct($number)
19
    {
20 11
        $number = preg_replace('/\D/', '', $number);
21 11
        parent::__construct($number, self::LENGTH, 2, self::LABEL);
22 4
    }
23
24
    /**
25
     * @return string Returns formatted number, such as: 00.000.000/0000-00
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, 11);
39 6
        $calculator->useComplementaryInsteadOfModule();
40 6
        $calculator->replaceWhen('0', 10, 11);
41 6
        $calculator->withModule(DigitCalculator::MODULE_11);
42 6
        $firstDigit = $calculator->calculate();
43 6
        $calculator->addDigit($firstDigit);
44 6
        $secondDigit = $calculator->calculate();
45
46 6
        return "{$firstDigit}{$secondDigit}";
47
    }
48
}
49