Skip to content

Commit 564d2b1

Browse files
committed
[+]: add "PhpCsFixer"
-> e.g. php-cs-fixer.phar fix --config=phpcs.php_cs
1 parent 588747c commit 564d2b1

15 files changed

+1885
-866
lines changed

.editorconfig

+809-7
Large diffs are not rendered by default.

phpcs.php_cs

+238
Large diffs are not rendered by default.

src/EDI/Analyser.php

+83-87
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
*/
1111
class Analyser
1212
{
13-
1413
/**
1514
* @var array<mixed>
1615
*/
17-
public $segments;
16+
public $segments;
1817

1918
/**
2019
* @var array<mixed>
@@ -36,80 +35,14 @@ public function loadMessageXml(string $message_xml_file)
3635
$messageXml = new \SimpleXMLIterator($messageXmlString);
3736
unset($messageXmlString);
3837
$message = [
39-
"defaults" => $this->readMessageDefaults($messageXml),
40-
"segments" => $this->readXmlNodes($messageXml),
38+
'defaults' => $this->readMessageDefaults($messageXml),
39+
'segments' => $this->readXmlNodes($messageXml),
4140
];
4241
unset($messageXml);
4342

4443
return $message;
4544
}
4645

47-
/**
48-
* read default values in given message xml
49-
*
50-
* @param \SimpleXMLElement $message
51-
*
52-
* @return array
53-
*/
54-
protected function readMessageDefaults(\SimpleXMLElement $message): array
55-
{
56-
// init
57-
$defaults = [];
58-
59-
/* @var \SimpleXMLElement $defaultValueNode */
60-
foreach ($message->defaults[0] ?? [] as $defaultValueNode) {
61-
$attributes = $defaultValueNode->attributes();
62-
$id = (string)$attributes->id;
63-
$defaults[$id] = (string)$attributes->value;
64-
}
65-
66-
return $defaults;
67-
}
68-
69-
/**
70-
* read message segments and groups
71-
*
72-
* @param \SimpleXMLElement $element
73-
*
74-
* @return array
75-
*/
76-
protected function readXmlNodes(\SimpleXMLElement $element): array
77-
{
78-
$arrayElements = [];
79-
foreach ($element as $name => $node) {
80-
if ($name == "defaults") {
81-
continue;
82-
}
83-
$arrayElement = [];
84-
$arrayElement["type"] = $name;
85-
$arrayElement["attributes"] = $this->readAttributesArray($node);
86-
$details = $this->readXmlNodes($node);
87-
if (!empty($details)) {
88-
$arrayElement["details"] = $details;
89-
}
90-
$arrayElements[] = $arrayElement;
91-
}
92-
93-
return $arrayElements;
94-
}
95-
96-
/**
97-
* return an xml elements attributes in as array
98-
*
99-
* @param \SimpleXMLElement $element
100-
*
101-
* @return array
102-
*/
103-
protected function readAttributesArray(\SimpleXMLElement $element): array
104-
{
105-
$attributes = [];
106-
foreach ($element->attributes() ?? [] as $attrName => $attr) {
107-
$attributes[(string)$attrName] = (string)$attr;
108-
}
109-
110-
return $attributes;
111-
}
112-
11346
/**
11447
* get all data element codes
11548
*
@@ -135,15 +68,15 @@ public function loadCodesXml(string $codesXml)
13568
continue;
13669
}
13770

138-
$id = (string)$codeCollectionAttributes->id;
71+
$id = (string) $codeCollectionAttributes->id;
13972
$codes[$id] = [];
14073
foreach ($codeCollection as $codeNode) {
14174
\assert($codeNode instanceof \SimpleXMLIterator);
14275

14376
$codeAttributes = $codeNode->attributes();
14477
if ($codeAttributes !== null) {
145-
$code = (string)$codeAttributes->id;
146-
$codes[$id][$code] = (string)$codeAttributes->desc;
78+
$code = (string) $codeAttributes->id;
79+
$codes[$id][$code] = (string) $codeAttributes->desc;
14780
}
14881
}
14982
}
@@ -183,12 +116,12 @@ public function loadSegmentsXml(string $segment_xml_file)
183116
/* @var \SimpleXMLElement $segmentNode */
184117
$segmentNode = $segmentNode;
185118

186-
$qualifier = (string)$segmentNode->attributes()->id;
119+
$qualifier = (string) $segmentNode->attributes()->id;
187120
$segment = [];
188-
$segment["attributes"] = $this->readAttributesArray($segmentNode);
121+
$segment['attributes'] = $this->readAttributesArray($segmentNode);
189122
$details = $this->readXmlNodes($segmentNode);
190123
if (!empty($details)) {
191-
$segment["details"] = $details;
124+
$segment['details'] = $details;
192125
}
193126
$this->segments[$qualifier] = $segment;
194127
}
@@ -208,7 +141,6 @@ public function process(array $data, array $rawSegments = null): string
208141
{
209142
$r = [];
210143
foreach ($data as $nrow => $segment) {
211-
212144
$id = $segment[0];
213145

214146
$r[] = '';
@@ -218,22 +150,21 @@ public function process(array $data, array $rawSegments = null): string
218150
}
219151

220152
if (isset($this->segments[$id])) {
221-
222153
$attributes = $this->segments[$id]['attributes'];
223154
$details_desc = $this->segments[$id]['details'];
224155

225156
$r[] = $id . ' - ' . $attributes['name'];
226-
$r[] = ' (' . \wordwrap($attributes['desc'], 75, PHP_EOL . ' ') . ')';
157+
$r[] = ' (' . \wordwrap($attributes['desc'], 75, \PHP_EOL . ' ') . ')';
227158

228-
$jsonelements = ["segmentCode" => $id];
159+
$jsonelements = ['segmentCode' => $id];
229160
foreach ($segment as $idx => $detail) {
230161
$n = $idx - 1;
231162
if ($idx == 0 || !isset($details_desc[$n])) {
232163
continue;
233164
}
234165
$d_desc_attr = $details_desc[$n]['attributes'];
235166
$l1 = ' ' . $d_desc_attr['id'] . ' - ' . $d_desc_attr['name'];
236-
$l2 = ' ' . \wordwrap($d_desc_attr['desc'], 71, PHP_EOL . ' ');
167+
$l2 = ' ' . \wordwrap($d_desc_attr['desc'], 71, \PHP_EOL . ' ');
237168

238169
if (\is_array($detail)) {
239170
$r[] = ' [' . $n . '] ' . \implode(',', $detail);
@@ -248,7 +179,7 @@ public function process(array $data, array $rawSegments = null): string
248179
$d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
249180
$r[] = ' [' . $d_n . '] ' . $d_detail;
250181
$r[] = ' id: ' . $d_sub_desc_attr['id'] . ' - ' . $d_sub_desc_attr['name'];
251-
$r[] = ' ' . \wordwrap($d_sub_desc_attr['desc'], 69, PHP_EOL . ' ');
182+
$r[] = ' ' . \wordwrap($d_sub_desc_attr['desc'], 69, \PHP_EOL . ' ');
252183
$r[] = ' type: ' . $d_sub_desc_attr['type'];
253184

254185
$jsoncomposite[$d_sub_desc_attr['name']] = $d_detail;
@@ -277,8 +208,7 @@ public function process(array $data, array $rawSegments = null): string
277208
if (!empty($d_sub_desc_attr)) {
278209
var_dump($d_sub_desc_attr);
279210
}
280-
*/
281-
211+
*/
282212
}
283213
}
284214
$jsonelements[$d_desc_attr['name']] = $jsoncomposite;
@@ -292,21 +222,87 @@ public function process(array $data, array $rawSegments = null): string
292222
$jsonsegment[$attributes['name']] = $jsonelements;
293223
} else {
294224
$r[] = $id;
295-
$jsonsegment["UnrecognisedType"] = $segment;
225+
$jsonsegment['UnrecognisedType'] = $segment;
296226
}
297227
$this->jsonedi[] = $jsonsegment;
298228
}
299229

