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