Skip to content
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
2 changes: 1 addition & 1 deletion EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ $reader = new EDI\Reader($parser);
$groups = $reader->groupsExtract('INV');

foreach ($groups as $record) {
$parser->loadArray($record);
$parser->loadArray($record, false);
$r = EDI\Reader($parser);
$records[] = [
'storageLocation' => $r->readEdiDataValue(['LOC', ['2.0' => 'YA']], 2, 3),
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,11 @@ See section about EDI\Parser above on how to load a file into a parser.

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

EDI\Interpreter
Expand Down
27 changes: 17 additions & 10 deletions src/EDI/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,26 @@ public function loadString(string $txt): self
return $this;
}

/**
* Load the message from an array of strings.
/**
* Load a raw or parsed message from an array of strings.
* @param array $lines
* @param bool $raw If the data hasn't been parsed yet
* @return self
*/
public function loadArray(array $lines): self
*/
public function loadArray(array $lines, bool $raw = true): self
{
$this->resetUNA();
$this->resetUNB();
$this->rawSegments = $lines;
if (\count($lines) === 1) {
$this->loadString($lines[0]);
}
if($raw) {
$this->resetUNA();
$this->resetUNB();
$this->rawSegments = $lines;
if (\count($lines) === 1) {
$this->loadString($lines[0]);
}
} else {
$this->rawSegments = [];
$this->parsedfile = $lines;
}

return $this;
}

Expand Down
9 changes: 4 additions & 5 deletions src/EDI/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,13 @@ public function readGroups(string $before, string $start, string $end, string $a
}

/**
* Get groups from message when last segment is unknown but you know the barrier
* useful for invoices by default.
* 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<mixed>
* @return array Containing parsed lines
*/
public function groupsExtract(string $start = 'LIN', array $barrier = ['UNS']): array
{
Expand All @@ -511,8 +511,7 @@ public function groupsExtract(string $start = 'LIN', array $barrier = ['UNS']):
$segment = $edi_row[0];
if (
$position == 'group_is'
&&
(
&& (
$segment == $start
||
\in_array($segment, $barrier, true)
Expand Down
10 changes: 10 additions & 0 deletions tests/EDITest/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public function testArrayUnwrap()
static::assertSame($expected, $p->get());
}

public function testArrayParsed()
{
$arr = [['LOC', '11', 'ITGOA'], ['MEA', 'WT', '', ['KGM', '9040']]];
$p = new Parser();
$p->loadArray($arr,false)->parse();

$expected = [['LOC', '11', 'ITGOA'], ['MEA', 'WT', '', ['KGM', '9040']]];
static::assertSame($expected, $p->get());
}

public function testGetRawSegments()
{
$txt = "LOC+11+ITGOA'MEA+WT++KGM:9040'";
Expand Down