300-
return \implode(PHP_EOL, $r);
230+
return \implode(\PHP_EOL, $r);
301231
}
302232

303233
/**
304234
* return the processed EDI in json format
305235
*
306-
* @return string|false
236+
* @return false|string
307237
*/
308238
public function getJson()
309239
{
310240
return \json_encode($this->jsonedi);
311241
}
242+
243+
/**
244+
* read default values in given message xml
245+
*
246+
* @param \SimpleXMLElement $message
247+
*
248+
* @return array
249+
*/
250+
protected function readMessageDefaults(\SimpleXMLElement $message): array
251+
{
252+
// init
253+
$defaults = [];
254+
255+
/* @var \SimpleXMLElement $defaultValueNode */
256+
foreach ($message->defaults[0] ?? [] as $defaultValueNode) {
257+
$attributes = $defaultValueNode->attributes();
258+
$id = (string) $attributes->id;
259+
$defaults[$id] = (string) $attributes->value;
260+
}
261+
262+
return $defaults;
263+
}
264+
265+
/**
266+
* read message segments and groups
267+
*
268+
* @param \SimpleXMLElement $element
269+
*
270+
* @return array
271+
*/
272+
protected function readXmlNodes(\SimpleXMLElement $element): array
273+
{
274+
$arrayElements = [];
275+
foreach ($element as $name => $node) {
276+
if ($name == 'defaults') {
277+
continue;
278+
}
279+
$arrayElement = [];
280+
$arrayElement['type'] = $name;
281+
$arrayElement['attributes'] = $this->readAttributesArray($node);
282+
$details = $this->readXmlNodes($node);
283+
if (!empty($details)) {
284+
$arrayElement['details'] = $details;
285+
}
286+
$arrayElements[] = $arrayElement;
287+
}
288+
289+
return $arrayElements;
290+
}
291+
292+
/**
293+
* return an xml elements attributes in as array
294+
*
295+
* @param \SimpleXMLElement $element
296+
*
297+
* @return array
298+
*/
299+
protected function readAttributesArray(\SimpleXMLElement $element): array
300+
{
301+
$attributes = [];
302+
foreach ($element->attributes() ?? [] as $attrName => $attr) {
303+
$attributes[(string) $attrName] = (string) $attr;
304+
}
305+
306+
return $attributes;
307+
}
312308
}

