Skip to content

Commit

Permalink
Cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
sabas committed Oct 25, 2023
1 parent 29f4c57 commit 2949865
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 512 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
"name": "Stefano Sabatini",
"email": "sabas88@gmail.com",
"homepage": "http://stefanosabatini.eu",
"homepage": "http://stefanosabatini.com",
"role": "Developer"
},
{
Expand All @@ -34,7 +34,7 @@
"require-dev": {
"php-edifact/edifact-mapping": "dev-master",
"phpunit/phpunit": "~9.0",
"voku/arrayy": "~7.1"
"symplify/easy-coding-standard": "^12.0"
},
"autoload": {
"psr-4": {
Expand Down
32 changes: 32 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// this way you add a single rule
$ecsConfig->rules([
NoUnusedImportsFixer::class,
]);

// this way you can add sets - group of rules
$ecsConfig->sets([
// run and fix, one by one
SetList::SPACES,
SetList::ARRAY,
// SetList::DOCBLOCK,
// SetList::NAMESPACES,
// SetList::COMMENTS,
SetList::PSR_12,
SetList::LARAVEL,
SetList::CLEAN_CODE,
]);
};
238 changes: 0 additions & 238 deletions phpcs.php_cs

This file was deleted.

6 changes: 0 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,4 @@
<directory>tests</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
89 changes: 38 additions & 51 deletions src/EDI/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ class Analyser
* @var array<mixed>
*/
private $jsonedi;

/**
* @var array
*/
private $codes;

public function setXml($segments, $codes) {
public function setXml($segments, $codes)
{
$this->segments = $segments;
$this->codes = $codes;
}

/**
* @param string $message_xml_file
*
* @return array|false
*/
public function loadMessageXml(string $message_xml_file)
Expand All @@ -57,7 +57,6 @@ public function loadMessageXml(string $message_xml_file)
/**
* get all data element codes
*
* @param string $codesXml
*
* @return array|false
*/
Expand Down Expand Up @@ -99,8 +98,6 @@ public function loadCodesXml(string $codesXml)
* composite_data_element same as in XML
* For single message, it's vailable also in (new EDI\Mapping\MappingProvider($version))->loadSegmentsXml()
*
* @param string $segmentXmlFile
* @param bool $discardOldSegments
*
* @return array|false
*/
Expand Down Expand Up @@ -134,7 +131,7 @@ public function loadSegmentsXml(string $segmentXmlFile, bool $discardOldSegments
$segment = [];
$segment['attributes'] = $this->readAttributesArray($segmentNode);
$details = $this->readXmlNodes($segmentNode);
if (!empty($details)) {
if (! empty($details)) {
$segment['details'] = $details;
}
$this->segments[$qualifier] = $segment;
Expand All @@ -147,13 +144,14 @@ public function loadSegmentsXml(string $segmentXmlFile, bool $discardOldSegments
* Load segment definitions from multiple files
*
* @see Analyser::loadSegmentsXml()
*
* @param string[] $segmentXmlFiles
* @return array|false
*/
public function loadMultiSegmentsXml(array $segmentXmlFiles)
{
foreach ($segmentXmlFiles as $xmlFile) {
if (!$result = $this->loadSegmentsXml($xmlFile, false)) {
if (! $result = $this->loadSegmentsXml($xmlFile, false)) {
return $result;
}
}
Expand All @@ -166,7 +164,6 @@ public function loadMultiSegmentsXml(array $segmentXmlFiles)
*
* @param array $data by EDI\Parser:parse() created array from plain EDI message
* @param array|null $rawSegments (optional) List of raw segments from EDI\Parser::getRawSegments
*
* @return string file
*/
public function process(array $data, array $rawSegments = null): string
Expand All @@ -185,25 +182,27 @@ public function process(array $data, array $rawSegments = null): string
$attributes = $this->segments[$id]['attributes'];
$details_desc = $this->segments[$id]['details'];

$idHeader = $id . ' - ' . $attributes['name'];
if($this->directory && $id !== 'UNB') {
$idHeader .= ' https://service.unece.org/trade/untdid/' . strtolower($this->directory) . '/trsd/trsd' . strtolower($id) . '.htm';
$idHeader = $id.' - '.$attributes['name'];
if ($this->directory && $id !== 'UNB') {
$idHeader .= ' https://service.unece.org/trade/untdid/'.strtolower($this->directory).'/trsd/trsd'.strtolower($id).'.htm';
}
$r[] = $idHeader;
$r[] = ' (' . \wordwrap($attributes['desc'], 75, \PHP_EOL . ' ') . ')';
$r[] = ' ('.\wordwrap($attributes['desc'], 75, \PHP_EOL.' ').')';

$jsonelements = ['segmentCode' => $id];
$jsonelements = [
'segmentCode' => $id,
];
foreach ($segment as $idx => $detail) {
$n = $idx - 1;
if ($idx == 0 || !isset($details_desc[$n])) {
if ($idx == 0 || ! isset($details_desc[$n])) {
continue;
}
$d_desc_attr = $details_desc[$n]['attributes'];
$l1 = ' ' . $d_desc_attr['id'] . ' - ' . $d_desc_attr['name'];
$l2 = ' ' . \wordwrap($d_desc_attr['desc'], 71, \PHP_EOL . ' ');
$l1 = ' '.$d_desc_attr['id'].' - '.$d_desc_attr['name'];
$l2 = ' '.\wordwrap($d_desc_attr['desc'], 71, \PHP_EOL.' ');

if (\is_array($detail)) {
$r[] = ' [' . $n . '] ' . \implode(',', $detail);
$r[] = ' ['.$n.'] '.\implode(',', $detail);
$r[] = $l1;
$r[] = $l2;

Expand All @@ -214,25 +213,25 @@ public function process(array $data, array $rawSegments = null): string
foreach ($detail as $d_n => $d_detail) {
$d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
$codeElementId = $d_sub_desc_attr['id'];
$line = ' [' . $d_n . '] ' . $d_detail;
if(isset($this->codes[(int)$codeElementId][$d_detail])){
$line .= ' - ' . \wordwrap($this->codes[$codeElementId][$d_detail], 69, \PHP_EOL . ' ');
$line = ' ['.$d_n.'] '.$d_detail;
if (isset($this->codes[(int) $codeElementId][$d_detail])) {
$line .= ' - '.\wordwrap($this->codes[$codeElementId][$d_detail], 69, \PHP_EOL.' ');
}
$r[] = $line;

$r[] = ' id: ' . $codeElementId . ' - ' . $d_sub_desc_attr['name'];
$r[] = ' ' . \wordwrap($d_sub_desc_attr['desc'], 69, \PHP_EOL . ' ');
$r[] = ' type: ' . $d_sub_desc_attr['type'];
$r[] = ' id: '.$codeElementId.' - '.$d_sub_desc_attr['name'];
$r[] = ' '.\wordwrap($d_sub_desc_attr['desc'], 69, \PHP_EOL.' ');
$r[] = ' type: '.$d_sub_desc_attr['type'];

$jsoncomposite[$d_sub_desc_attr['name']] = $d_detail;
if (isset($d_sub_desc_attr['maxlength'])) {
$r[] = ' maxlen: ' . $d_sub_desc_attr['maxlength'];
$r[] = ' maxlen: '.$d_sub_desc_attr['maxlength'];
}
if (isset($d_sub_desc_attr['required'])) {
$r[] = ' required: ' . $d_sub_desc_attr['required'];
$r[] = ' required: '.$d_sub_desc_attr['required'];
}
if (isset($d_sub_desc_attr['length'])) {
$r[] = ' length: ' . $d_sub_desc_attr['length'];
$r[] = ' length: '.$d_sub_desc_attr['length'];
}

// check for skipped data
Expand All @@ -256,16 +255,16 @@ public function process(array $data, array $rawSegments = null): string
$jsonelements[$d_desc_attr['name']] = $jsoncomposite;
} else {
$codeElementId = $d_desc_attr['id'];
$line = ' [' . $n . '] ' . $detail;
if(isset($this->codes[(int)$codeElementId][$detail])){
/*
* for retrieving code element description when first element of the segment
* is a data element and not a composite one. Ex: NAD segment.
* We rewrite also l1 line for adding 'id:' prefix before data element id.
* It's just a cosmetic fix
*/
$line .= ' - ' . \wordwrap($this->codes[$codeElementId][$detail], 71, \PHP_EOL . ' ');
$l1 = ' id: ' . $d_desc_attr['id'] . ' - ' . $d_desc_attr['name'];
$line = ' ['.$n.'] '.$detail;
if (isset($this->codes[(int) $codeElementId][$detail])) {
/*
* for retrieving code element description when first element of the segment
* is a data element and not a composite one. Ex: NAD segment.
* We rewrite also l1 line for adding 'id:' prefix before data element id.
* It's just a cosmetic fix
*/
$line .= ' - '.\wordwrap($this->codes[$codeElementId][$detail], 71, \PHP_EOL.' ');
$l1 = ' id: '.$d_desc_attr['id'].' - '.$d_desc_attr['name'];
}
$r[] = $line;
$r[] = $l1;
Expand Down Expand Up @@ -296,17 +295,13 @@ public function getJson()

/**
* read default values in given message xml
*
* @param \SimpleXMLElement $message
*
* @return array
*/
protected function readMessageDefaults(\SimpleXMLElement $message): array
{
// init
$defaults = [];

/* @var \SimpleXMLElement $defaultValueNode */
/** @var \SimpleXMLElement $defaultValueNode */
foreach ($message->defaults[0] ?? [] as $defaultValueNode) {
$attributes = $defaultValueNode->attributes();
$id = (string) $attributes->id;
Expand All @@ -318,10 +313,6 @@ protected function readMessageDefaults(\SimpleXMLElement $message): array

/**
* read message segments and groups
*
* @param \SimpleXMLElement $element
*
* @return array
*/
protected function readXmlNodes(\SimpleXMLElement $element): array
{
Expand All @@ -334,7 +325,7 @@ protected function readXmlNodes(\SimpleXMLElement $element): array
$arrayElement['type'] = $name;
$arrayElement['attributes'] = $this->readAttributesArray($node);
$details = $this->readXmlNodes($node);
if (!empty($details)) {
if (! empty($details)) {
$arrayElement['details'] = $details;
}
$arrayElements[] = $arrayElement;
Expand All @@ -345,10 +336,6 @@ protected function readXmlNodes(\SimpleXMLElement $element): array

/**
* return an xml elements attributes in as array
*
* @param \SimpleXMLElement $element
*
* @return array
*/
protected function readAttributesArray(\SimpleXMLElement $element): array
{
Expand Down
42 changes: 12 additions & 30 deletions src/EDI/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public function __construct($array = null, $compact = true)
* @param array[] $array
* @param bool $compact All segments on a single line?
* @param bool $filterKeys
*
* @return string
*/
public function encode(array $array, $compact = true, $filterKeys = false): string
{
Expand All @@ -93,13 +91,13 @@ public function encode(array $array, $compact = true, $filterKeys = false): stri
$count = \count($array);
$k = 0;
foreach ($array as $row) {
++$k;
$k++;
if ($filterKeys) {
unset($row['segmentIdx']);
}
$row = \array_values($row);
$edistring .= $this->encodeSegment($row);
if (!$compact && $k < $count) {
if (! $compact && $k < $count) {
$edistring .= "\n";
}
}
Expand All @@ -108,11 +106,6 @@ public function encode(array $array, $compact = true, $filterKeys = false): stri
return $edistring;
}

/**
* @param array $row
*
* @return string
*/
public function encodeSegment(array $row): string
{
// init
Expand Down Expand Up @@ -155,34 +148,25 @@ public function encodeSegment(array $row): string
return $str;
}

/**
* @return string
*/
public function get(): string
{
if ($this->UNAActive) {
$una = 'UNA' . $this->sepComp .
$this->sepData .
$this->sepDec .
$this->symbRel .
$this->symbRep .
$una = 'UNA'.$this->sepComp.
$this->sepData.
$this->sepDec.
$this->symbRel.
$this->symbRep.
$this->symbEnd;
if ($this->compact === false) {
$una .= "\n";
}

return $una . $this->output;
return $una.$this->output;
}

return $this->output;
}

/**
* @param string $chars
* @param bool $user_call
*
* @return bool
*/
public function setUNA(string $chars, bool $user_call = true): bool
{
if (\strlen($chars) == 6) {
Expand Down Expand Up @@ -225,8 +209,6 @@ public function disableUNA()

/**
* @param int|string $str
*
* @return string
*/
private function escapeValue(&$str): string
{
Expand All @@ -237,10 +219,10 @@ private function escapeValue(&$str): string
$this->symbEnd,
];
$replace = [
$this->symbRel . $this->symbRel,
$this->symbRel . $this->sepComp,
$this->symbRel . $this->sepData,
$this->symbRel . $this->symbEnd,
$this->symbRel.$this->symbRel,
$this->symbRel.$this->sepComp,
$this->symbRel.$this->sepData,
$this->symbRel.$this->symbEnd,
];

return \str_replace($search, $replace, (string) $str);
Expand Down
Loading

0 comments on commit 2949865

Please sign in to comment.