Skip to content

Commit 29f4c57

Browse files
authored
Merge pull request #123 from gaxweb/master
Loading extracted groups back in caused error
2 parents 8bbd737 + eba89e1 commit 29f4c57

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

EXAMPLES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ $reader = new EDI\Reader($parser);
6565
$groups = $reader->groupsExtract('INV');
6666

6767
foreach ($groups as $record) {
68-
$parser->loadArray($record);
68+
$parser->loadArray($record, false);
6969
$r = EDI\Reader($parser);
7070
$records[] = [
7171
'storageLocation' => $r->readEdiDataValue(['LOC', ['2.0' => 'YA']], 2, 3),

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,11 @@ See section about EDI\Parser above on how to load a file into a parser.
477477

478478
Errors
479479
```php
480-
$c->errors();
480+
$r->errors();
481481
```
482482
Array
483483
```php
484-
$c->getParsedFile();
484+
$r->getParsedFile();
485485
```
486486

487487
EDI\Interpreter

src/EDI/Parser.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,26 @@ public function loadString(string $txt): self
341341
return $this;
342342
}
343343

344-
/**
345-
* Load the message from an array of strings.
344+
/**
345+
* Load a raw or parsed message from an array of strings.
346346
* @param array $lines
347+
* @param bool $raw If the data hasn't been parsed yet
347348
* @return self
348-
*/
349-
public function loadArray(array $lines): self
349+
*/
350+
public function loadArray(array $lines, bool $raw = true): self
350351
{
351-
$this->resetUNA();
352-
$this->resetUNB();
353-
$this->rawSegments = $lines;
354-
if (\count($lines) === 1) {
355-
$this->loadString($lines[0]);
356-
}
352+
if($raw) {
353+
$this->resetUNA();
354+
$this->resetUNB();
355+
$this->rawSegments = $lines;
356+
if (\count($lines) === 1) {
357+
$this->loadString($lines[0]);
358+
}
359+
} else {
360+
$this->rawSegments = [];
361+
$this->parsedfile = $lines;
362+
}
363+
357364
return $this;
358365
}
359366

src/EDI/Reader.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,13 @@ public function readGroups(string $before, string $start, string $end, string $a
492492
}
493493

494494
/**
495-
* Get groups from message when last segment is unknown but you know the barrier
496-
* useful for invoices by default.
495+
* Get groups from message when last segment is unknown but you know the barrier.
496+
* Useful for invoices by default.
497497
*
498498
* @param string $start first segment start a new group
499499
* @param array<array-key,string> $barrier barrier segment (NOT in group)
500500
*
501-
* @return array<mixed>
501+
* @return array Containing parsed lines
502502
*/
503503
public function groupsExtract(string $start = 'LIN', array $barrier = ['UNS']): array
504504
{
@@ -511,8 +511,7 @@ public function groupsExtract(string $start = 'LIN', array $barrier = ['UNS']):
511511
$segment = $edi_row[0];
512512
if (
513513
$position == 'group_is'
514-
&&
515-
(
514+
&& (
516515
$segment == $start
517516
||
518517
\in_array($segment, $barrier, true)

tests/EDITest/ParserTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public function testArrayUnwrap()
4242
static::assertSame($expected, $p->get());
4343
}
4444

45+
public function testArrayParsed()
46+
{
47+
$arr = [['LOC', '11', 'ITGOA'], ['MEA', 'WT', '', ['KGM', '9040']]];
48+
$p = new Parser();
49+
$p->loadArray($arr,false)->parse();
50+
51+
$expected = [['LOC', '11', 'ITGOA'], ['MEA', 'WT', '', ['KGM', '9040']]];
52+
static::assertSame($expected, $p->get());
53+
}
54+
4555
public function testGetRawSegments()
4656
{
4757
$txt = "LOC+11+ITGOA'MEA+WT++KGM:9040'";

0 commit comments

Comments
 (0)