1 | <?php |
||
7 | abstract class AbstractDocument implements DocumentInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $number; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $digit; |
||
18 | |||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | protected $length; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $numberOfDigits; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $type; |
||
33 | |||
34 | /** |
||
35 | * AbstractDocument constructor. |
||
36 | * |
||
37 | * @param string $number Numeric section with checker digit. |
||
38 | * @param int $length Max length of document. |
||
39 | * @param int $numberOfDigits Max length of checker digits. |
||
40 | * @param string $type Document name/type. |
||
41 | */ |
||
42 | 548 | public function __construct($number, $length, $numberOfDigits, $type) |
|
43 | { |
||
44 | 548 | $this->type = (string) $type; |
|
45 | 548 | $this->numberOfDigits = (int) $numberOfDigits; |
|
46 | 548 | $this->length = (int) $length; |
|
47 | 548 | $this->digit = $this->extractCheckerDigit($number); |
|
48 | 548 | $this->validate($number); |
|
49 | 409 | $this->number = $number; |
|
50 | 409 | } |
|
51 | |||
52 | /** |
||
53 | * Check if document number is valid. |
||
54 | * |
||
55 | * @param string $number Numeric section with checker digit. |
||
56 | * |
||
57 | * @throws InvalidDocumentException when number is empty |
||
58 | * @throws InvalidDocumentException when number is not valid |
||
59 | */ |
||
60 | 548 | protected function validate($number) |
|
69 | |||
70 | /** |
||
71 | * Validates number is a valid. |
||
72 | * |
||
73 | * @param string $number Numeric section with checker digit. |
||
74 | * |
||
75 | * @return bool Returns true if it is a valid number, otherwise false. |
||
76 | */ |
||
77 | 491 | protected function isValid($number) |
|
90 | |||
91 | /** |
||
92 | * Handle number to string. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 409 | public function __toString() |
|
100 | |||
101 | /** |
||
102 | * Extracts the base number document. |
||
103 | * |
||
104 | * @param string $number Number of document. |
||
105 | * |
||
106 | * @return string Returns only base number without checker digit. |
||
107 | */ |
||
108 | 44 | protected function extractBaseNumber($number) |
|
112 | |||
113 | /** |
||
114 | * Extracts the checker digit from document number. |
||
115 | * |
||
116 | * @param string $number Number of document. |
||
117 | * |
||
118 | * @return string Returns only checker digit. |
||
119 | */ |
||
120 | 72 | protected function extractCheckerDigit($number) |
|
124 | } |
||
125 |