Skip to content

Commit 40120ed

Browse files
committed
Extract function in interpreter
1 parent 0179699 commit 40120ed

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/EDI/Interpreter.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* EDIFACT Messages Interpreter
4-
* (c)2016 Stefano Sabatini
4+
* (c) 2018 Stefano Sabatini
55
*/
66

77
namespace EDI;
@@ -175,7 +175,7 @@ private function loopMessage($message, $xml, &$errors)
175175
}
176176

177177
/**
178-
* Proccess an XML Group
178+
* Process an XML Group
179179
*
180180
*/
181181
public function processXmlGroup($elm, $message, &$segmentIdx, &$array, &$errors)
@@ -212,16 +212,7 @@ private function processXmlSegment($elm, $message, &$segmentIdx, &$array, &$erro
212212
if ($message[$segmentIdx][0] == $elm['id']) {
213213
$jsonMessage = $this->processSegment($message[$segmentIdx], $this->xmlSeg, $segmentIdx, $errors);
214214
$segmentVisited = true;
215-
if (!isset($array[$jsonMessage['key']])) {
216-
$array[$jsonMessage['key']] = $jsonMessage['value'];
217-
} else {
218-
if (isset($array[$jsonMessage['key']]['segmentCode']) || $jsonMessage['key'] === 'UnrecognisedType') {
219-
$temp = $array[$jsonMessage['key']];
220-
$array[$jsonMessage['key']] = [];
221-
$array[$jsonMessage['key']][] = $temp;
222-
}
223-
$array[$jsonMessage['key']][] = $jsonMessage['value'];
224-
}
215+
$this->doAddArray($array, $jsonMessage);
225216
$segmentIdx++;
226217
} else {
227218
if (!$segmentVisited && isset($elm['required'])) {
@@ -236,6 +227,25 @@ private function processXmlSegment($elm, $message, &$segmentIdx, &$array, &$erro
236227
}
237228
}
238229

230+
/**
231+
* Adds a processed segment to the current group
232+
*
233+
* @param $array A reference to the group
234+
* @param $jsonMessage A segment processed by processSegment()
235+
*/
236+
private function doAddArray(&$array, $jsonMessage) {
237+
if (!isset($array[$jsonMessage['key']])) {
238+
$array[$jsonMessage['key']] = $jsonMessage['value'];
239+
} else {
240+
if (isset($array[$jsonMessage['key']]['segmentCode']) || $jsonMessage['key'] === 'UnrecognisedType') {
241+
$temp = $array[$jsonMessage['key']];
242+
$array[$jsonMessage['key']] = [];
243+
$array[$jsonMessage['key']][] = $temp;
244+
}
245+
$array[$jsonMessage['key']][] = $jsonMessage['value'];
246+
}
247+
}
248+
239249
/**
240250
* Add human readable keys as in Analyser
241251
*

0 commit comments

Comments
 (0)