src/EDI/Encoder.php

+30-28
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Encoder
6363
/**
6464
* Encoder constructor.
6565
*
66-
* @param null|array $array
66+
* @param array|null $array
6767
* @param bool $wrap
6868
*/
6969
public function __construct($array = null, $wrap = true)
@@ -93,7 +93,7 @@ public function encode(array $array, $wrap = true, $filterKeys = false): string
9393
$count = \count($array);
9494
$k = 0;
9595
foreach ($array as $row) {
96-
$k++;
96+
++$k;
9797
if ($filterKeys) {
9898
unset($row['segmentIdx']);
9999
}
@@ -152,36 +152,13 @@ public function encodeSegment(array $row): string
152152
return $str;
153153
}
154154

155-
/**
156-
* @param string|int $str
157-
*
158-
* @return string
159-
*/
160-
private function escapeValue(&$str): string
161-
{
162-
$search = [
163-
$this->symbRel,
164-
$this->sepComp,
165-
$this->sepData,
166-
$this->symbEnd,
167-
];
168-
$replace = [
169-
$this->symbRel . $this->symbRel,
170-
$this->symbRel . $this->sepComp,
171-
$this->symbRel . $this->sepData,
172-
$this->symbRel . $this->symbEnd,
173-
];
174-
175-
return \str_replace($search, $replace, (string)$str);
176-
}
177-
178155
/**
179156
* @return string
180157
*/
181158
public function get(): string
182159
{
183160
if ($this->UNAActive) {
184-
$una = "UNA" . $this->sepComp .
161+
$una = 'UNA' . $this->sepComp .
185162
$this->sepData .
186163
$this->sepDec .
187164
$this->symbRel .
@@ -205,8 +182,10 @@ public function get(): string
205182
*/
206183
public function setUNA(string $chars, bool $user_call = true): bool
207184
{
208-
if (\is_string($chars)
209-
&& \strlen($chars) == 6
185+
if (
186+
\is_string($chars)
187+
&&
188+
\strlen($chars) == 6
210189
) {
211190
$this->sepComp = $chars[0];
212191
$this->sepData = $chars[1];
@@ -244,4 +223,27 @@ public function disableUNA()
244223
{
245224
$this->UNAActive = false;
246225
}
226+
227+
/**
228+
* @param int|string $str
229+
*
230+
* @return string
231+
*/
232+
private function escapeValue(&$str): string
233+
{
234+
$search = [
235+
$this->symbRel,
236+
$this->sepComp,
237+
$this->sepData,
238+
$this->symbEnd,
239+
];
240+
$replace = [
241+
$this->symbRel . $this->symbRel,
242+
$this->symbRel . $this->sepComp,
243+
$this->symbRel . $this->sepData,
244+
$this->symbRel . $this->symbEnd,
245+
];
246+
247+
return \str_replace($search, $replace, (string) $str);
248+
}
247249
}

0 commit comments

Comments
 (0)