Skip to content

Commit 588f0e8

Browse files
committed
Make comparison criteria customizable
1 parent 40120ed commit 588f0e8

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/EDI/Interpreter.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class Interpreter
1515
private $errors;
1616
private $msgs;
1717
private $serviceSeg;
18+
19+
private $comparisonFunction;
20+
1821
public $messageTextConf = [
1922
'MISSINGREQUIREDSEGMENT' => "Missing required segment",
2023
'NOTCONFORMANT' => "It looks like that this message isn't conformant to the mapping provided. (Not all segments were added)",
@@ -41,6 +44,20 @@ public function __construct($xmlMsg, $xmlSeg, $xmlSvc, $messageTextConf = null)
4144
$this->messageTextConf = array_replace($this->messageTextConf, $messageTextConf);
4245
}
4346
$this->errors = [];
47+
48+
$this->comparisonFunction = function ($segment, $elm) {
49+
return $segment[0] == $elm['id'];
50+
};
51+
}
52+
53+
/**
54+
* Split multiple messages and process
55+
*
56+
* @param $func A function accepting two arguments, first is the segment array, then the element definition
57+
* @return void
58+
*/
59+
public function setComparisonFunction(callable $func) {
60+
$this->comparisonFunction = $func;
4461
}
4562

4663
/**
@@ -178,7 +195,7 @@ private function loopMessage($message, $xml, &$errors)
178195
* Process an XML Group
179196
*
180197
*/
181-
public function processXmlGroup($elm, $message, &$segmentIdx, &$array, &$errors)
198+
private function processXmlGroup($elm, $message, &$segmentIdx, &$array, &$errors)
182199
{
183200
$newGroup = [];
184201
for ($g = 0; $g < $elm['maxrepeat']; $g++) {
@@ -209,7 +226,7 @@ private function processXmlSegment($elm, $message, &$segmentIdx, &$array, &$erro
209226
{
210227
$segmentVisited = false;
211228
for ($i = 0; $i < $elm['maxrepeat']; $i++) {
212-
if ($message[$segmentIdx][0] == $elm['id']) {
229+
if (call_user_func($this->comparisonFunction, $message[$segmentIdx], $elm)) {
213230
$jsonMessage = $this->processSegment($message[$segmentIdx], $this->xmlSeg, $segmentIdx, $errors);
214231
$segmentVisited = true;
215232
$this->doAddArray($array, $jsonMessage);

0 commit comments

Comments
 (0)