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
|
9 |
|
public function __construct($renavam) |
21
|
|
|
{ |
22
|
9 |
|
$renavam = preg_replace('/\D/', '', $renavam); |
23
|
9 |
|
$renavam = $this->padNumber($renavam); |
24
|
9 |
|
parent::__construct($renavam, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL); |
25
|
4 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Pad left a number to length(11) with 0(ZERO) |
29
|
|
|
* |
30
|
|
|
* @param string $number |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
9 |
|
private function padNumber($number) |
35
|
|
|
{ |
36
|
9 |
|
if (empty($number)) { |
37
|
2 |
|
return ''; |
38
|
|
|
} |
39
|
|
|
|
40
|
7 |
|
return str_pad($number, self::LENGTH, 0, STR_PAD_LEFT); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
1 |
|
public function format() |
47
|
|
|
{ |
48
|
1 |
|
return preg_replace(self::REGEX, '$1.$2-$3', "{$this}"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
5 |
|
public function calculateDigit($baseNumber) |
55
|
|
|
{ |
56
|
5 |
|
$calculator = new DigitCalculator($baseNumber); |
57
|
|
|
//$calculator->withMultipliers([3, 2, 9, 8, 7, 6, 5, 4, 3, 2]); |
|
|
|
|
58
|
5 |
|
$calculator->withMultipliers([2, 3, 4, 5, 6, 7, 8, 9, 2, 3]); |
59
|
5 |
|
$calculator->replaceWhen('0', 10); |
60
|
5 |
|
$calculator->withModule(DigitCalculator::MODULE_11); |
61
|
5 |
|
$calculator->multiplySumBy(10); |
62
|
5 |
|
$digit = $calculator->calculate(); |
63
|
|
|
|
64
|
5 |
|
return "{$digit}"; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.