-
Notifications
You must be signed in to change notification settings - Fork 88
/
Encoder.php
226 lines (190 loc) · 4.97 KB
/
Encoder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
declare(strict_types=1);
namespace EDI;
/**
* EDIFACT Messages Encoder
* (c) 2018 Stefano Sabatini
*/
class Encoder
{
/**
* @var string
*/
private $output = '';
/**
* @var bool
*/
private $UNAActive = false; // disabled by default to preserve backward compatibility
/**
* @var array
*/
private $originalArray = [];
/**
* @var bool When false adds a new line after each segment
*/
private $compact = true;
/**
* @var string Component separator character (default :)
*/
private $sepComp;
/**
* @var string Data separator character (default +)
*/
private $sepData;
/**
* @var string Dec separator character (no use but here) (default .)
*/
private $sepDec;
/**
* @var string Release character (default ?)
*/
private $symbRel;
/**
* @var string Repetition character (no use but here) (default space)
*/
private $symbRep;
/**
* @var string End character (default ')
*/
private $symbEnd;
/**
* Encoder constructor.
*
* @param array|null $array
* @param bool $compact
*/
public function __construct($array = null, $compact = true)
{
$this->setUNA(":+.? '", false);
if ($array === null) {
return;
}
/** @noinspection UnusedFunctionResultInspection */
$this->encode($array, $compact);
}
/**
* @param array[] $array
* @param bool $compact All segments on a single line?
*/
public function encode(array $array, $compact = true): string
{
$this->originalArray = $array;
$this->compact = $compact;
$edistring = '';
$count = \count($array);
$k = 0;
foreach ($array as $row) {
$k++;
$row = \array_values($row);
$edistring .= $this->encodeSegment($row);
if (! $compact && $k < $count) {
$edistring .= "\n";
}
}
$this->output = $edistring;
return $edistring;
}
public function encodeSegment(array $row): string
{
// init
$str = '';
$t = \count($row);
/** @noinspection AlterInForeachInspection */
foreach ($row as $i => &$iValue) {
if (\is_array($iValue)) {
if (
\count($iValue) === 1
&&
\is_array(\reset($iValue))
) {
$iValue = \array_pop($iValue);
}
/** @noinspection NotOptimalIfConditionsInspection */
if (\is_array($iValue)) {
foreach ($iValue as &$temp) {
$temp = $this->escapeValue($temp);
}
unset($temp);
}
$elm = \implode($this->sepComp, $iValue);
} else {
$elm = $this->escapeValue($iValue);
}
$str .= $elm;
if ($i == $t - 1) {
break;
}
$str .= $this->sepData;
}
$str .= $this->symbEnd;
return $str;
}
public function get(): string
{
if ($this->UNAActive) {
$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 $this->output;
}
public function setUNA(string $chars, bool $user_call = true): bool
{
if (\strlen($chars) == 6) {
$this->sepComp = $chars[0];
$this->sepData = $chars[1];
$this->sepDec = $chars[2];
$this->symbRel = $chars[3];
$this->symbRep = $chars[4];
$this->symbEnd = $chars[5];
if ($user_call) {
$this->enableUNA();
}
if ($this->output != '') {
$this->output = $this->encode($this->originalArray);
}
return true;
}
return false;
}
/**
* @return void
*/
public function enableUNA()
{
$this->UNAActive = true;
}
/**
* @return void
*/
public function disableUNA()
{
$this->UNAActive = false;
}
/**
* @param int|string $str
*/
private function escapeValue(&$str): string
{
$search = [
$this->symbRel,
$this->sepComp,
$this->sepData,
$this->symbEnd,
];
$replace = [
$this->symbRel.$this->symbRel,
$this->symbRel.$this->sepComp,
$this->symbRel.$this->sepData,
$this->symbRel.$this->symbEnd,
];
return \str_replace($search, $replace, (string) $str);
}
}