Skip to content

Commit

Permalink
Merge pull request #103 from duergner/master
Browse files Browse the repository at this point in the history
Add possibility to choose output key (id or name)
  • Loading branch information
sabas authored Feb 26, 2020
2 parents 87ee7ab + 03df029 commit 6c9af6b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
59 changes: 43 additions & 16 deletions src/EDI/Interpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Interpreter
];

/**
*
* @var bool
*/
private $patchFiles = true;

Expand Down Expand Up @@ -84,13 +84,18 @@ class Interpreter
*/
private $comparisonFunction;

/**
* @var string
*/
private $outputKey = 'name';

/**
* Split multiple messages and process
*
* @param string $xmlMsg Path to XML Message representation
* @param array $xmlSeg Segments processed by EDI\Analyser::loadSegmentsXml
* @param array $xmlSvc Service segments processed by EDI\Analyser::loadSegmentsXml
* @param array|null $messageTextConf Personalisation of error messages
* @param string $xmlMsg Path to XML Message representation
* @param array $xmlSeg Segments processed by EDI\Analyser::loadSegmentsXml
* @param array $xmlSvc Service segments processed by EDI\Analyser::loadSegmentsXml
* @param array|null $messageTextConf Personalisation of error messages
*/
public function __construct(string $xmlMsg, array $xmlSeg, array $xmlSvc, array $messageTextConf = null)
{
Expand Down Expand Up @@ -118,6 +123,11 @@ public function __construct(string $xmlMsg, array $xmlSeg, array $xmlSvc, array
};
}

/**
* @param bool $flag
*
* @return void
*/
public function togglePatching(bool $flag)
{
$this->patchFiles = $flag;
Expand Down Expand Up @@ -171,6 +181,23 @@ public function setComparisonFunction(callable $func)
$this->comparisonFunction = $func;
}

/**
* Set to true if UNCEFACT XML ID should be used instead of names
*
* @param bool $toggle
*
* @return void
*/
public function toggleUseIdInsteadOfNameForOutput(bool $toggle)
{
if ($toggle) {
$this->outputKey = 'id';
}
else {
$this->outputKey = 'name';
}
}

/**
* Split multiple messages and process
*
Expand Down Expand Up @@ -601,32 +628,32 @@ private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, ar
}

$d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
if (!isset($jsoncomposite[$d_sub_desc_attr['name']])) { //New
$jsoncomposite[$d_sub_desc_attr['name']] = $d_detail;
} elseif (\is_string($jsoncomposite[$d_sub_desc_attr['name']])) { // More data than one string
$jsoncomposite[$d_sub_desc_attr['name']] = [
$jsoncomposite[$d_sub_desc_attr['name']],
if (!isset($jsoncomposite[$d_sub_desc_attr[$this->outputKey]])) { //New
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]] = $d_detail;
} elseif (\is_string($jsoncomposite[$d_sub_desc_attr[$this->outputKey]])) { // More data than one string
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]] = [
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]],
$d_detail,
];
} else { // More and more
$jsoncomposite[$d_sub_desc_attr['name']][] = $d_detail;
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]][] = $d_detail;
}
}
} else {
$d_sub_desc_attr = $sub_details_desc[0]['attributes'];
$jsoncomposite[$d_sub_desc_attr['name']] = $detail;
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]] = $detail;
}
} else {
$jsoncomposite = $detail;
}

if (\array_key_exists($d_desc_attr['name'], $jsonelements)) {
$jsonelements[$d_desc_attr['name'] . $n] = $jsoncomposite;
if (\array_key_exists($d_desc_attr[$this->outputKey], $jsonelements)) {
$jsonelements[$d_desc_attr[$this->outputKey] . $n] = $jsoncomposite;
} else {
$jsonelements[$d_desc_attr['name']] = $jsoncomposite;
$jsonelements[$d_desc_attr[$this->outputKey]] = $jsoncomposite;
}
}
$jsonsegment['key'] = $attributes['name'];
$jsonsegment['key'] = $attributes[$this->outputKey];
$jsonsegment['value'] = $jsonelements;
} elseif ($xmlMap !== $this->xmlSvc) {
$jsonsegment = $this->processSegment($segment, $this->xmlSvc, $segmentIdx, $errors);
Expand Down
23 changes: 23 additions & 0 deletions tests/EDITest/InterpreterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,27 @@ public function testTooManyElements()
static::assertCount(0, $errors);
static::assertArrayHasKey('Extension2', $svcSegs['interchangeTrailer']);
}

public function testIdInsteadOfName()
{
$edi = \file_get_contents(__DIR__ . '/../files/D96ADESADV.edi');
$parser = new Parser($edi);
$mapping = new \EDI\Mapping\MappingProvider($parser->getMessageDirectory());
$analyser = new Analyser();
$segs = $analyser->loadSegmentsXml($mapping->getSegments());
$svc = $analyser->loadSegmentsXml($mapping->getServiceSegments(3));

$interpreter = new Interpreter($mapping->getMessage($parser->getMessageFormat()), $segs, $svc);
$interpreter->toggleUseIdInsteadOfNameForOutput(true);

$p = $interpreter->prepare($parser->get());
static::assertSame([], $parser->errors());

static::assertArrayHasKey('BGM', $p[0]);
static::assertArrayHasKey('C002', $p[0]['BGM']);
static::assertArrayHasKey('1001', $p[0]['BGM']['C002']);
static::assertSame('351', $p[0]['BGM']['C002']['1001']);
static::assertArrayHasKey('SG10', $p[0]);
static::assertArrayHasKey('CPS', $p[0]['SG10'][0]);
}
}

0 comments on commit 6c9af6b

Please sign in to comment.