Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca4f66a

Browse files
committed
Added parsed to segment DMT (date)
1 parent 9c0a6c7 commit ca4f66a

File tree

4 files changed

+112
-37
lines changed

4 files changed

+112
-37
lines changed

src/X12/Parse/Document/Document.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,3 @@ public function setSegments(array $segments): void
3939
$this->segments = $segments;
4040
}
4141
}
42-
?>

src/X12/Parse/Segments/DtmParser.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Aonach\X12\Parse\Segments;
4+
5+
/**
6+
* Class DtmParser
7+
* @package Aonach\X12\Parse\Segments
8+
*/
9+
class DtmParser implements SegmentParserInterface
10+
{
11+
12+
/**
13+
* To specify pertinent dates and times
14+
*/
15+
const DTM_00 = 'date_reference';
16+
17+
/**
18+
* Date/Time Qualifier
19+
*/
20+
const DTM_01 = 'date_qualifier';
21+
22+
/**
23+
* Date - CCYYMMDD
24+
*/
25+
const DTM_02 = 'date';
26+
27+
28+
/**
29+
* Code table with DTM_01 specifications
30+
*/
31+
const CODE_TABLE_01 = [63 => 'do_not_deliver_after', 64 => 'do_not_deliver_before'];
32+
33+
34+
/**
35+
* @param $segment
36+
* @return array
37+
*/
38+
public static function parse($segment)
39+
{
40+
$content = array();
41+
array_walk_recursive($segment, 'self::setContentType');
42+
foreach ($segment as $key => $item) {
43+
$content[key($item)] = $item[key($item)];
44+
}
45+
46+
return $content;
47+
}
48+
49+
/**
50+
* Set the the key to a meaningfull value.
51+
* @param $item
52+
* @param $key
53+
*/
54+
private static function setContentType(&$item, $key)
55+
{
56+
switch ($key) {
57+
case 0:
58+
$item = [self::DTM_00 => $item];
59+
break;
60+
case 1:
61+
$item = [self::DTM_01 => $item];
62+
break;
63+
case 2:
64+
$item = [self::DTM_02 => $item];
65+
break;
66+
default:
67+
break;
68+
}
69+
}
70+
}

src/X12/Parse/Segments/Parser.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Aonach\X12\Parse\Segments\SeParser;
1313
use Aonach\X12\Parse\Segments\GeParser;
1414
use Aonach\X12\Parse\Segments\IeaParser;
15-
15+
use Aonach\X12\Parse\Segments\DtmParser;
1616

