1 | <?php |
||
16 | abstract class AbstractDocument implements DigitCalculable, Formattable |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $number; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $digit; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $length; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $numberOfDigits; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $type; |
||
42 | |||
43 | /** |
||
44 | * AbstractDocument constructor. |
||
45 | * |
||
46 | * @param string $number Numeric section with checker digit. |
||
47 | * @param int $length Max length of document. |
||
48 | * @param int $numberOfDigits Max length of checker digits. |
||
49 | * @param string $type Document name/type. |
||
50 | */ |
||
51 | 1152 | public function __construct( |
|
52 | string $number, |
||
53 | int $length, |
||
54 | int $numberOfDigits, |
||
55 | string $type |
||
56 | ) { |
||
57 | 1152 | $this->type = $type; |
|
58 | 1152 | $this->numberOfDigits = $numberOfDigits; |
|
59 | 1152 | $this->length = $length; |
|
60 | 1152 | $this->digit = $this->extractCheckerDigit($number); |
|
61 | 1152 | $this->assert($number); |
|
62 | 888 | $this->number = $number; |
|
63 | 888 | } |
|
64 | |||
65 | 5 | public function __get(string $name) |
|
69 | |||
70 | public function __set(string $name, string $value) |
||
74 | |||
75 | /** |
||
76 | * Create a Document object from given number. |
||
77 | * |
||
78 | * @param string $number Numeric section with checker digit. |
||
79 | * |
||
80 | * @return AbstractDocument|boolean Returns a new Document instance or FALSE on failure. |
||
81 | */ |
||
82 | abstract public static function createFromString(string $number); |
||
83 | |||
84 | /** |
||
85 | * Try to create a Document object from given number. |
||
86 | * |
||
87 | * @param string $number Numeric section with checker digit. |
||
88 | * @param int $length Max length of document. |
||
89 | * @param int $numberOfDigits Max length of checker digits. |
||
90 | * @param string $type Document name/type. |
||
91 | * |
||
92 | * @return AbstractDocument|boolean Returns a new Document instance or FALSE on failure. |
||
93 | */ |
||
94 | 129 | protected static function tryCreateFromString( |
|
107 | |||
108 | /** |
||
109 | * Handle number to string. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 883 | public function __toString() : string |
|
117 | |||
118 | /** |
||
119 | * Check if document number is valid. |
||
120 | * |
||
121 | * @param string $number Numeric section with checker digit. |
||
122 | * |
||
123 | * @throws Exception\InvalidDocument when number is empty |
||
124 | * @throws Exception\InvalidDocument when number is not valid |
||
125 | */ |
||
126 | 1152 | protected function assert(string $number) |
|
135 | |||
136 | /** |
||
137 | * Validates number is a valid. |
||
138 | * |
||
139 | * @param string $number Numeric section with checker digit. |
||
140 | * |
||
141 | * @return bool Returns true if it is a valid number, otherwise false. |
||
142 | */ |
||
143 | 1152 | protected function isValid(string $number) : bool |
|
161 | |||
162 | /** |
||
163 | * Extracts the base number document. |
||
164 | * |
||
165 | * @param string $number Number of document. |
||
166 | * |
||
167 | * @return string Returns only base number without checker digit. |
||
168 | */ |
||
169 | 248 | protected function extractBaseNumber(string $number) : string |
|
173 | |||
174 | /** |
||
175 | * Extracts the checker digit from document number. |
||
176 | * |
||
177 | * @param string $number Number of document. |
||
178 | * |
||
179 | * @return string Returns only checker digit. |
||
180 | */ |
||
181 | 248 | protected function extractCheckerDigit(string $number) : string |
|
185 | } |
||
186 |