-
Notifications
You must be signed in to change notification settings - Fork 88
/
Reader.php
596 lines (510 loc) · 16 KB
/
Reader.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
declare(strict_types=1);
namespace EDI;
/**
* EDIFACT Messages Reader
* (c)2016 Uldis Nelsons
*/
class Reader
{
/**
* @var Parser Holding parsed EDI file
*/
private $parser;
/**
* @var array<int,string>
*/
private $errors = [];
/**
* Reader constructor.
*
* @param Parser $parser With a message already loaded.
*/
public function __construct(Parser $parser)
{
$this->parser = $parser;
$this->preValidate();
}
/**
* Get errors
*
* @return array<int,string>
*/
public function errors(): array
{
return $this->errors;
}
/**
* reset errors
*
* @return void
*/
public function resetErrors()
{
$this->errors = [];
}
/**
* Returns the parsed message.
*
* @returns array
*/
public function getParsedFile(): array
{
return $this->parser->get();
}
/**
* Do initial validation
*/
public function preValidate(): bool
{
$this->errors = [];
if (! \is_array($this->getParsedFile())) {
$this->errors[] = 'Incorrect format parsed file';
return false;
}
$r = $this->readUNHmessageNumber();
if (
! $r
&&
(
$this->errors !== []
&&
$this->errors[0] == 'Segment "UNH" is ambiguous'
)
) {
$this->errors = [];
$this->errors[] = 'File has multiple messages';
return false;
}
return true;
}
/**
* Split multi messages to separate messages
*
*
* @return array<mixed>
*/
public static function splitMultiMessage(string $ediMessage): array
{
// init
$splicedMessages = [];
$message = [];
$unb = false;
$segment = '';
foreach (self::unwrap($ediMessage) as $segment) {
if (\strpos($segment, 'UNB') === 0) {
$unb = $segment;
continue;
}
if (\strpos($segment, 'UNH') === 0) {
if ($unb) {
$message[] = $unb;
}
$message[] = $segment;
continue;
}
if (\strpos($segment, 'UNT') === 0) {
$message[] = $segment;
$splicedMessages[] = $message;
$message = [];
continue;
}
if ($message) {
$message[] = $segment;
}
}
if (\strpos($segment, 'UNZ') === 0) {
$segment = \preg_replace('#UNZ\+\d+\+#', 'UNZ+1+', $segment);
foreach ($splicedMessages as $k => $message) {
$splicedMessages[$k][] = $segment;
}
}
foreach ($splicedMessages as $k => &$message) {
$message = \implode(\PHP_EOL, $splicedMessages[$k]);
}
return $splicedMessages;
}
/**
* read required value. if no found, registered error
*
* @param array<mixed>|string $filter segment filter by segment name and values
* @param false|int $l2
* @return string|null
*/
public function readEdiDataValueReq($filter, int $l1, $l2 = false)
{
return $this->readEdiDataValue($filter, $l1, $l2, true);
}
/**
* read data value from parsed EDI data
*
* @param array|string $filter 'AGR' - segment code
* or ['AGR',['1'=>'BB']], where AGR segment code and first element equal 'BB'
* or ['AGR',['1.0'=>'BB']], where AGR segment code and first element zero subelement
* equal 'BB'
* @param int $l1 first level item number (start by 1)
* @param false|int $l2 second level item number (start by 0)
* @param bool $required if required, but no exist, register error
* @param int|null $offset if multiple segments found, get segment by offset
* @return string|null
*/
public function readEdiDataValue($filter, int $l1, $l2 = false, bool $required = false, ?int $offset = null)
{
$found_segments = [];
$segment_name = $filter;
$filter_elements = false;
if (\is_array($filter)) {
$segment_name = $filter[0];
$filter_elements = $filter[1];
}
// search segments which conform to filter
foreach ($this->getParsedFile() as $edi_row) {
if ($edi_row[0] == $segment_name) {
if ($filter_elements) {
foreach ($filter_elements as $el_id => $el_value) {
$filter_ok = false;
$f_el_list = \explode('.', (string) $el_id);
if (\count($f_el_list) === 1) {
if (isset($edi_row[$el_id]) && $edi_row[$el_id] == $el_value) {
$filter_ok = true;
}
} elseif (
isset($edi_row[$f_el_list[0]])
&& (
(
isset($edi_row[$f_el_list[0]][$f_el_list[1]])
&& \is_array($edi_row[$f_el_list[0]])
&& $edi_row[$f_el_list[0]][$f_el_list[1]] == $el_value
) || (
isset($edi_row[$f_el_list[0]])
&& \is_string($edi_row[$f_el_list[0]])
&& $edi_row[$f_el_list[0]] == $el_value
)
)
) {
$filter_ok = true;
}
if ($filter_ok === false) {
break;
}
}
if ($filter_ok === false) {
continue;
}
}
$found_segments[] = $edi_row;
}
}
try {
if ($offset !== null) {
$segment = $this->getOffsetSegmentFromResult($found_segments, $offset, $required, $segment_name);
} else {
$segment = $this->getSegmentFromResult($found_segments, $required, $segment_name);
}
} catch (ReaderException $e) {
$this->errors[] = $e->getMessage();
return null;
}
if ($segment === false) {
return null;
}
// validate elements
if (! isset($segment[$l1])) {
if ($required) {
$this->errors[] = 'Segment value "'.$segment_name.'['.$l1.']" no exist';
}
return null;
}
// requested first level element
if ($l2 === false) {
return $segment[$l1];
}
// requested second level element, but not exist
if (! \is_array($segment[$l1]) || ! isset($segment[$l1][$l2])) {
if ($required) {
$this->errors[] = 'Segment value "'.$segment_name.'['.$l1.']['.$l2.']" no exist';
}
return null;
}
// second level element
return $segment[$l1][$l2];
}
/**
* read date from DTM segment period qualifier - codelist 2005
*
* @param int $PeriodQualifier period qualifier (codelist/2005)
* @return string|null YYYY-MM-DD HH:MM:SS
*/
public function readEdiSegmentDTM($PeriodQualifier)
{
$date = $this->readEdiDataValue([
'DTM', [
'1.0' => $PeriodQualifier,
]], 1, 1);
$format = $this->readEdiDataValue([
'DTM', [
'1.0' => $PeriodQualifier,
]], 1, 2);
if (empty($date)) {
return $date;
}
switch ($format) {
case 203: //CCYYMMDDHHMM
return \preg_replace('#(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)#', '$1-$2-$3 $4:$5:00', $date);
case 102: //CCYYMMDD
return \preg_replace('/(\d{4})(\d{2})(\d{2})/', '$1-$2-$3', $date);
default:
return $date;
}
}
/**
* @deprecated
*
* @return string|null
*/
public function readUNBDateTimeOfPpreperation()
{
return $this->readUNBDateTimeOfPreparation();
}
/**
* @deprecated
*
* @return string|null
*/
public function readUNBDateTimeOfPreperation()
{
return $this->readUNBDateTimeOfPreparation();
}
/**
* get message preparation time
*
* @return string|null
*/
public function readUNBDateTimeOfPreparation()
{
// separate date (YYMMDD) and time (HHMM)
$date = $this->readEdiDataValue('UNB', 4, 0);
if (! empty($date)) {
$time = $this->readEdiDataValue('UNB', 4, 1);
if ($time !== null) {
$time = (string) \preg_replace('#(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)#', '20$1-$2-$3 $4:$5:00', $date.$time);
}
return $time;
}
// common YYYYMMDDHHMM
$datetime = $this->readEdiDataValue('UNB', 4);
if ($datetime !== null) {
$datetime = (string) \preg_replace('#(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)#', '$1-$2-$3 $4:$5:00', $datetime);
}
return $datetime;
}
public function readUNBInterchangeSender()
{
return $this->readEdiDataValue('UNB', 2);
}
public function readUNBInterchangeRecipient()
{
return $this->readEdiDataValue('UNB', 3);
}
/**
* read transport identification number
*
* @param mixed $transportStageQualifier
* @return string|null
*/
public function readTDTtransportIdentification($transportStageQualifier)
{
$transportIdentification = $this->readEdiDataValue([
'TDT', [
'1' => $transportStageQualifier,
]], 8, 0);
if (! empty($transportIdentification)) {
return $transportIdentification;
}
return $this->readEdiDataValue([
'TDT', [
'1' => $transportStageQualifier,
]], 8);
}
/**
* read message type
*
* @return string|null
*/
public function readUNHmessageType()
{
return $this->readEdiDataValue('UNH', 2, 0);
}
/**
* read message number
*
* @return string|null
*/
public function readUNHmessageNumber()
{
return $this->readEdiDataValue('UNH', 1);
}
/**
* read message number
*
* @return string|null
*/
public function readUNHmessageRealise()
{
return $this->readEdiDataValue('UNH', 3);
}
/**
* Get groups from message.
*
* @param string $before segment before groups
* @param string $start first segment of group
* @param string $end last segment of group
* @param string $after segment after groups
* @return array<mixed>|false
*/
public function readGroups(string $before, string $start, string $end, string $after)
{
// init
$groups = [];
$group = [];
$position = 'before_search';
foreach ($this->getParsedFile() as $edi_row) {
// search before group segment
if ($position == 'before_search' && $edi_row[0] == $before) {
$position = 'before_is';
continue;
}
if ($position == 'before_search') {
continue;
}
if ($position == 'before_is' && $edi_row[0] == $before) {
continue;
}
// after before search start
if ($position == 'before_is' && $edi_row[0] == $start) {
$position = 'group_is';
$group[] = $edi_row;
continue;
}
// if after before segment no start segment, search again before segment
if ($position == 'before_is') {
$position = 'before_search';
continue;
}
// get group element
if ($position == 'group_is' && $edi_row[0] != $end) {
$group[] = $edi_row;
continue;
}
// found end of group
if ($position == 'group_is' && $edi_row[0] == $end) {
$position = 'group_finish';
$group[] = $edi_row;
$groups[] = $group;
$group = [];
continue;
}
// next group start
if ($position == 'group_finish' && $edi_row[0] == $start) {
$group[] = $edi_row;
$position = 'group_is';
continue;
}
// finish
if ($position == 'group_finish' && $edi_row[0] == $after) {
break;
}
$this->errors[] = 'Reading group '.$before.'/'.$start.'/'.$end.'/'.$after
.'. Error on position: '.$position;
return false;
}
return $groups;
}
/**
* Get groups from message when last segment is unknown but you know the barrier.
* Useful for invoices by default.
*
* @param string $start first segment start a new group
* @param array<array-key,string> $barrier barrier segment (NOT in group)
* @return array Containing parsed lines
*/
public function groupsExtract(string $start = 'LIN', array $barrier = ['UNS']): array
{
// init
$groups = [];
$group = [];
$position = 'before_search';
foreach ($this->getParsedFile() as $edi_row) {
$segment = $edi_row[0];
if (
$position == 'group_is'
&& (
$segment == $start
||
\in_array($segment, $barrier, true)
)
) {
// end of group
$groups[] = $group;
// start new group
$group = [];
$position = 'group_finish';
}
if ($segment == $start) {
$position = 'group_is';
}
// add element to group
if ($position == 'group_is') {
$group[] = $edi_row;
}
}
return $groups;
}
/**
* unwrap string splitting rows on terminator (if not escaped)
*
* @param string $string
* @return \Generator<string>
*/
private static function unwrap($string)
{
$array = \preg_split("/(?<!\?)'/", $string);
if ($array === false) {
return;
}
foreach ($array as &$line) {
$line = \preg_replace('#[\x00\r\n]#', '', $line);
$temp = $line."'";
if ($temp !== "'") {
yield $temp;
}
}
}
/**
* @return false|mixed
*/
private function getOffsetSegmentFromResult(array $matchingSegments, int $offset, bool $required, mixed $segment_name): mixed
{
if (isset($matchingSegments[$offset])) {
return $matchingSegments[$offset];
}
if ($required) {
throw new ReaderException('Segment "'.$segment_name.'" does not exist at offset "'.$offset.'"');
}
return false;
}
/**
* @return false|mixed
*/
private function getSegmentFromResult(array $matchingSegments, bool $required, mixed $segment_name): mixed
{
// found more than one segment - error
if (count($matchingSegments) > 1) {
throw new ReaderException('Segment "'.$segment_name.'" is ambiguous');
}
if ($required && ! isset($matchingSegments[0])) {
throw new ReaderException('Segment "'.$segment_name.'" no exist');
}
return $matchingSegments[0] ?? false;
}
}