Skip to content

Commit ab27826

Browse files
committed
ecs, improvements in rebuildArray
1 parent cce1d49 commit ab27826

File tree

13 files changed

+350
-257
lines changed

13 files changed

+350
-257
lines changed

src/EDI/Encoder.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ public function __construct($array = null, $compact = true)
8080
/**
8181
* @param array[] $array
8282
* @param bool $compact All segments on a single line?
83-
* @param bool $filterKeys
8483
*/
85-
public function encode(array $array, $compact = true, $filterKeys = false): string
84+
public function encode(array $array, $compact = true): string
8685
{
8786
$this->originalArray = $array;
8887
$this->compact = $compact;
@@ -92,9 +91,6 @@ public function encode(array $array, $compact = true, $filterKeys = false): stri
9291
$k = 0;
9392
foreach ($array as $row) {
9493
$k++;
95-
if ($filterKeys) {
96-
unset($row['segmentIdx']);
97-
}
9894
$row = \array_values($row);
9995
$edistring .= $this->encodeSegment($row);
10096
if (! $compact && $k < $count) {

src/EDI/Interpreter.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Interpreter
2323
'MISSINGMESSAGEDELIMITER' => 'The message has at least one UNH or UNT missing',
2424
'TOOMANYSEGMENTS' => 'The message has some additional segments beyond the maximum repetition allowed',
2525
'TOOMANYGROUPS' => 'The message has some additional groups beyond the maximum repetition allowed',
26-
'SPURIOUSSEGMENT' => 'This segment is spurious'
26+
'SPURIOUSSEGMENT' => 'This segment is spurious',
2727
];
2828

2929
/**
@@ -160,16 +160,17 @@ public function __construct(string $xmlMsg, array $xmlSeg, array $xmlSvc, array
160160
public function togglePatching(bool $flag)
161161
{
162162
$this->patchFiles = $flag;
163+
163164
return $this;
164165
}
165166

166-
167167
/**
168168
* @return void
169169
*/
170170
public function toggleSegmentGroup(bool $flag)
171171
{
172172
$this->segmentGroup = $flag;
173+
173174
return $this;
174175
}
175176

@@ -179,6 +180,7 @@ public function toggleSegmentGroup(bool $flag)
179180
public function forceArrayWhenRepeatable(bool $flag)
180181
{
181182
$this->forceArrayWhenRepeatable = $flag;
183+
182184
return $this;
183185
}
184186

@@ -191,6 +193,7 @@ public function forceArrayWhenRepeatable(bool $flag)
191193
public function setMessageTextConf(array $messageTextConf)
192194
{
193195
$this->messageTextConf = \array_replace($this->messageTextConf, $messageTextConf);
196+
194197
return $this;
195198
}
196199

@@ -203,6 +206,7 @@ public function setMessageTextConf(array $messageTextConf)
203206
public function setSegmentTemplates(array $segmentTemplates)
204207
{
205208
$this->segmentTemplates = $segmentTemplates;
209+
206210
return $this;
207211
}
208212

@@ -215,6 +219,7 @@ public function setSegmentTemplates(array $segmentTemplates)
215219
public function setGroupTemplates(array $groupTemplates)
216220
{
217221
$this->groupTemplates = $groupTemplates;
222+
218223
return $this;
219224
}
220225

@@ -227,6 +232,7 @@ public function setGroupTemplates(array $groupTemplates)
227232
public function setCodes(array $codes)
228233
{
229234
$this->codes = $codes;
235+
230236
return $this;
231237
}
232238

@@ -239,6 +245,7 @@ public function setCodes(array $codes)
239245
public function setComparisonFunction(callable $func)
240246
{
241247
$this->comparisonFunction = $func;
248+
242249
return $this;
243250
}
244251

@@ -251,6 +258,7 @@ public function setComparisonFunction(callable $func)
251258
public function setReplacementFunction(callable $func)
252259
{
253260
$this->replacementFunction = $func;
261+
254262
return $this;
255263
}
256264

@@ -267,6 +275,7 @@ public function toggleUseIdInsteadOfNameForOutput(bool $toggle)
267275
} else {
268276
$this->outputKey = 'name';
269277
}
278+
270279
return $this;
271280
}
272281

