Skip to content

Commit 820a083

Browse files
authored
Merge pull request #88 from voku/master
update to php7+
2 parents a1b539c + a1257b9 commit 820a083

16 files changed

+2243
-646
lines changed

composer.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@
2929
"url": "https://github.com/php-edifact/edifact"
3030
}
3131
],
32+
"require": {
33+
"php": ">=7.0.0",
34+
"ext-simplexml": "*",
35+
"ext-json": "*"
36+
},
3237
"require-dev": {
33-
"phpunit/phpunit": "4.1.*",
38+
"phpunit/phpunit": "~6.0",
3439
"php-edifact/edifact-mapping": "dev-master"
3540
},
3641
"autoload": {

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit bootstrap="tests/Bootstrap.php" colors="true">
3+
<phpunit bootstrap="tests/bootstrap.php" colors="true">
44
<testsuites>
55
<testsuite name="sabas/edifact">
66
<directory>./tests/EDITest</directory>

src/EDI/Analyser.php

+89-75
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,68 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EDI;
6+
27
/**
38
* EDIFACT Messages Parser
49
* (c)2016 Uldis Nelsons
510
*/
6-
7-
namespace EDI;
8-
911
class Analyser
1012
{
1113

12-
public $segments;
14+
public $segments;
1315
private $jsonedi;
1416

1517
/**
1618
* @param string $message_xml_file
19+
*
1720
* @return array
1821
*/
19-
public function loadMessageXml($message_xml_file)
22+
public function loadMessageXml($message_xml_file): array
2023
{
21-
$messageXmlString = file_get_contents($message_xml_file);
24+
$messageXmlString = \file_get_contents($message_xml_file);
2225
$messageXml = new \SimpleXMLIterator($messageXmlString);
2326
unset($messageXmlString);
2427
$message = [
25-
"defaults" => $this->readMessageDefaults($messageXml),
26-
"segments" => $this->readXmlNodes($messageXml),
28+
"defaults" => $this->readMessageDefaults($messageXml),
29+
"segments" => $this->readXmlNodes($messageXml),
2730
];
2831
unset($messageXml);
32+
2933
return $message;
3034
}
3135

3236
/**
3337
* read default values in given message xml
34-
* @param \SimpleXmlElement $message
38+
*
39+
* @param \SimpleXMLElement $message
40+
*
3541
* @return array
3642
*/
37-
protected function readMessageDefaults(\SimpleXmlElement $message)
43+
protected function readMessageDefaults(\SimpleXMLElement $message): array
3844
{
39-
$defaults = array();
40-
$defaultsNode = isset($message->defaults[0]) ? $message->defaults[0] : array();
41-
/**
42-
* @var \SimpleXMLElement $defaultValueNode
43-
*/
44-
foreach ($defaultsNode as $defaultValueNode) {
45+
// init
46+
$defaults = [];
47+
48+
/* @var \SimpleXMLElement $defaultValueNode */
49+
foreach ($message->defaults[0] ?? [] as $defaultValueNode) {
4550
$attributes = $defaultValueNode->attributes();
46-
$id = (string) $attributes->id;
47-
$value = (string) $attributes->value;
48-
$defaults[$id] = $value;
51+
$id = (string)$attributes->id;
52+
$defaults[$id] = (string)$attributes->value;
4953
}
54+
5055
return $defaults;
5156
}
5257

5358
/**
5459
* read message segments and groups
55-
* @param \SimpleXmlElement $element
60+
*
61+
* @param \SimpleXMLElement $element
62+
*
5663
* @return array
5764
*/
58-
protected function readXmlNodes(\SimpleXmlElement $element)
65+
protected function readXmlNodes(\SimpleXMLElement $element): array
5966
{
6067
$arrayElements = [];
6168
foreach ($element as $name => $node) {
@@ -71,73 +78,72 @@ protected function readXmlNodes(\SimpleXmlElement $element)
7178
}
7279
$arrayElements[] = $arrayElement;
7380
}
81+
7482
return $arrayElements;
7583
}
7684

7785
/**
7886
* return an xml elements attributes in as array
79-
* @param \SimpleXmlElement $element
87+
*
88+
* @param \SimpleXMLElement $element
89+
*
8090
* @return array
8191
*/
82-
protected function readAttributesArray(\SimpleXmlElement $element)
92+
protected function readAttributesArray(\SimpleXMLElement $element): array
8393
{
8494
$attributes = [];
8595
foreach ($element->attributes() as $attrName => $attr) {
86-
$attributes[$attrName] = (string) $attr;
96+
$attributes[(string)$attrName] = (string)$attr;
8797
}
98+
8899
return $attributes;
89100
}
90101

91-
92102
/**
93103
* get all data element codes
104+
*
94105
* @param string $codesXml
106+
*
95107
* @return array
96108
*/
97-
public function loadCodesXml($codesXml)
109+
public function loadCodesXml($codesXml): array
98110
{
99-
$codesXmlString = file_get_contents($codesXml);
111+
$codesXmlString = \file_get_contents($codesXml);
100112
$codesXml = new \SimpleXMLIterator($codesXmlString);
101113
unset($codesXmlString);
102114
$codes = [];
103-
/**
104-
* @var \SimpleXmlIterator $codeCollection
105-
*/
115+
/* @var \SimpleXmlIterator $codeCollection */
106116
foreach ($codesXml as $codeCollection) {
107117
$id = (string)$codeCollection->attributes()->id;
108118
$codes[$id] = [];
109-
/**
110-
* @var \SimpleXmlIterator $codeNode
111-
*/
119+
/* @var \SimpleXmlIterator $codeNode */
112120
foreach ($codeCollection as $codeNode) {
113121
$codeAttributes = $codeNode->attributes();
114122
$code = (string)$codeAttributes->id;
115-
$desc = (string)$codeAttributes->desc;
116-
$codes[$id][$code] = $desc;
123+
$codes[$id][$code] = (string)$codeAttributes->desc;
117124
}
118125
}
119-
unset($codesXml);
126+
120127
return $codes;
121128
}
122129

123130
/**
124131
* convert segment definition from XML to array. Sequence of data_elements and
125132
* composite_data_element same as in XML
133+
*
126134
* @param string $segment_xml_file
135+
*
127136
* @return array
128137
*/
129-
public function loadSegmentsXml($segment_xml_file)
138+
public function loadSegmentsXml($segment_xml_file): array
130139
{
140+
$segments_xml = \file_get_contents($segment_xml_file);
131141

132-
$segments_xml = file_get_contents($segment_xml_file);
133-
134-
$xml = simplexml_load_string($segments_xml);
142+
$xml = \simplexml_load_string($segments_xml);
135143
unset($segments_xml);
136-
$this->segments = array();
144+
$this->segments = [];
137145

138-
/**
139-
* @var \SimpleXmlElement $segmentNode
140-
*/
146+
/* @var \SimpleXMLElement $segmentNode */
141147
foreach ($xml as $segmentNode) {
142148
$qualifier = (string)$segmentNode->attributes()->id;
143149
$segment = [];
@@ -148,26 +154,29 @@ public function loadSegmentsXml($segment_xml_file)
148154
}
149155
$this->segments[$qualifier] = $segment;
150156
}
157+
151158
return $this->segments;
152159
}
153160

154161
/**
155162
* create readable EDI MESSAGE with comments
156-
* @param array $data by EDI\Parser:parse() created array from plain EDI message
163+
*
164+
* @param array $data by EDI\Parser:parse() created array from plain EDI message
157165
* @param array $rawSegments (optional) List of raw segments from EDI\Parser::getRawSegments
166+
*
158167
* @return string file
159168
*/
160-
public function process($data, $rawSegments = null)
169+
public function process($data, $rawSegments = null): string
161170
{
162-
$r = array();
171+
$r = [];
163172
foreach ($data as $nrow => $segment) {
164173

165174
$id = $segment[0];
166175

167176
$r[] = '';
168177
$jsonsegment = [];
169-
if (isset($rawSegments, $rawSegments[$nrow])) {
170-
$r[] = trim($rawSegments[$nrow]);
178+
if (isset($rawSegments[$nrow])) {
179+
$r[] = \trim($rawSegments[$nrow]);
171180
}
172181

173182
if (isset($this->segments[$id])) {
@@ -176,37 +185,32 @@ public function process($data, $rawSegments = null)
176185
$details_desc = $this->segments[$id]['details'];
177186

178187
$r[] = $id . ' - ' . $attributes['name'];
179-
$r[] = ' (' . wordwrap($attributes['desc'], 75, PHP_EOL.' ') . ')';
188+
$r[] = ' (' . \wordwrap($attributes['desc'], 75, PHP_EOL . ' ') . ')';
180189

181190
$jsonelements = ["segmentCode" => $id];
182191
foreach ($segment as $idx => $detail) {
183-
$n = $idx-1;
192+
$n = $idx - 1;
184193
if ($idx == 0 || !isset($details_desc[$n])) {
185194
continue;
186195
}
187-
$d_desc_attr = $details_desc[$n]['attributes'];
196+
$d_desc_attr = $details_desc[$n]['attributes'];
188197
$l1 = ' ' . $d_desc_attr['id'] . ' - ' . $d_desc_attr['name'];
189-
$l2 = ' ' . wordwrap($d_desc_attr['desc'], 71, PHP_EOL.' ');
198+
$l2 = ' ' . \wordwrap($d_desc_attr['desc'], 71, PHP_EOL . ' ');
190199

191-
if (!is_array($detail)) {
192-
$r[] = ' [' . $n . '] ' . $detail;
193-
$r[] = $l1;
194-
$r[] = $l2;
195-
$jsonelements[$d_desc_attr['name']] = $detail;
196-
} else {
197-
$r[] = ' [' . $n . '] ' . implode(',', $detail);
200+
if (\is_array($detail)) {
201+
$r[] = ' [' . $n . '] ' . \implode(',', $detail);
198202
$r[] = $l1;
199203
$r[] = $l2;
200204

201205
$jsoncomposite = [];
202206
if (isset($details_desc[$n]['details'])) {
203-
$sub_details_desc = $details_desc[$n]['details'];
207+
$sub_details_desc = $details_desc[$n]['details'];
204208

205209
foreach ($detail as $d_n => $d_detail) {
206-
$d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
210+
$d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
207211
$r[] = ' [' . $d_n . '] ' . $d_detail;
208212
$r[] = ' id: ' . $d_sub_desc_attr['id'] . ' - ' . $d_sub_desc_attr['name'];
209-
$r[] = ' ' . wordwrap($d_sub_desc_attr['desc'], 69, PHP_EOL.' ');
213+
$r[] = ' ' . \wordwrap($d_sub_desc_attr['desc'], 69, PHP_EOL . ' ');
210214
$r[] = ' type: ' . $d_sub_desc_attr['type'];
211215

212216
$jsoncomposite[$d_sub_desc_attr['name']] = $d_detail;
@@ -221,21 +225,30 @@ public function process($data, $rawSegments = null)
221225
}
222226

223227
//check for skipped data
224-
unset($d_sub_desc_attr['id']);
225-
unset($d_sub_desc_attr['name']);
226-
unset($d_sub_desc_attr['desc']);
227-
unset($d_sub_desc_attr['type']);
228-
unset($d_sub_desc_attr['maxlength']);
229-
unset($d_sub_desc_attr['required']);
230-
unset($d_sub_desc_attr['length']);
228+
unset(
229+
$d_sub_desc_attr['id'],
230+
$d_sub_desc_attr['name'],
231+
$d_sub_desc_attr['desc'],
232+
$d_sub_desc_attr['type'],
233+
$d_sub_desc_attr['maxlength'],
234+
$d_sub_desc_attr['required'],
235+
$d_sub_desc_attr['length']
236+
);
237+
238+
/*
231239
if (!empty($d_sub_desc_attr)) {
232-
var_dump($d_sub_desc_attr);
240+
var_dump($d_sub_desc_attr);
233241
}
242+
*/
234243

235244
}
236245
}
237246
$jsonelements[$d_desc_attr['name']] = $jsoncomposite;
238-
//exit;
247+
} else {
248+
$r[] = ' [' . $n . '] ' . $detail;
249+
$r[] = $l1;
250+
$r[] = $l2;
251+
$jsonelements[$d_desc_attr['name']] = $detail;
239252
}
240253
}
241254
$jsonsegment[$attributes['name']] = $jsonelements;
@@ -246,15 +259,16 @@ public function process($data, $rawSegments = null)
246259
$this->jsonedi[] = $jsonsegment;
247260
}
248261

249-
return implode(PHP_EOL, $r);
262+
return \implode(PHP_EOL, $r);
250263
}
251264

252265
/**
253266
* return the processed EDI in json format
267+
*
254268
* @return string json
255269
*/
256-
public function getJson()
270+
public function getJson(): string
257271
{
258-
return json_encode($this->jsonedi);
272+
return \json_encode($this->jsonedi);
259273
}
260274
}

0 commit comments

Comments
 (0)