|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of Underscore.php |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Maxime Fabre <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Underscore; |
|
14
|
|
|
|
|
15
|
|
|
use JsonException; |
|
16
|
|
|
use Underscore\Methods\ArraysMethods; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Parse from various formats to various formats. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Parse |
|
22
|
|
|
{ |
|
23
|
|
|
//////////////////////////////////////////////////////////////////// |
|
24
|
|
|
/////////////////////////////// JSON /////////////////////////////// |
|
25
|
|
|
//////////////////////////////////////////////////////////////////// |
|
26
|
|
|
/** |
|
27
|
|
|
* Converts data from JSON. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $data The data to parse |
|
30
|
|
|
* |
|
31
|
|
|
* @throws JsonException |
|
32
|
1 |
|
*/ |
|
33
|
|
|
public static function fromJSON(string $data) : mixed |
|
34
|
1 |
|
{ |
|
35
|
|
|
return json_decode($data, true, 512, JSON_THROW_ON_ERROR); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Converts data to JSON. |
|
40
|
|
|
* |
|
41
|
|
|
* @param mixed $data The data to convert |
|
42
|
|
|
* |
|
43
|
|
|
* @return string Converted data |
|
44
|
8 |
|
* @throws JsonException |
|
45
|
|
|
*/ |
|
46
|
8 |
|
public static function toJSON(mixed $data) : string |
|
47
|
|
|
{ |
|
48
|
|
|
return json_encode($data, JSON_THROW_ON_ERROR); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
//////////////////////////////////////////////////////////////////// |
|
52
|
|
|
//////////////////////////////// XML /////////////////////////////// |
|
53
|
|
|
//////////////////////////////////////////////////////////////////// |
|
54
|
|
|
/** |
|
55
|
|
|
* Converts data from XML. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $xml The data to parse |
|
58
|
|
|
* |
|
59
|
|
|
* @throws JsonException |
|
60
|
1 |
|
*/ |
|
61
|
|
|
public static function fromXML(string $xml) : array |
|
62
|
1 |
|
{ |
|
63
|
1 |
|
$xmlEl = simplexml_load_string($xml); |
|
64
|
1 |
|
$xmlEnc = json_encode($xmlEl, JSON_THROW_ON_ERROR); |
|
65
|
|
|
|
|
66
|
1 |
|
return json_decode($xmlEnc, true, 512, JSON_THROW_ON_ERROR); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
//////////////////////////////////////////////////////////////////// |
|
70
|
|
|
//////////////////////////////// CSV /////////////////////////////// |
|
71
|
|
|
//////////////////////////////////////////////////////////////////// |
|
72
|
|
|
/** |
|
73
|
|
|
* Converts data from CSV. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $data The data to parse |
|
76
|
|
|
* @param bool $hasHeaders Whether the CSV has headers |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function fromCSV(string $data, bool $hasHeaders = false) : array |
|
79
|
|
|
{ |
|
80
|
|
|
$data = trim($data); |
|
81
|
2 |
|
|
|
82
|
|
|
// Explodes rows |
|
83
|
2 |
|
$dataArray = static::explodeWith($data, [PHP_EOL, "\r", "\n"]); |
|
84
|
|
|
$dataArray = array_map(fn($row): array => Parse::explodeWith($row, [';', "\t", ',']), $dataArray); |
|
85
|
|
|
|
|
86
|
2 |
|
// Get headers |
|
87
|
|
|
$headers = $hasHeaders ? $dataArray[0] : array_keys($dataArray[0]); |
|
88
|
2 |
|
if ($hasHeaders) { |
|
89
|
2 |
|
array_shift($dataArray); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
// Parse the columns in each row |
|
93
|
2 |
|
$array = []; |
|
94
|
1 |
|
foreach ($dataArray as $row => $columns) { |
|
95
|
|
|
foreach ($columns as $columnNumber => $column) { |
|
96
|
|
|
$array[$row][$headers[$columnNumber]] = $column; |
|
97
|
|
|
} |
|
98
|
2 |
|
} |
|
99
|
2 |
|
|
|
100
|
2 |
|
return $array; |
|
101
|
2 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Converts data to CSV. |
|
105
|
2 |
|
* |
|
106
|
|
|
* @param mixed $data The data to convert |
|
107
|
|
|
* |
|
108
|
|
|
* @return string Converted data |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function toCSV(mixed $data, string $delimiter = ';', bool $exportHeaders = false) : string |
|
111
|
|
|
{ |
|
112
|
|
|
$csv = []; |
|
113
|
|
|
|
|
114
|
|
|
// Convert objects to arrays |
|
115
|
|
|
if (\is_object($data)) { |
|
116
|
|
|
$data = (array) $data; |
|
117
|
3 |
|
} |
|
118
|
|
|
|
|
119
|
3 |
|
// Don't convert if it's not an array |
|
120
|
|
|
if ( ! \is_array($data)) { |
|
121
|
|
|
return $data; |
|
122
|
3 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
// Fetch headers if requested |
|
125
|
|
|
if ($exportHeaders) { |
|
126
|
|
|
$headers = array_keys(ArraysMethods::first($data)); |
|
127
|
3 |
|
$csv[] = implode($delimiter, $headers); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Quote values and create row |
|
131
|
|
|
foreach ($data as $header => $row) { |
|
132
|
3 |
|
// If single column |
|
133
|
1 |
|
if ( ! \is_array($row)) { |
|
134
|
1 |
|
$csv[] = '"'.$header.'"'.$delimiter.'"'.$row.'"'; |
|
135
|
|
|
continue; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
3 |
|
// Else add values |
|
139
|
|
|
foreach ($row as $key => $value) { |
|
140
|
3 |
|
$row[$key] = '"'.stripslashes((string) $value).'"'; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$csv[] = implode($delimiter, $row); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
return implode(PHP_EOL, $csv); |
|
147
|
3 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
//////////////////////////////////////////////////////////////////// |
|
150
|
3 |
|
///////////////////////// TYPES SWITCHERS ////////////////////////// |
|
151
|
|
|
//////////////////////////////////////////////////////////////////// |
|
152
|
|
|
/** |
|
153
|
3 |
|
* Converts data to an array. |
|
154
|
|
|
* |
|
155
|
|
|
* |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function toArray(mixed $data) : array |
|
158
|
|
|
{ |
|
159
|
|
|
// Look for common array conversion patterns in objects |
|
160
|
|
|
if (\is_object($data) && method_exists($data, 'toArray')) { |
|
161
|
|
|
$data = $data->toArray(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return (array) $data; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
18 |
|
/** |
|
168
|
|
|
* Converts data to a string. |
|
169
|
|
|
* |
|
170
|
18 |
|
* |
|
171
|
1 |
|
* @throws JsonException |
|
172
|
|
|
*/ |
|
173
|
|
|
public static function toString(mixed $data) : string |
|
174
|
18 |
|
{ |
|
175
|
|
|
// Avoid Array to Strings conversion exception |
|
176
|
|
|
if (\is_array($data)) { |
|
177
|
|
|
return static::toJSON($data); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return (string) $data; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
12 |
|
* Converts data to an integer. |
|
185
|
|
|
* |
|
186
|
|
|
* |
|
187
|
12 |
|
*/ |
|
188
|
2 |
|
public static function toInteger(mixed $data) : int |
|
189
|
|
|
{ |
|
190
|
|
|
// Returns size of arrays |
|
191
|
10 |
|
if (\is_array($data)) { |
|
192
|
|
|
return \count($data); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// Returns size of strings |
|
196
|
|
|
if (\is_string($data) && ! preg_match('/[0-9. ,]+/', $data)) { |
|
197
|
|
|
return \strlen($data); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return (int) $data; |
|
201
|
6 |
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
6 |
|
* Converts data to a boolean. |
|
205
|
2 |
|
* |
|
206
|
|
|
* |
|
207
|
|
|
*/ |
|
208
|
|
|
public static function toBoolean(mixed $data) : bool |
|
209
|
4 |
|
{ |
|
210
|
2 |
|
return (bool) $data; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
2 |
|
/** |
|
214
|
|
|
* Converts data to an object. |
|
215
|
|
|
* |
|
216
|
|
|
* |
|
217
|
|
|
*/ |
|
218
|
|
|
public static function toObject(mixed $data) : object |
|
219
|
|
|
{ |
|
220
|
|
|
return (object) $data; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
5 |
|
//////////////////////////////////////////////////////////////////// |
|
224
|
|
|
///////////////////////////// HELPERS ////////////////////////////// |
|
225
|
5 |
|
//////////////////////////////////////////////////////////////////// |
|
226
|
|
|
/** |
|
227
|
|
|
* Tries to explode a string with an array of delimiters. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $string The string |
|
230
|
|
|
* @param array $delimiters An array of delimiters |
|
231
|
|
|
*/ |
|
232
|
|
|
public static function explodeWith(string $string, array $delimiters) : array |
|
233
|
|
|
{ |
|
234
|
|
|
$array = $string; |
|
|
|
|
|
|
235
|
3 |
|
|
|
236
|
|
|
foreach ($delimiters as $delimiter) { |
|
237
|
3 |
|
$array = explode($delimiter, $string); |
|
238
|
|
|
if (\count($array) === 1) { |
|
239
|
|
|
continue; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return $array; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return [$string]; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|