@@ -599,7 +608,7 @@ private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int
599608
$segmentVisited = true;
600609
$this->doAddArray($array, $jsonMessage, (int) $elm['maxrepeat']);
601610
$segmentIdx++;
602-
} else if ($this->replacementFunction !== null && $replacementSegment = \call_user_func($this->replacementFunction, $message[$segmentIdx], $elm)) {
611+
} elseif ($this->replacementFunction !== null && $replacementSegment = \call_user_func($this->replacementFunction, $message[$segmentIdx], $elm)) {
603612
//the function shall return false, true or a new segment
604613
$fixed = false;
605614

@@ -627,18 +636,20 @@ private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int
627636
'segmentId' => (string) $elm['id'],
628637
];
629638
$segmentIdx++;
639+
630640
continue;
631641
} else {
632642
if (! $segmentVisited && isset($elm['required'])) {
633643
$segmentVisited = true;
634-
if (isset($message[$segmentIdx+1]) && \call_user_func($this->comparisonFunction, $message[$segmentIdx+1], $elm)) {
644+
if (isset($message[$segmentIdx + 1]) && \call_user_func($this->comparisonFunction, $message[$segmentIdx + 1], $elm)) {
635645
$errors[] = [
636646
'text' => $this->messageTextConf['SPURIOUSSEGMENT'].($this->patchFiles ? ' (skipped)' : ''),
637647
'position' => $segmentIdx,
638648
'segmentId' => (string) $message[$segmentIdx][0],
639649
];
640650
$segmentIdx++; //just move the index
641651
$i--; //but don't count as repetition
652+
642653
continue;
643654
}
644655

@@ -672,8 +683,8 @@ private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int
672683
$loopMove = 0;
673684
while (
674685
isset($message[$segmentIdx]) &&
675-
\call_user_func($this->comparisonFunction, $message[$segmentIdx+$loopMove], $elm) &&
676-
(string)$elm['id'] !== $this->currentGroupHeader
686+
\call_user_func($this->comparisonFunction, $message[$segmentIdx + $loopMove], $elm) &&
687+
(string) $elm['id'] !== $this->currentGroupHeader
677688
) {
678689
$errors[] = [
679690
'text' => $this->messageTextConf['TOOMANYSEGMENTS'].($this->patchFiles ? ' (skipped)' : ''),
@@ -735,7 +746,7 @@ private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, ar
735746

736747
$jsonelements = [
737748
'segmentIdx' => $segmentIdx,
738-
'segmentCode' => $id
749+
'segmentCode' => $id,
739750
];
740751

741752
if ($this->segmentGroup) {
@@ -783,7 +794,9 @@ private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, ar
783794
}
784795

785796
$d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
786-
797+
//print_r($d_sub_desc_attr);
798+
//print_r($d_detail);
799+
//die();
787800
if ($this->codes !== null && isset($this->codes[$d_sub_desc_attr['id']]) && is_array($this->codes[$d_sub_desc_attr['id']])) { //if codes is set enable translation of the value
788801
if (isset($this->codes[$d_sub_desc_attr['id']][$d_detail])) {
789802
$d_detail = $this->codes[$d_sub_desc_attr['id']][$d_detail];
@@ -859,9 +872,19 @@ public function rebuildArray()
859872
if ($this->codes !== null) {
860873
throw new \LogicException('Run the Interpreter without calling setCodes()');
861874
}
875+
$unh = $this->serviceSeg['interchangeHeader'];
876+
unset($unh['segmentIdx']);
877+
unset($unh['segmentGroup']);
878+
$unz = $this->serviceSeg['interchangeTrailer'];
879+
unset($unz['segmentIdx']);
880+
unset($unz['segmentGroup']);
862881

863-
return $this->recursionReconstruct($this->ediGroups);
882+
$rebuilt = $this->recursionReconstruct($this->ediGroups);
864883

884+
array_unshift($rebuilt, $unh);
885+
$rebuilt[] = $unz;
886+
887+
return $rebuilt;
865888
}
866889

867890
private function recursionReconstruct($tempArr)
@@ -875,7 +898,7 @@ private function recursionReconstruct($tempArr)
875898
$reconstructArr[$k] = $i;
876899
}
877900
} else {
878-
$idx=$arr['segmentIdx'];
901+
$idx = $arr['segmentIdx'];
879902
unset($arr['segmentIdx']);
880903
if ($this->segmentGroup) {
881904
unset($arr['segmentGroup']);
@@ -884,6 +907,7 @@ private function recursionReconstruct($tempArr)
884907
}
885908
}
886909
}
910+
887911
return $reconstructArr;
888912
}
889913
}

src/EDI/Parser.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,13 @@ public function checkEncoding(): bool
291291
throw new \LogicException('No text has been parsed yet');
292292
}
293293
if (! isset(self::$charsets[$this->syntaxID])) {
294-
throw new \RuntimeException('Unsupported syntax identifier: ' . $this->syntaxID);
294+
throw new \RuntimeException('Unsupported syntax identifier: '.$this->syntaxID);
295295
}
296296

