Renavam::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
final class Renavam extends AbstractDocument
6
{
7
    const LENGTH = 11;
8
9
    const LABEL = 'Renavam';
10
11
    const REGEX = '/^([\d]{4})([\d]{6})([\d]{1})$/';
12
13
    const NUMBER_OF_DIGITS = 1;
14
15
    /**
16
     * Renavam constructor.
17
     *
18
     * @param string $renavam Only accept numbers
19
     */
20 14
    public function __construct(string $renavam)
21
    {
22 14
        $renavam = preg_replace('/\D/', '', $renavam);
23 14
        $renavam = $this->padNumber($renavam);
24 14
        parent::__construct($renavam, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
25
    }
26
27 7
    public static function createFromString(string $number)
28
    {
29 7
        return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
30
    }
31
32
    /**
33
     * Pad left a number to length(11) with 0(ZERO)
34
     *
35
     * @param string $number
36
     *
37
     * @return string
38
     */
39 14
    private function padNumber(string $number) : string
40
    {
41 14
        if (empty($number)) {
42
            return '';
43
        }
44
45 14
        return str_pad($number, self::LENGTH, 0, STR_PAD_LEFT);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function format() : string
52
    {
53 2
        return preg_replace(self::REGEX, '$1.$2-$3', "{$this}");
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 10
    public function calculateDigit(string $baseNumber) : string
60
    {
61 10
        $calculator = new DigitCalculator($baseNumber);
62
        //$calculator->withMultipliers([3, 2, 9, 8, 7, 6, 5, 4, 3, 2]);
63 10
        $calculator->withMultipliers([2, 3, 4, 5, 6, 7, 8, 9, 2, 3]);
64 10
        $calculator->replaceWhen('0', 10);
65 10
        $calculator->withModule(DigitCalculator::MODULE_11);
66 10
        $calculator->multiplySumBy(10);
67 10
        $digit = $calculator->calculate();
68
69 10
        return "{$digit}";
70
    }
71
}
72