1 | <?php |
||
7 | class TransferMessage |
||
8 | { |
||
9 | /** |
||
10 | * Set divisor used to calculate the modulus |
||
11 | */ |
||
12 | const MODULO = 97; |
||
13 | |||
14 | /** |
||
15 | * Set the asterisk sign as a circumfix |
||
16 | */ |
||
17 | const CIRCUMFIX_ASTERISK = "*"; |
||
18 | |||
19 | /** |
||
20 | * Set the plus sign as a circumfix |
||
21 | */ |
||
22 | const CIRCUMFIX_PLUS = "+"; |
||
23 | |||
24 | /** |
||
25 | * The number used to generate a structured message |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | private $number; |
||
30 | |||
31 | /** |
||
32 | * The modulus resulting from the modulo operation |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | private $modulus; |
||
37 | |||
38 | /** |
||
39 | * A structured message with a valid formatting |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | private $structuredMessage = null; |
||
44 | |||
45 | /** |
||
46 | * Create a new instance |
||
47 | * |
||
48 | * @param int $number The number used to generate a structured message |
||
49 | */ |
||
50 | 16 | public function __construct($number = null) |
|
55 | |||
56 | /** |
||
57 | * Generate a valid structured message based on the number |
||
58 | * |
||
59 | * @param string $circumfix The circumfix. Defaults to the plus sign |
||
60 | * |
||
61 | * @return string A valid structured message |
||
62 | */ |
||
63 | 16 | public function generate($circumfix = self::CIRCUMFIX_PLUS) |
|
75 | |||
76 | /** |
||
77 | * The mod97 calculation |
||
78 | * |
||
79 | * If the modulus is 0, the result is substituted to 97 |
||
80 | * |
||
81 | * @param int $dividend The dividend |
||
82 | * |
||
83 | * @return int The modulus |
||
84 | */ |
||
85 | 16 | private function mod($dividend) |
|
91 | |||
92 | /** |
||
93 | * Get the number |
||
94 | * |
||
95 | * @return int The number used to generate a structured message |
||
96 | */ |
||
97 | 2 | public function getNumber() |
|
101 | |||
102 | /** |
||
103 | * Set the number |
||
104 | * |
||
105 | * If no number is passed to this method, a random number will be generated |
||
106 | * |
||
107 | * @param int $number The number used to generate a structured message |
||
108 | * |
||
109 | * @throws TransferMessageException If the number is out of bounds |
||
110 | */ |
||
111 | 16 | public function setNumber($number = null) |
|
133 | |||
134 | /** |
||
135 | * Get the modulus |
||
136 | * |
||
137 | * @return int The modulus resulting from the modulo operation |
||
138 | */ |
||
139 | 2 | public function getModulus() |
|
143 | |||
144 | /** |
||
145 | * Get the structured message |
||
146 | * |
||
147 | * @return string A valid formatted structured message |
||
148 | */ |
||
149 | 2 | public function getStructuredMessage() |
|
153 | |||
154 | /** |
||
155 | * Set a structured message |
||
156 | * |
||
157 | * @param string $structuredMessage A structured message |
||
158 | * |
||
159 | * @throws TransferMessageException If the format is not valid |
||
160 | */ |
||
161 | 4 | public function setStructuredMessage($structuredMessage) |
|
174 | |||
175 | /** |
||
176 | * Validates a structured message |
||
177 | * |
||
178 | * The validation is the mod97 calculation of the number and comparison of |
||
179 | * the result to the provided modulus. |
||
180 | * |
||
181 | * @return bool TRUE if valid, FALSE if invalid |
||
182 | */ |
||
183 | 2 | public function validate() |
|
194 | } |
||
195 |