Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding check and restructured file loading #122

Merged
merged 7 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,48 @@ Load a message and dump a json
Loading from file:
```php
use EDI\Parser;
$fn = "example.edi"; //it's a path!
$p = new EDI\Parser($fn);
$filePath = "example.edi";
$p = new EDI\Parser();
$p->load($filePath)->parse();
$p->checkEncoding(); // optional
if (count($p->errors()) > 0) {
echo json_encode($p->errors());
return;
}
echo json_encode($p->get());
```
Same example works for ```$fn``` containing a wrapped string (seg'seg) or an array ([seg,seg])
To load a single-line string (`"seg'seg"`) use `$p->loadString()`.
To load an array of lines (`["seg","seg"]`) use `$p->loadArray()`.

Convert a formatted array to EDIFACT message
--------------------------------------------
Loading from a php array:
Loading from a PHP array:
```php
use EDI\Encoder;
$arr = []; //array
$p = new EDI\Encoder($arr, false); //one segment per line
echo $p->get();
$enc = new EDI\Encoder($arr, false); //one segment per line
echo $enc->get();
```

Create from EDI file readable file with comments
------------------------------------------------
Create human-readable file with comments from EDI file
------------------------------------------------------

```php
$fileName = 'demo.edi';
$filePath = 'demo.edi';
$parser = new EDI\Parser();
$parsed = $parser->load($fileName);
$parser->load($filePath);
$segments = $parser->getRawSegments();

$analyser = new EDI\Analyser();
$analyser->loadSegmentsXml('edifact/src/EDI/Mapping/d95b/segments.xml');

$text = $analyser->process($parsed, $segments);
$text = $analyser->process($parsed, $parser->get());
```

EDI data reading from extracted group
-------------------------------------

As not to have to go through the indexes for extracted groups, just set the group as ParsedFile of the reader.
As not to have to go through the indexes for extracted groups, just use a reader with a different parser.

E.g. inventory messages (snippet, not a valid EDI message!):

Expand All @@ -56,16 +59,18 @@ INV+1++11'QTY+156:100:PCE'QTY+145:2300:PCE'LOC+18+YA:::567'DTM+179:20180510:102'
```

```php
$reader = new EDI\Reader($fileName);
$recordReader = EDI\Reader();
$parser = new EDI\Parser();
$parser->load($filePath);
$reader = new EDI\Reader($parser);
$groups = $reader->groupsExtract('INV');

foreach ($groups as $record) {
$recordReader->setParsedFile($record);
$parser->loadArray($record);
$r = EDI\Reader($parser);
$records[] = [
'storageLocation' => $recordReader->readEdiDataValue(['LOC', ['2.0' => 'YA']], 2, 3),
'bookingDate' => $recordReader->readEdiSegmentDTM(179),
'enteredOn' => $recordReader->readEdiSegmentDTM(171),
'storageLocation' => $r->readEdiDataValue(['LOC', ['2.0' => 'YA']], 2, 3),
'bookingDate' => $r->readEdiSegmentDTM(179),
'enteredOn' => $r->readEdiSegmentDTM(171),
'quantity' => $r->readEdiDataValue(['QTY', ['1.0' => 156]], 1, 1),
'actualStock' => $r->readEdiDataValue(['QTY', ['1.0' => 145]], 1, 1)
];
Expand Down Expand Up @@ -120,11 +125,13 @@ UNB - InterchangeHeader
```

EDI data element reading
-----------------
------------------------

```php
$fileName = 'files/truck_out_176699.edi';
$reader = new EDI\Reader($fileName);
$filePath = 'files/truck_out_176699.edi';
$parser = new EDI\Parser();
$parser->load($filePath);
$reader = new EDI\Reader($parser);

$record = [
'interchangeSender' => $reader->readEdiDataValue('UNB', 2),
Expand Down
79 changes: 31 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,20 @@ It's provided in a Composer package:

`composer require sabas/edifact`

The mapping xml files are provided in a separate package:
The mapping XML files are provided in a separate package:

`composer require php-edifact/edifact-mapping`

EDI\Parser
------------------
Given an edi message checks the syntax, outputs errors and returns the message as a multidimensional array.
Given an EDI message checks the syntax, outputs errors and returns the message as a multidimensional array.

**INPUT**
```php
$c = new Parser($x);
```
Where `$x` could be:
* a url
* a string (wrapped message)
* an array of strings (a segment per entry)

OR

```php
$c = new Parser();
$c->load($file);
$c->loadString($string);
$p = new EDI\Parser();
$p->load($location); // a local path to a file or a URL (if file_get_contents() allows for remote access)
$p->loadString($string); // a full message (single line text)
$p->loadArray($array); // an array of strings (one segment per entry)
```

**OUTPUT**
Expand All @@ -52,18 +43,18 @@ Given a multidimensional array (formatted as the output of the parser), returns

**INPUT**
```php
$c = new Encoder($x, $wrap = true);
$c = new EDI\Encoder($x, $compact = true);
```
`$x` is a multidimensional array where first dimension is the EDI segment, second contains elements:
* single value
* array (representing composite elements)

`$wrap` is a boolean, if you need a segment per line. Set to false to disable wrapping
`$compact` is a boolean, if you need a segment per line. Set to false to enable wrapped lines.

OR
```php
$c = new Encoder();
$c->encode($array, $wrap);
$c = new EDI\Encoder();
$c->encode($array, false);
```

**OUTPUT**
Expand All @@ -73,15 +64,16 @@ $c->get(); // returns String

EDI\Analyser
------------------
Create from EDI file readable structured text with comments from `segments.xml`.
Create human-readable, structured text with comments from `segments.xml`.
Requires the EDI\Mapping package.

```php
$parser = new Parser($file);
$parsed = $parser->get();
$parser = new EDI\Parser();
$parser->load($file);
$segments = $parser->getRawSegments();
$analyser = new Analyser();
$mapping = new MappingProvider('D95B');
$parsed = $parser->get();
$analyser = new EDI\Analyser();
$mapping = new EDI\MappingProvider('D95B');
$analyser->loadSegmentsXml($mapping->getSegments());
$analyser->loadMessageXml($mapping->getMessage('coparn'));
$analyser->loadCodesXml($mapping->getCodes());
Expand All @@ -91,7 +83,7 @@ Requires the EDI\Mapping package.
```
* `$file` is the path to orginal EDI message file

*** Example INPUT
### Example INPUT
```text
UNA:+,? '
UNB+UNOA:1+MAEU+LVRIXBCT+200813:0816+1412605'
Expand All @@ -110,7 +102,7 @@ UNT+12+141260500001'
UNZ+1+1412605'
```

*** Example Output
### Example Output
```text

UNA:+,? '
Expand Down Expand Up @@ -467,48 +459,39 @@ UNZ - InterchangeTrailer http://www.unece.org/trade/untdid/d95b/trsd/trsdunz.htm

EDI\Reader
------------------
Read from EDI file requested segment element values.
Read specific segment element values from parsed EDI file.
Pass the reader a parser with a message already loaded.

**INPUT**
```php
$r = new Reader($x);
$p = new EDI\Parser();
$p->load($x);
$r = new EDI\Reader($p);
$sender = $r->readEdiDataValue('UNB', 2);
$Dt = $r->readUNBDateTimeOfPreperation();

```
Where X could be:
* a url
* a string (wrapped message)
* an array of strings (a segment per entry)

OR

```php
$c = new Parser($x);

$r = new Reader();
$r->setParsedFile($c->get());
$sender = $r->readEdiDataValue('UNB', 2);
$Dt = $r->readUNBDateTimeOfPreperation();
```
See section about EDI\Parser above on how to load a file into a parser.

**OUTPUT**

Errors
```php
$c->errors();
```
Array
```php
$c->get();
$c->getParsedFile();
```

EDI\Interpreter
---------------
Organizes the data parsed by EDI/Parser using the xml description of the message and the xml segments.
Organizes the data parsed by EDI/Parser using the XML description of the message and the XML segments.

**INPUT**
```php
$p = new EDI\Parser($edifile);
$p = new EDI\Parser();
$p->load($edifile);
$edi = $p->get();

$mapping = new EDI\Mapping\MappingProvider('D95B');
Expand All @@ -523,12 +506,12 @@ $prep = $interpreter->prepare($edi);

**OUTPUT**

Json
JSON
```php
$interpreter->getJson()
```

Json for interchange service segments (UNB / UNZ)
JSON for interchange service segments (UNB / UNZ)
```php
$interpreter->getJsonServiceSegments()
```
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"require": {
"php": "~8",
"ext-json": "*",
"ext-mbstring": "*",
"ext-simplexml": "*"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/EDI/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace EDI;

/**
* EDIFACT Messages Parser
* EDIFACT Messages Analyser
* (c)2016 Uldis Nelsons
*/
class Analyser
Expand Down
Loading