1 | <?php |
||
33 | class DigitCalculator |
||
34 | { |
||
35 | const MODULE_10 = 10; |
||
36 | |||
37 | const MODULE_11 = 11; |
||
38 | |||
39 | /** |
||
40 | * A list for digits. |
||
41 | * |
||
42 | * @var \ArrayObject |
||
43 | */ |
||
44 | protected $number; |
||
45 | |||
46 | /** |
||
47 | * A list of integer multipliers. |
||
48 | * |
||
49 | * @var \ArrayObject |
||
50 | */ |
||
51 | protected $multipliers; |
||
52 | |||
53 | /** |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $additional = false; |
||
58 | |||
59 | /** |
||
60 | * @var int |
||
61 | */ |
||
62 | protected $module = DigitCalculator::MODULE_11; |
||
63 | |||
64 | /** |
||
65 | * @var bool |
||
66 | */ |
||
67 | protected $singleSum; |
||
68 | |||
69 | /** |
||
70 | * @var \ArrayObject |
||
71 | */ |
||
72 | private $replacements; |
||
73 | |||
74 | /** |
||
75 | * Creates object to be filled with fluent interface and store a numeric section into |
||
76 | * a list of digits. It is required because the numeric section could be so bigger than a integer number supports. |
||
77 | * |
||
78 | * @param string $number Base numeric section to be calculate your digit. |
||
79 | */ |
||
80 | 33 | public function __construct($number) |
|
89 | |||
90 | /** |
||
91 | * Sequential multipliers (or coefficient) and ascending order, this method allow |
||
92 | * to create a list of multipliers. |
||
93 | * |
||
94 | * It will be used in cycle, when the base number is larger than multipliers sequence. |
||
95 | * By default, multipliers are started with 2-9. |
||
96 | * |
||
97 | * You can enter another value and this default will be overwritten. |
||
98 | * |
||
99 | * @param int $start First number of sequential interval of multipliers |
||
100 | * @param int $end Last number of sequential interval of multipliers |
||
101 | * |
||
102 | * @return DigitCalculator |
||
103 | */ |
||
104 | 33 | public function withMultipliersInterval($start, $end) |
|
113 | |||
114 | /** |
||
115 | * There are some documents in which the multipliers do not use all the numbers in a range or |
||
116 | * change your order. |
||
117 | * |
||
118 | * In such cases, the multipliers list can be passed through array of integers. |
||
119 | * |
||
120 | * @param int[] $multipliers A list of integers sequence, such as: [9, 8, 7, 6, 5, 4, 3, 2, 1]. |
||
121 | * |
||
122 | * @return DigitCalculator |
||
123 | */ |
||
124 | public function withMultipliers(array $multipliers) |
||
137 | |||
138 | /** |
||
139 | * It is common digit generators need additional module instead of module itself. |
||
140 | * |
||
141 | * So to call this method enables a flag that is used in module method to decide |
||
142 | * if the returned result is pure module or its complementary. |
||
143 | * |
||
144 | * @return DigitCalculator |
||
145 | */ |
||
146 | 26 | public function useComplementaryInsteadOfModule() |
|
152 | |||
153 | /** |
||
154 | * There are some documents with specific rules for calculated digits. |
||
155 | * |
||
156 | * Some cases is possible to find X as digit checker. |
||
157 | * |
||
158 | * @param string $replaceTo A string to replace a digit. |
||
159 | * @param int[] $integers A list of numbers to be replaced by $replaceTo |
||
160 | * |
||
161 | * @return DigitCalculator |
||
162 | */ |
||
163 | 28 | public function replaceWhen($replaceTo, ...$integers) |
|
171 | |||
172 | /** |
||
173 | * Full whereby the rest will be taken and also its complementary. |
||
174 | * |
||
175 | * The default value is DigitCalculator::MODULE_11. |
||
176 | * |
||
177 | * @param int $module A integer to define module (DigitCalculator::MODULE_11 or DigitCalculator::MODULE_10) |
||
178 | * |
||
179 | * @return DigitCalculator |
||
180 | */ |
||
181 | 33 | public function withModule($module) |
|
187 | |||
188 | /** |
||
189 | * Indicates whether to calculate the module, the sum of the multiplication results |
||
190 | * should be considered digit by digit. |
||
191 | * |
||
192 | * Eg: 2 * 9 = 18, sum = 9 (1 + 8) instead of 18 |
||
193 | * |
||
194 | * @return DigitCalculator |
||
195 | */ |
||
196 | public function singleSum() |
||
202 | |||
203 | /** |
||
204 | * Calculates the check digit from given numeric section. |
||
205 | * |
||
206 | * @return string Returns a single calculated digit. |
||
207 | */ |
||
208 | 33 | public function calculate() |
|
225 | |||
226 | /** |
||
227 | * Replaces the digit when mapped to be replaced by other digit. |
||
228 | * |
||
229 | * @param string $digit A digit to be replaced. |
||
230 | * |
||
231 | * @return string Returns digit replaced if it has been mapped, otherwise returns given digit. |
||
232 | */ |
||
233 | 33 | private function replaceDigit($digit) |
|
241 | |||
242 | /** |
||
243 | * Calculates additional digit when is additional is defined. |
||
244 | * |
||
245 | * @param string $digit A digit to be subtract from module. |
||
246 | * |
||
247 | * @return int Returns calculated digit. |
||
248 | */ |
||
249 | 33 | private function calculateAdditionalDigit($digit) |
|
257 | |||
258 | /** |
||
259 | * Calculates single sum. |
||
260 | * |
||
261 | * @param int $total A total to be calculated. |
||
262 | * |
||
263 | * @return float Returns a calculated total. |
||
264 | */ |
||
265 | 33 | private function calculateSingleSum($total) |
|
273 | |||
274 | /** |
||
275 | * Gets the next multiplier. |
||
276 | * |
||
277 | * @param int $position Current position. |
||
278 | * |
||
279 | * @return int Returns next position or zero (0) when it is greater than number of defined multipliers. |
||
280 | */ |
||
281 | 33 | private function nextMultiplier($position) |
|
290 | |||
291 | /** |
||
292 | * Adds a digit into number collection. |
||
293 | * |
||
294 | * @param string $digit Digit to be prepended into number collection. |
||
295 | * |
||
296 | * @return DigitCalculator |
||
297 | */ |
||
298 | 12 | public function addDigit($digit) |
|
306 | } |
||
307 |