297297
$check = mb_check_encoding($this->parsedfile, self::$charsets[$this->syntaxID]);
298-
if(!$check)
298+
if (! $check) {
299299
$this->errors[] = 'Character encoding does not match declaration in UNB interchange header';
300+
}
300301

301302
return $check;
302303
}
@@ -312,7 +313,7 @@ public function errors(): array
312313
/**
313314
* (Un)Set strict parsing.
314315
*/
315-
public function setStrict(bool $strict):void
316+
public function setStrict(bool $strict): void
316317
{
317318
$this->strict = $strict;
318319
}
@@ -330,24 +331,24 @@ public function get(?string $encoding = null): array
330331
if (empty($this->parsedfile)) {
331332
$this->parse();
332333
}
333-
334+
334335
if (null === $encoding) {
335336
return $this->parsedfile;
336337
}
337-
338+
338339
return $this->convertEncoding($this->parsedfile, self::$charsets[$this->syntaxID], $encoding);
339340
}
340-
341+
341342
private function convertEncoding($data, string $from, string $to)
342343
{
343344
if (is_array($data)) {
344345
foreach ($data as $k => $v) {
345346
$data[$k] = $this->convertEncoding($v, $from, $to);
346347
}
347348
} elseif (is_string($data)) {
348-
$data = function_exists('iconv') ? iconv($from, $to . '//TRANSLIT', $data) : mb_convert_encoding($data, $to, $from);
349+
$data = function_exists('iconv') ? iconv($from, $to.'//TRANSLIT', $data) : mb_convert_encoding($data, $to, $from);
349350
}
350-
351+
351352
return $data;
352353
}
353354

@@ -365,7 +366,6 @@ public function getRawSegments(): array
365366
* Get syntax identifier from the UNB header.
366367
* Does not necessarily mean that the text is actually encoded as such.
367368
*
368-
* @return string
369369
* @throws \RuntimeException
370370
*/
371371
public function getSyntaxIdentifier(): string
@@ -380,7 +380,7 @@ public function getSyntaxIdentifier(): string
380380
*/
381381
public function load(string $location): self
382382
{
383-
$contents = \file_get_contents($location);
383+
$contents = file_get_contents($location);
384384
if ($contents === false) {
385385
throw new \RuntimeException('File could not be retrieved');
386386
}

src/EDI/Reader.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,6 @@ private static function unwrap($string)
561561
}
562562

563563
/**
564-
* @param array $matchingSegments
565-
* @param int $offset
566-
* @param bool $required
567-
* @param mixed $segment_name
568-
*
569564
* @return false|mixed
570565
*/
571566
private function getOffsetSegmentFromResult(array $matchingSegments, int $offset, bool $required, mixed $segment_name): mixed
@@ -575,27 +570,24 @@ private function getOffsetSegmentFromResult(array $matchingSegments, int $offset
575570
}
576571

577572
if ($required) {
578-
throw new ReaderException('Segment "' . $segment_name . '" does not exist at offset "' . $offset . '"');
573+
throw new ReaderException('Segment "'.$segment_name.'" does not exist at offset "'.$offset.'"');
579574
}
580575

581576
return false;
582577
}
583578

584579
/**
585-
* @param array $matchingSegments
586-
* @param mixed $segment_name
587-
*
588580
* @return false|mixed
589581
*/
590582
private function getSegmentFromResult(array $matchingSegments, bool $required, mixed $segment_name): mixed
591583
{
592584
// found more than one segment - error
593585
if (count($matchingSegments) > 1) {
594-
throw new ReaderException('Segment "' . $segment_name . '" is ambiguous');
586+
throw new ReaderException('Segment "'.$segment_name.'" is ambiguous');
595587
}
596588

597-
if ($required && !isset($matchingSegments[0])) {
598-
throw new ReaderException('Segment "' . $segment_name . '" no exist');
589+
if ($required && ! isset($matchingSegments[0])) {
590+
throw new ReaderException('Segment "'.$segment_name.'" no exist');
599591
}
600592

601593
return $matchingSegments[0] ?? false;

src/EDI/ReaderException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
class ReaderException extends RuntimeException
1010
{
11-
}
11+
}

0 commit comments

Comments
 (0)