1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brazanation\Documents; |
4
|
|
|
|
5
|
|
|
final class Cnh extends AbstractDocument implements DocumentInterface |
6
|
|
|
{ |
7
|
|
|
const LENGTH = 11; |
8
|
|
|
|
9
|
|
|
const LABEL = 'CNH'; |
10
|
|
|
|
11
|
|
|
const REGEX = '/^([\d]{3})([\d]{3})([\d]{4})([\d]{1})$/'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Cnh constructor. |
15
|
|
|
* |
16
|
|
|
* @param string $cnh Only accept numbers |
17
|
|
|
*/ |
18
|
9 |
|
public function __construct($cnh) |
19
|
|
|
{ |
20
|
9 |
|
$cnh = preg_replace('/\D/', '', $cnh); |
21
|
9 |
|
parent::__construct($cnh, self::LENGTH, 2, self::LABEL); |
22
|
3 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
4 |
|
public function calculateDigit($baseNumber) |
28
|
|
|
{ |
29
|
4 |
|
$firstDigit = $this->calculateFirstDigit($baseNumber); |
30
|
4 |
|
$secondDigit = $this->calculateSecondDigit($baseNumber); |
31
|
|
|
|
32
|
4 |
|
return "{$firstDigit}{$secondDigit}"; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
1 |
|
public function format() |
39
|
|
|
{ |
40
|
1 |
|
return "{$this}"; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Calculate check digit from base number. |
45
|
|
|
* |
46
|
|
|
* @param string $baseNumber Base numeric section to be calculate your digit. |
47
|
|
|
* |
48
|
|
|
* @return string Returns a calculated checker digit. |
49
|
|
|
*/ |
50
|
4 |
|
private function calculateFirstDigit($baseNumber) |
51
|
|
|
{ |
52
|
4 |
|
$calculator = new DigitCalculator($baseNumber); |
53
|
4 |
|
$calculator->withMultipliersInterval(1, 9); |
54
|
4 |
|
$calculator->replaceWhen('0', 10, 11); |
55
|
4 |
|
$calculator->withModule(DigitCalculator::MODULE_11); |
56
|
4 |
|
$firstDigit = $calculator->calculate(); |
57
|
|
|
|
58
|
4 |
|
return "{$firstDigit}"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Calculate check digit from base number. |
63
|
|
|
* |
64
|
|
|
* @param string $baseNumber Base numeric section to be calculate your digit. |
65
|
|
|
* |
66
|
|
|
* @return string Returns a calculated checker digit. |
67
|
|
|
*/ |
68
|
4 |
|
private function calculateSecondDigit($baseNumber) |
69
|
|
|
{ |
70
|
4 |
|
$calculator = new DigitCalculator($baseNumber); |
71
|
4 |
|
$calculator->withMultipliers([9, 8, 7, 6, 5, 4, 3, 2, 1]); |
72
|
4 |
|
$calculator->replaceWhen('0', 10, 11); |
73
|
4 |
|
$calculator->withModule(DigitCalculator::MODULE_11); |
74
|
4 |
|
$secondDigit = $calculator->calculate(); |
75
|
|
|
|
76
|
4 |
|
return "{$secondDigit}"; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|