1 | <?php |
||
2 | |||
3 | namespace Brazanation\Documents; |
||
4 | |||
5 | final class Voter extends AbstractDocument |
||
6 | { |
||
7 | const LENGTH = 12; |
||
8 | |||
9 | const LABEL = 'Voter'; |
||
10 | |||
11 | const NUMBER_OF_DIGITS = 2; |
||
12 | |||
13 | private $section; |
||
14 | |||
15 | private $zone; |
||
16 | |||
17 | /** |
||
18 | * Voter constructor. |
||
19 | * |
||
20 | * @param string $number |
||
21 | * @param string $section [optional] |
||
22 | * @param string $zone [optional] |
||
23 | */ |
||
24 | 18 | public function __construct(string $number, string $section = null, string $zone = null) |
|
25 | { |
||
26 | 18 | $number = preg_replace('/\D/', '', $number); |
|
27 | 18 | parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL); |
|
28 | |||
29 | 14 | $this->section = str_pad($section, 4, '0', STR_PAD_LEFT); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
30 | 14 | $this->zone = str_pad($zone, 3, '0', STR_PAD_LEFT); |
|
31 | } |
||
32 | |||
33 | 9 | public static function createFromString(string $number, string $section = null, string $zone = null) |
|
34 | { |
||
35 | 9 | return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Gets section. |
||
40 | * |
||
41 | * @return string Returns section. |
||
42 | */ |
||
43 | public function getSection() : string |
||
44 | { |
||
45 | return $this->section; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Gets zone. |
||
50 | * |
||
51 | * @return string Returns zone. |
||
52 | */ |
||
53 | public function getZone() : string |
||
54 | { |
||
55 | return $this->zone; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Voter does not has a specific format. |
||
60 | * |
||
61 | * @return string Returns only numbers. |
||
62 | */ |
||
63 | 2 | public function format() : string |
|
64 | { |
||
65 | 2 | return "{$this}"; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 18 | public function calculateDigit(string $baseNumber) : string |
|
72 | { |
||
73 | 18 | $firstDigit = $this->calculateFirstDigit($baseNumber); |
|
74 | 18 | $secondDigit = $this->calculateSecondDigit("{$baseNumber}{$firstDigit}"); |
|
75 | |||
76 | 18 | return "{$firstDigit}{$secondDigit}"; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Calculate check digit from base number. |
||
81 | * |
||
82 | * @param string $baseNumber Base numeric section to be calculate your digit. |
||
83 | * |
||
84 | * @return string Returns the checker digit. |
||
85 | */ |
||
86 | 18 | private function calculateFirstDigit(string $baseNumber) : string |
|
87 | { |
||
88 | 18 | $calculator = new DigitCalculator(substr($baseNumber, 0, -2)); |
|
89 | 18 | $calculator->withMultipliers([9, 8, 7, 6, 5, 4, 3, 2]); |
|
90 | 18 | $calculator->withModule(DigitCalculator::MODULE_11); |
|
91 | 18 | $digit = $calculator->calculate(); |
|
92 | |||
93 | 18 | return "{$digit}"; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Calculate check digit from base number. |
||
98 | * |
||
99 | * @param string $baseNumber Base numeric section to be calculate your digit. |
||
100 | * |
||
101 | * @return string Returns the checker digit. |
||
102 | */ |
||
103 | 18 | private function calculateSecondDigit(string $baseNumber) : string |
|
104 | { |
||
105 | 18 | $calculator = new DigitCalculator(substr($baseNumber, -3)); |
|
106 | 18 | $calculator->withMultipliers([9, 8, 7]); |
|
107 | 18 | $calculator->withModule(DigitCalculator::MODULE_11); |
|
108 | 18 | $digit = $calculator->calculate(); |
|
109 | |||
110 | 18 | return "{$digit}"; |
|
111 | } |
||
112 | } |
||
113 |