2
2
# jupitern/file-parser
3
3
#### PHP File Parser.
4
4
5
- read, filter, parse and format {csv, tsv, dsv, variable-length-delimited} and other txt files
5
+ read, filter, parse and format {csv, tsv, dsv, variable-length-delimited} from files or strings
6
6
7
7
## Requirements
8
8
9
- PHP 5.4 or higher.
9
+ PHP 8.0 or higher.
10
10
11
11
## Installation
12
12
@@ -22,6 +22,92 @@ Include jupitern/file-parser in your project, by adding it to your composer.json
22
22
## Usage
23
23
``` php
24
24
25
+ Lets parse a csv from a string with contents (animal, category, count):
26
+ animal,type,count
27
+ crocodile,reptile,4
28
+ dolphin,mammal,0
29
+ duck,bird,2
30
+ koala,mammal,4
31
+ lion,mammal,5
32
+
33
+ lets parse the file with:
34
+ - ignore the first line
35
+ - convert encoding from ISO-9959-1 to UTF-8
36
+ - convert lines to objects
37
+ - remove animals with count 0
38
+ - format the animal type to uppercase
39
+ - group by type
40
+
41
+ $objectsArr = \Jupitern\Parser\FileParser::instance()
42
+ ->fromString('animal,type,count
43
+ crocodile,reptile,4
44
+ dolphin,mammal,0
45
+ duck,bird,2
46
+ koala,mammal,4
47
+ lion,mammal,5', ',')
48
+ ->setEncoding('ISO-8859-1', 'UTF-8')
49
+ ->toObject(['animal', 'type', 'animalCount'])
50
+ ->filter(function ($line, $lineNumber) {
51
+ return $lineNumber > 1 && $line->animalCount > 0;
52
+ })
53
+ ->format('type', function ($val) {
54
+ return strtoupper($val);
55
+ })
56
+ ->group(function ($line) {
57
+ return $line->type;
58
+ })
59
+ ->parse();
60
+
61
+ echo '<pre >';
62
+ print_r($objectsArr);
63
+
64
+ /*
65
+ output:
66
+ Array
67
+ (
68
+ [REPTILE] => Array
69
+ (
70
+ [0] => stdClass Object
71
+ (
72
+ [animal] => crocodile
73
+ [type] => REPTILE
74
+ [animalCount] => 4
75
+ )
76
+
77
+ )
78
+
79
+ [BIRD] => Array
80
+ (
81
+ [0] => stdClass Object
82
+ (
83
+ [animal] => duck
84
+ [type] => BIRD
85
+ [animalCount] => 2
86
+ )
87
+
88
+ )
89
+
90
+ [MAMMAL] => Array
91
+ (
92
+ [0] => stdClass Object
93
+ (
94
+ [animal] => koala
95
+ [type] => MAMMAL
96
+ [animalCount] => 4
97
+ )
98
+
99
+ [1] => stdClass Object
100
+ (
101
+ [animal] => lion
102
+ [type] => MAMMAL
103
+ [animalCount] => 5
104
+ )
105
+
106
+ )
107
+
108
+ )
109
+ */
110
+
25
111
Given a csv file "filename.csv" with contents (animal, category, count):
26
112
animal,type,count
27
113
crocodile,reptile,4
@@ -32,19 +118,19 @@ lion,mammal,5
32
118
33
119
lets parse the file with:
34
120
- ignore the first line
35
- - convert encoding to ISO-9959-1
121
+ - convert encoding from ISO-9959-1 to UTF-8
36
122
- convert lines to objects
37
123
- remove animals with count 0
38
124
- format the animal type to uppercase
39
125
- group by type
40
126
41
127
// read a file to array
42
128
$objectsArr = \Jupitern\Parser\FileParser::instance()
43
- ->setFile("csv .txt" , ',')
129
+ ->fromFile('D:\\aaa .txt' , ',')
44
130
->setEncoding('ISO-8859-1', 'UTF-8')
45
131
->toObject(['animal', 'type', 'animalCount'])
46
132
->filter(function ($line, $lineNumber) {
47
- return $lineNumber > 1 && $line->animalCounnt > 0;
133
+ return $lineNumber > 1 && $line->animalCount > 0;
48
134
})
49
135
->format('type', function ($val) {
50
136
return strtoupper($val);
@@ -61,58 +147,61 @@ print_r($objectsArr);
61
147
output:
62
148
Array
63
149
(
64
- [REPTILE] => Array(
65
- [0] => stdClass Object(
150
+ [REPTILE] => Array
151
+ (
152
+ [0] => stdClass Object
153
+ (
66
154
[animal] => crocodile
67
155
[type] => REPTILE
68
- [number ] => 4
156
+ [animalCount ] => 4
69
157
)
158
+
70
159
)
71
160
72
- [BIRD] => Array(
73
- [0] => stdClass Object(
161
+ [BIRD] => Array
162
+ (
163
+ [0] => stdClass Object
164
+ (
74
165
[animal] => duck
75
166
[type] => BIRD
76
- [number ] => 2
167
+ [animalCount ] => 2
77
168
)
169
+
78
170
)
79
171
80
- [MAMMAL] => Array (
81
- [0] => stdClass Object(
172
+ [MAMMAL] => Array
173
+ (
174
+ [0] => stdClass Object
175
+ (
82
176
[animal] => koala
83
177
[type] => MAMMAL
84
- [number ] => 4
178
+ [animalCount ] => 4
85
179
)
86
- [1] => stdClass Object(
180
+
181
+ [1] => stdClass Object
182
+ (
87
183
[animal] => lion
88
184
[type] => MAMMAL
89
- [number ] => 5
185
+ [animalCount ] => 5
90
186
)
91
- )
92
187
93
- [FISH] => Array
94
- (
95
- [0] => stdClass Object(
96
- [animal] => áéíóú
97
- [type] => FISH
98
- [number] => 3
99
- )
100
188
)
189
+
101
190
)
102
191
*/
103
192
104
193
105
194
For the same file lets parse with:
106
- - convert encoding to ISO-9959-1
195
+ - convert encoding from ISO-9959-1 to UTF-8
107
196
- convert lines to arrays
108
197
- remove animals with count 0
109
198
- group by type
110
199
111
200
$objectsArr = \Jupitern\Parser\FileParser::instance()
112
201
->setFile("csv.txt", ',')
113
202
->setEncoding('ISO-8859-1', 'UTF-8')
114
- ->filter(function ($line) {
115
- return $line[2] > 0;
203
+ ->filter(function ($line, $lineNumber ) {
204
+ return $lineNumber > 1 && $ line[2] > 0;
116
205
})
117
206
->group(function ($line) {
118
207
return $line[1];
@@ -128,40 +217,44 @@ Array
128
217
(
129
218
[reptile] => Array
130
219
(
131
- [0] => Array(
220
+ [0] => Array
221
+ (
132
222
[0] => crocodile
133
223
[1] => reptile
134
224
[2] => 4
135
225
)
226
+
136
227
)
137
- [bird] => Array(
138
- [0] => Array(
228
+
229
+ [bird] => Array
230
+ (
231
+ [0] => Array
232
+ (
139
233
[0] => duck
140
234
[1] => bird
141
235
[2] => 2
142
236
)
237
+
143
238
)
239
+
144
240
[mammal] => Array
145
241
(
146
- [0] => Array(
242
+ [0] => Array
243
+ (
147
244
[0] => koala
148
245
[1] => mammal
149
246
[2] => 4
150
247
)
151
- [1] => Array(
248
+
249
+ [1] => Array
250
+ (
152
251
[0] => lion
153
252
[1] => mammal
154
253
[2] => 5
155
254
)
255
+
156
256
)
157
- [fish] => Array
158
- (
159
- [0] => Array(
160
- [0] => áéíóú
161
- [1] => fish
162
- [2] => 3
163
- )
164
- )
257
+
165
258
)
166
259
*/
167
260
@@ -174,7 +267,7 @@ Given a dsv file "file.txt" with contents (empolyee number, birth date, monthly
174
267
05luís manuel 1983-01-01 1323.52
175
268
176
269
lets parse the file doing:
177
- - convert encoding to ISO-9959-1
270
+ - convert encoding from ISO-9959-1 to UTF-8
178
271
- convert lines to objects
179
272
- format person name capitalize first letters
180
273
- group by wage bellow or above 1000
@@ -205,47 +298,68 @@ print_r($objectsArr);
205
298
Output:
206
299
Array
207
300
(
208
- [bellow 1000] => Array(
209
- [0] => stdClass Object(
301
+ [bellow 1000] => Array
302
+ (
303
+ [0] => stdClass Object
304
+ (
210
305
[Number] => 01
211
306
[Name] => John Doe
212
307
[BirthDate] => 1980-01-01
213
308
[MonthlyIncome] => 923.5
214
309
)
215
- [1] => stdClass Object(
310
+
311
+ [1] => stdClass Object
312
+ (
216
313
[Number] => 02
217
314
[Name] => Jaqueline Wayne
218
315
[BirthDate] => 1983-01-01
219
316
[MonthlyIncome] => 822.44
220
317
)
318
+
319
+ [2] => stdClass Object
320
+ (
321
+ [Number] => 05
322
+ [Name] => LuÃs Manuel
323
+ [BirthDate] => 1983-01-0
324
+ [MonthlyIncome] => 1
325
+ )
326
+
221
327
)
222
- [above 1000] => Array(
223
- [0] => stdClass Object(
328
+
329
+ [above 1000] => Array
330
+ (
331
+ [0] => stdClass Object
332
+ (
224
333
[Number] => 01
225
334
[Name] => Luis West
226
335
[BirthDate] => 1976-01-01
227
336
[MonthlyIncome] => 1143.3
228
337
)
229
- [1] => stdClass Object(
338
+
339
+ [1] => stdClass Object
340
+ (
230
341
[Number] => 01
231
342
[Name] => Madalena
232
343
[BirthDate] => 1983-01-01
233
344
[MonthlyIncome] => 2173.6
234
345
)
235
- [2] => stdClass Object(
236
- [Number] => 05
237
- [Name] => Luís Manuel
238
- [BirthDate] => 1983-01-01
239
- [MonthlyIncome] => 1323.52
240
- )
346
+
241
347
)
348
+
242
349
)
243
350
*/
244
351
245
352
```
246
353
247
354
## ChangeLog
248
355
356
+ v1.2.0
357
+
358
+ - min php version updated to 8.0
359
+ - code refactor for php8
360
+ - allow parse from string or file
361
+
362
+ v1
249
363
- initial release
250
364
251
365
## Contributing
0 commit comments