Skip to content

Commit 6c9af6b

Browse files
authored
Merge pull request #103 from duergner/master
Add possibility to choose output key (id or name)
2 parents 87ee7ab + 03df029 commit 6c9af6b

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

src/EDI/Interpreter.php

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Interpreter
4040
];
4141

4242
/**
43-
*
43+
* @var bool
4444
*/
4545
private $patchFiles = true;
4646

@@ -84,13 +84,18 @@ class Interpreter
8484
*/
8585
private $comparisonFunction;
8686

87+
/**
88+
* @var string
89+
*/
90+
private $outputKey = 'name';
91+
8792
/**
8893
* Split multiple messages and process
8994
*
90-
* @param string $xmlMsg Path to XML Message representation
91-
* @param array $xmlSeg Segments processed by EDI\Analyser::loadSegmentsXml
92-
* @param array $xmlSvc Service segments processed by EDI\Analyser::loadSegmentsXml
93-
* @param array|null $messageTextConf Personalisation of error messages
95+
* @param string $xmlMsg Path to XML Message representation
96+
* @param array $xmlSeg Segments processed by EDI\Analyser::loadSegmentsXml
97+
* @param array $xmlSvc Service segments processed by EDI\Analyser::loadSegmentsXml
98+
* @param array|null $messageTextConf Personalisation of error messages
9499
*/
95100
public function __construct(string $xmlMsg, array $xmlSeg, array $xmlSvc, array $messageTextConf = null)
96101
{
@@ -118,6 +123,11 @@ public function __construct(string $xmlMsg, array $xmlSeg, array $xmlSvc, array
118123
};
119124
}
120125

126+
/**
127+
* @param bool $flag
128+
*
129+
* @return void
130+
*/
121131
public function togglePatching(bool $flag)
122132
{
123133
$this->patchFiles = $flag;
@@ -171,6 +181,23 @@ public function setComparisonFunction(callable $func)
171181
$this->comparisonFunction = $func;
172182
}
173183

184+
/**
185+
* Set to true if UNCEFACT XML ID should be used instead of names
186+
*
187+
* @param bool $toggle
188+
*
189+
* @return void
190+
*/
191+
public function toggleUseIdInsteadOfNameForOutput(bool $toggle)
192+
{
193+
if ($toggle) {
194+
$this->outputKey = 'id';
195+
}
196+
else {
197+
$this->outputKey = 'name';
198+
}
199+
}
200+
174201
/**
175202
* Split multiple messages and process
176203
*
@@ -601,32 +628,32 @@ private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, ar
601628
}
602629

603630
$d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
604-
if (!isset($jsoncomposite[$d_sub_desc_attr['name']])) { //New
605-
$jsoncomposite[$d_sub_desc_attr['name']] = $d_detail;
606-
} elseif (\is_string($jsoncomposite[$d_sub_desc_attr['name']])) { // More data than one string
607-
$jsoncomposite[$d_sub_desc_attr['name']] = [
608-
$jsoncomposite[$d_sub_desc_attr['name']],
631+
if (!isset($jsoncomposite[$d_sub_desc_attr[$this->outputKey]])) { //New
632+
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]] = $d_detail;
633+
} elseif (\is_string($jsoncomposite[$d_sub_desc_attr[$this->outputKey]])) { // More data than one string
634+
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]] = [
635+
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]],
609636
$d_detail,
610637
];
611638
} else { // More and more
612-
$jsoncomposite[$d_sub_desc_attr['name']][] = $d_detail;
639+
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]][] = $d_detail;
613640
}
614641
}
615642
} else {
616643
$d_sub_desc_attr = $sub_details_desc[0]['attributes'];
617-
$jsoncomposite[$d_sub_desc_attr['name']] = $detail;
644+
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]] = $detail;
618645
}
619646
} else {
620647
$jsoncomposite = $detail;
621648
}
622649

623-
if (\array_key_exists($d_desc_attr['name'], $jsonelements)) {
624-
$jsonelements[$d_desc_attr['name'] . $n] = $jsoncomposite;
650+
if (\array_key_exists($d_desc_attr[$this->outputKey], $jsonelements)) {
651+
$jsonelements[$d_desc_attr[$this->outputKey] . $n] = $jsoncomposite;
625652
} else {
626-
$jsonelements[$d_desc_attr['name']] = $jsoncomposite;
653+
$jsonelements[$d_desc_attr[$this->outputKey]] = $jsoncomposite;
627654
}
628655
}
629-
$jsonsegment['key'] = $attributes['name'];
656+
$jsonsegment['key'] = $attributes[$this->outputKey];
630657
$jsonsegment['value'] = $jsonelements;
631658
} elseif ($xmlMap !== $this->xmlSvc) {
632659
$jsonsegment = $this->processSegment($segment, $this->xmlSvc, $segmentIdx, $errors);

tests/EDITest/InterpreterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,27 @@ public function testTooManyElements()
234234
static::assertCount(0, $errors);
235235
static::assertArrayHasKey('Extension2', $svcSegs['interchangeTrailer']);
236236
}
237+
238+
public function testIdInsteadOfName()
239+
{
240+
$edi = \file_get_contents(__DIR__ . '/../files/D96ADESADV.edi');
241+
$parser = new Parser($edi);
242+
$mapping = new \EDI\Mapping\MappingProvider($parser->getMessageDirectory());
243+
$analyser = new Analyser();
244+
$segs = $analyser->loadSegmentsXml($mapping->getSegments());
245+
$svc = $analyser->loadSegmentsXml($mapping->getServiceSegments(3));
246+
247+
$interpreter = new Interpreter($mapping->getMessage($parser->getMessageFormat()), $segs, $svc);
248+
$interpreter->toggleUseIdInsteadOfNameForOutput(true);
249+
250+
$p = $interpreter->prepare($parser->get());
251+
static::assertSame([], $parser->errors());
252+
253+
static::assertArrayHasKey('BGM', $p[0]);
254+
static::assertArrayHasKey('C002', $p[0]['BGM']);
255+
static::assertArrayHasKey('1001', $p[0]['BGM']['C002']);
256+
static::assertSame('351', $p[0]['BGM']['C002']['1001']);
257+
static::assertArrayHasKey('SG10', $p[0]);
258+
static::assertArrayHasKey('CPS', $p[0]['SG10'][0]);
259+
}
237260
}

0 commit comments

Comments
 (0)