-
Notifications
You must be signed in to change notification settings - Fork 4
/
v11.php
303 lines (247 loc) · 7.49 KB
/
v11.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
namespace Ticketpark\v11;
use Ticketpark\v11\Record\TotalRecord;
use Ticketpark\v11\Record\TransactionCode\TransactionCodeFactory;
use Ticketpark\v11\Record\TransactionRecord;
class v11
{
protected $lines = array();
protected $participantIdentifier;
protected $file;
protected $error;
/**
* Constructor
*
* @param mixed $contents
*/
public function __construct($contents = null)
{
if (is_array($contents)) {
$this->setLines($contents);
} elseif (null !== $contents) {
$this->setFile($contents);
}
}
public function setParticipantIdentifier($participantIdentifier)
{
$this->participantIdentifier = $participantIdentifier;
}
public function getParticipantIdentifier()
{
return $this->participantIdentifier;
}
/**
* Get the transaction records
*
* @return array
*/
public function getTransactionRecords()
{
$records = array();
foreach ($this->getTransactionLines() as $line) {
$records[] = $this->getTransactionRecord($line);
}
return $records;
}
/**
* Get the total record
*
* @return TotalRecord
*/
public function getTotalRecord()
{
$record = new TotalRecord();
$line = $this->getTotalLine();
if ($transactionCode = TransactionCodeFactory::create(substr($line, 0, 3))) {
$record->setTransactionCode($transactionCode);
}
$record
->setBankingAccount(substr($line, 3, 9))
->setSortingKey(substr($line, 12, 27))
->setAmount(substr($line, 39, 12) / 100)
->setNumberOfTransactions(substr($line, 51, 12) * 1)
->setDateFileCreation($this->createDate(substr($line, 63, 6)))
->setTotalFees((substr($line, 69, 9)) / 100);
return $record;
}
/**
* Set path to file containing v11 contents
*
* @param string $file
*/
public function setFile($file)
{
$this->file = $file;
return $this;
}
/**
* Get path to file
*
* @return string
*/
public function getFile()
{
return $this->file;
}
/**
* Set the raw lines
*
* @param array $lines
*/
public function setLines(array $lines)
{
$newLines = array();
foreach($lines as $line) {
$newLines[] = trim($line);
}
$this->lines = $newLines;
return $this;
}
/**
* Get the raw lines
*
* @return array
*/
public function getLines()
{
return $this->lines;
}
/**
* Get the raw transaction lines
*
* @return array
*/
public function getTransactionLines()
{
return array_slice($this->getLines(), 0, -1);
}
/**
* Get the raw total line
*
* @return string
*/
public function getTotalLine()
{
$lines = $this->getLines();
return array_pop($lines);
}
/**
* Creates a transaction record
*
* @param $line
* @return Record
*/
protected function getTransactionRecord($line)
{
$record = new TransactionRecord();
if ($transactionCode = TransactionCodeFactory::create(substr($line, 0, 3))) {
$record->setTransactionCode($transactionCode);
}
$record->setParticipantIdentifier($this->getParticipantIdentifier())
->setBankingAccount(substr($line, 3, 9))
->setReferenceNumber(substr($line, 12, 27))
->setAmount(substr($line, 39, 10) / 100)
->setInternalBankReference(substr($line, 49, 10))
->setDatePaid($this->createDate(substr($line, 59, 6)))
->setDateProcessed($this->createDate(substr($line, 65, 6)))
->setDateCreditNote($this->createDate(substr($line, 71, 6)))
->setMicrofilmReference(substr($line, 77, 9))
->setRejectCode(substr($line, 86, 1))
->setDateValuta($this->createDate(substr($line, 87, 6)))
->setFee((substr($line, 96, 4)) / 100);
return $record;
}
/**
* Validate the v11 contents
*
* @return bool
*/
public function validate()
{
if (null !== $this->getFile()) {
if (is_readable($this->getFile())) {
$this->setLines(file($this->getFile(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
} else {
$this->setError(sprintf('The provided file %s is not readable', $this->getFile()));
return false;
}
}
if (count($this->getLines()) == 0) {
return true;
}
$lines = $this->getLines();
// Last line is the total record
// It must have 87 characters and only consist of digits and spaces
$lastLine = array_pop($lines);
if (strlen($lastLine) != 87) {
$this->setError(sprintf('The total line contains %s instead of 87 characters', strlen($lastLine)));
return false;
}
if (!preg_match('/^[\d ]+$/', $lastLine)) {
$this->setError('The total line contains invalid characters. It may only contain digits and spaces');
return false;
}
// Transaction records must have 100 characters and only consist of digits and spaces
$i=1;
foreach ($lines as $line) {
if (strlen($line) != 100) {
$this->setError(sprintf('Line number %s contains %s instead of 100 characters', $i, strlen($line)));
return false;
}
if (!preg_match('/^[\d a-zA-Z]+$/', $line)) {
$this->setError(sprintf('Line number %s contains invalid characters. It may only contain digits, letters and spaces', $i));
return false;
}
$i++;
}
//Number of transactions must match
if ($this->getTotalRecord()->getNumberOfTransactions() != count($lines)) {
$this->setError(sprintf('The number of %s transactions does not match the number of %s transactions according to the total line', count($lines), $this->getTotalRecord()->getNumberOfTransactions()));
return false;
}
// All records must have a transaction code
if (null == $this->getTotalRecord()->getTransactionCode()) {
$this->setError(sprintf('The total line does not contain a valid transaction code'));
return false;
}
$i=1;
foreach($this->getTransactionRecords() as $record){
if (null == $record->getTransactionCode()) {
$this->setError(sprintf('Line number %s contains does not contain a valid transaction code', $i));
return false;
}
$i++;
}
return true;
}
/**
* Get the last error message
*
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* Set error message
*
* @param string $error
*/
protected function setError($error)
{
$this->error = $error;
}
/**
* Converts a 6-character string (YYMMDD) to a DateTime
* @param $string
* @return \DateTime|null
*/
protected function createDate($string)
{
if ('000000' == $string) {
return null;
}
return new \DateTime(substr($string, 0, 2) . '-' . substr($string, 2, 2) . '-' . substr($string, 4, 2));
}
}