1717
/**
1818
* Class Parser
@@ -27,40 +27,42 @@ class Parser
2727
*/
2828
public static function parseAllSegments(array $documents)
2929
{
30-
$documetParsed = array();
30+
$documentParsed = array();
3131
foreach ($documents as $document) {
3232
foreach ($document->getSegments() as $segment) {
3333
switch ($segment[0]) {
3434
case 'ISA':
35-
$documetParsed = array_merge_recursive($documetParsed, IsaParser::parse($segment));
35+
$documentParsed = array_merge_recursive($documentParsed, IsaParser::parse($segment));
3636
break;
3737
case 'GS';
38-
$documetParsed = array_merge_recursive($documetParsed, GsParser::parse($segment));
38+
$documentParsed = array_merge_recursive($documentParsed, GsParser::parse($segment));
3939
break;
4040
case 'ST';
41-
$documetParsed = array_merge_recursive($documetParsed, StParser::parse($segment));
41+
$documentParsed = array_merge_recursive($documentParsed, StParser::parse($segment));
4242
break;
4343
case 'BEG';
44-
$documetParsed = array_merge_recursive($documetParsed, BegParser::parse($segment));
44+
$documentParsed = array_merge_recursive($documentParsed, BegParser::parse($segment));
4545
break;
4646
case 'N1';
47-
$documetParsed = array_merge_recursive($documetParsed, N1Parser::parse($segment));
47+
$documentParsed = array_merge_recursive($documentParsed, N1Parser::parse($segment));
4848
break;
4949
case 'PO1';
50-
$documetParsed['po1'][] = Po1Parser::parse($segment);
50+
$documentParsed['po1'][] = Po1Parser::parse($segment);
5151
break;
5252
case 'CTT';
53-
$documetParsed = array_merge_recursive($documetParsed, CttParser::parse($segment));
53+
$documentParsed = array_merge_recursive($documentParsed, CttParser::parse($segment));
5454
break;
5555
case 'SE';
56-
$documetParsed = array_merge_recursive($documetParsed, SeParser::parse($segment));
56+
$documentParsed = array_merge_recursive($documentParsed, SeParser::parse($segment));
5757
break;
5858
case 'GE';
59-
$documetParsed = array_merge_recursive($documetParsed, GeParser::parse($segment));
59+
$documentParsed = array_merge_recursive($documentParsed, GeParser::parse($segment));
6060
break;
6161
case 'IEA';
62-
$documetParsed = array_merge_recursive($documetParsed, IeaParser::parse($segment));
62+
$documentParsed = array_merge_recursive($documentParsed, IeaParser::parse($segment));
6363
break;
64+
case 'DTM';
65+
$documentParsed['dtm'][] = DtmParser::parse($segment);
6466
default:
6567
break;
6668

@@ -69,6 +71,6 @@ public static function parseAllSegments(array $documents)
6971

7072
}
7173

72-
return $documetParsed;
74+
return $documentParsed;
7375
}
7476
}

src/X12/Parser.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
<?php
2+
23
namespace Aonach\X12;
34

45
use Aonach\X12\Parse\Document\Document;
56

67
/**
7-
* A class to parse ASC X12 EDI documents. Currently the entire document is
8-
* read into memory - this may change in future versions.
9-
*/
10-
class Parser {
11-
8+
* A class to parse ASC X12 EDI documents. Currently the entire document is
9+
* read into memory - this may change in future versions.
10+
*/
11+
class Parser
12+
{
13+
1214
const SEGMENT_TERMINATOR_POSITION = 105;
1315
const SUBELEMENT_SEPARATOR_POSITION = 104;
1416
const ELEMENT_SEPARATOR_POSITION = 3;
17+
1518
/**
16-
* Parse an EDI document. Data will be returned as an array of instances of
17-
* EDI\Document. Document should contain exactly one ISA/IEA envelope.
18-
*/
19-
public static function parse ($res) {
19+
* Parse an EDI document. Data will be returned as an array of instances of
20+
* EDI\Document. Document should contain exactly one ISA/IEA envelope.
21+
*/
22+
public static function parse($res)
23+
{
2024
$string = '';
2125
$segments = array();
2226

@@ -31,15 +35,15 @@ public static function parse ($res) {
3135
if (!$meta['seekable']) {
3236
throw new \Exception('Stream is not seekable');
3337
}
34-
35-
throw new \Exception('Not implemented!');
38+
39+
throw new \Exception('Not implemented!');
3640
} else {
3741
$data = $res;
3842
// treat as string.
3943
if (strcasecmp(substr($data, 0, 3), 'ISA') != 0) {
4044
throw new \Exception('ISA segment not found in data stream');
4145
}
42-
46+
4347
$segment_terminator = substr($data, self::SEGMENT_TERMINATOR_POSITION, 1);
4448
$element_separator = substr($data, self::ELEMENT_SEPARATOR_POSITION, 1);
4549
$subelement_separator = substr($data, self::SUBELEMENT_SEPARATOR_POSITION, 1);
@@ -66,21 +70,21 @@ public static function parse ($res) {
6670
}
6771
unset($element);
6872
}
69-
73+
7074
/* This is a ginormous switch statement, but necessarily so.
7175
* The idea is that the parser will, for each transaction set
7276
* in the ISA envelope, create a new Document instance with
7377
* the containing ISA and GS envelopes copied in.
7478
*/
7579
switch ($identifier) {
7680
case 'ISA':
77-
$current_isa = array( 'isa' => $elements );
81+
$current_isa = array('isa' => $elements);
7882
break;
7983
case 'GS':
80-
$current_gs = array( 'gs' => $elements );
84+
$current_gs = array('gs' => $elements);
8185
break;
8286
case 'ST':
83-
$current_st = array( 'st' => $elements );
87+
$current_st = array('st' => $elements);
8488
break;
8589
case 'SE':
8690
assert($current_gs != null, 'GS data structure isset');
@@ -91,7 +95,7 @@ public static function parse ($res) {
9195
array_push($current_gs['txn_sets'], $current_st);
9296
$current_st = null;
9397
break;
94-
case 'GE':
98+
case 'GE':
9599
assert($current_isa != null, 'ST data structure isset');
96100
$current_gs['ge'] = $elements;
97101
if (!isset($current_isa['func_groups'])) {
@@ -106,19 +110,19 @@ public static function parse ($res) {
106110
foreach ($gs['txn_sets'] as $st) {
107111
$segments = array_merge(
108112
array(
109-
$current_isa['isa'],
110-
$gs['gs'],
113+
$current_isa['isa'],
114+
$gs['gs'],
111115
$st['st']
112116
),
113117
$st['segments'],
114-
array(
118+
array(
115119
$st['se'],
116120
$gs['ge'],
117121
$current_isa['iea']
118122
)
119123
);
120124
$document = new Document($segments);
121-
array_push($documents, $document);
125+
array_push($documents, $document);
122126
}
123127
}
124128
break;
@@ -134,9 +138,9 @@ public static function parse ($res) {
134138
return $documents;
135139
}
136140

137-
public static function parseFile ($file) {
141+
public static function parseFile($file)
142+
{
138143
$contents = file_get_contents($file);
139144
return parse($contents);
140145
}
141146
}
142-
?>

0 commit comments

Comments
 (0)