1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Druc\XdtParser; |
4
|
|
|
|
5
|
|
|
class XdtParser |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Array to hold mappings to different xdt keys |
9
|
|
|
* Eg: ['first_name' => '2031', 'last_name' => '2031']; |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private $fieldsMap; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Holds the content unparsed rows |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $xdtRows = []; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
private $parsedRows = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $content |
25
|
|
|
* @param array $fieldsMap |
26
|
|
|
* @return XdtParser |
27
|
|
|
*/ |
28
|
45 |
|
public static function make(string $content, array $fieldsMap = []) |
29
|
|
|
{ |
30
|
45 |
|
return new static($content, $fieldsMap); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* XdtParser constructor. |
35
|
|
|
* @param string $content |
36
|
|
|
* @param array $fieldsMap |
37
|
|
|
*/ |
38
|
45 |
|
private function __construct(string $content, array $fieldsMap = []) |
39
|
|
|
{ |
40
|
45 |
|
$this->fieldsMap = $fieldsMap; |
41
|
45 |
|
$this->xdtRows = explode(PHP_EOL, $content); |
42
|
45 |
|
$this->parseXdtRows(); |
43
|
45 |
|
} |
44
|
|
|
|
45
|
45 |
|
private function parseXdtRows() |
46
|
|
|
{ |
47
|
45 |
|
foreach ($this->xdtRows as $row) { |
48
|
45 |
|
if ($row === '') { |
49
|
3 |
|
continue; |
50
|
|
|
} |
51
|
45 |
|
$this->parsedRows[] = $this->parseSingle($row); |
52
|
|
|
} |
53
|
45 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $string |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
45 |
|
public function parseSingle(string $string) |
60
|
|
|
{ |
61
|
45 |
|
$matched = preg_match('/^\\r?\\n?(\\d{3})(\\d{4})(.*?)\\r?\\n?$/', $string, $matches); |
62
|
|
|
|
63
|
45 |
|
if (!$matched) { |
64
|
3 |
|
throw new CorruptedXdt; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return [ |
68
|
45 |
|
'length' => $matches[1] ? intval($matches[1]) : null, |
69
|
45 |
|
'key' => $matches[2] ?? null, |
70
|
45 |
|
'value' => $matches[3] ?? null |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $field |
76
|
|
|
* @return null |
77
|
|
|
*/ |
78
|
9 |
|
public function first(string $field) |
79
|
|
|
{ |
80
|
9 |
View Code Duplication |
foreach ($this->parsedRows as $row) { |
|
|
|
|
81
|
9 |
|
if ($row['key'] === $this->getKey($field)) { |
82
|
9 |
|
return $row['value']; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $field |
91
|
|
|
* @return array|mixed|null |
92
|
|
|
*/ |
93
|
21 |
|
public function find(string $field) |
94
|
|
|
{ |
95
|
21 |
|
$result = []; |
96
|
|
|
|
97
|
21 |
View Code Duplication |
foreach ($this->parsedRows as $row) { |
|
|
|
|
98
|
21 |
|
if ($row['key'] === $this->getKey($field)) { |
99
|
21 |
|
$result[] = $row['value']; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
21 |
|
switch (count($result)) { |
104
|
21 |
|
case 0: |
105
|
3 |
|
return null; |
106
|
21 |
|
case 1: |
107
|
12 |
|
return $result[0]; |
108
|
|
|
default: |
109
|
21 |
|
return $result; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $field |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
33 |
|
public function getKey(string $field) |
118
|
|
|
{ |
119
|
33 |
|
return $this->fieldsMap[$field] ?? $field; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $key |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
3 |
|
public function getFieldName(string $key) |
127
|
|
|
{ |
128
|
3 |
|
foreach ($this->fieldsMap as $field => $k) { |
129
|
3 |
|
if ($k === $key) { |
130
|
3 |
|
return $field; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
return $key; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
12 |
|
public function getMapped() |
140
|
|
|
{ |
141
|
12 |
|
$result = []; |
142
|
|
|
|
143
|
12 |
|
foreach ($this->fieldsMap as $field => $key) { |
144
|
12 |
|
$result[$field] = $this->find($field); |
145
|
|
|
} |
146
|
|
|
|
147
|
12 |
|
return $result; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
3 |
|
public function all() |
154
|
|
|
{ |
155
|
3 |
|
$result = []; |
156
|
|
|
|
157
|
3 |
|
foreach ($this->parsedRows as $row) { |
158
|
3 |
|
$field = array_search($row['key'], $this->fieldsMap) ?: $row['key']; |
159
|
3 |
|
$result[$field] = $this->find($field); |
160
|
|
|
} |
161
|
|
|
|
162
|
3 |
|
return $result; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param array $fieldsMap |
167
|
|
|
* @return XdtParser |
168
|
|
|
*/ |
169
|
3 |
|
public function setFieldsMap(array $fieldsMap) |
170
|
|
|
{ |
171
|
3 |
|
$this->fieldsMap = $fieldsMap; |
172
|
3 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public function getFieldsMap() |
179
|
|
|
{ |
180
|
|
|
return $this->fieldsMap; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param array $fields |
185
|
|
|
*/ |
186
|
3 |
|
public function addFieldsMap(array $fields) |
187
|
|
|
{ |
188
|
3 |
|
foreach ($fields as $field => $key) { |
189
|
3 |
|
$this->fieldsMap[$field] = $key; |
190
|
|
|
} |
191
|
3 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param array $fields |
195
|
|
|
*/ |
196
|
3 |
|
public function removeFields(array $fields) |
197
|
|
|
{ |
198
|
3 |
|
foreach ($fields as $field) { |
199
|
3 |
|
unset($this->fieldsMap[$field]); |
200
|
|
|
} |
201
|
3 |
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
3 |
|
public function getXdtRows(): array |
207
|
|
|
{ |
208
|
3 |
|
return $this->xdtRows; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|