Skip to content

Commit eecf8a2

Browse files
committed
php8
- min php version updated to 8.0 - code refactor for php8 - allow parse from string or file
1 parent 7927fa1 commit eecf8a2

File tree

4 files changed

+229
-84
lines changed

4 files changed

+229
-84
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.env
2+
3+
# temp files from editors
4+
*~
5+
*.bak
6+
.DS_Store
7+
Thumbs.db
8+
.buildpath
9+
.project
10+
.settings
11+
.history
12+
.phpintel
13+
*.tmproj
14+
.idea
15+
nbproject/
16+
.composer.lock

README.md

Lines changed: 167 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# jupitern/file-parser
33
#### PHP File Parser.
44

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
66

77
## Requirements
88

9-
PHP 5.4 or higher.
9+
PHP 8.0 or higher.
1010

1111
## Installation
1212

@@ -22,6 +22,92 @@ Include jupitern/file-parser in your project, by adding it to your composer.json
2222
## Usage
2323
```php
2424

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+
25111
Given a csv file "filename.csv" with contents (animal, category, count):
26112
animal,type,count
27113
crocodile,reptile,4
@@ -32,19 +118,19 @@ lion,mammal,5
32118

33119
lets parse the file with:
34120
- ignore the first line
35-
- convert encoding to ISO-9959-1
121+
- convert encoding from ISO-9959-1 to UTF-8
36122
- convert lines to objects
37123
- remove animals with count 0
38124
- format the animal type to uppercase
39125
- group by type
40126

41127
// read a file to array
42128
$objectsArr = \Jupitern\Parser\FileParser::instance()
43-
->setFile("csv.txt", ',')
129+
->fromFile('D:\\aaa.txt', ',')
44130
->setEncoding('ISO-8859-1', 'UTF-8')
45131
->toObject(['animal', 'type', 'animalCount'])
46132
->filter(function ($line, $lineNumber) {
47-
return $lineNumber > 1 && $line->animalCounnt > 0;
133+
return $lineNumber > 1 && $line->animalCount > 0;
48134
})
49135
->format('type', function ($val) {
50136
return strtoupper($val);
@@ -61,58 +147,61 @@ print_r($objectsArr);
61147
output:
62148
Array
63149
(
64-
[REPTILE] => Array(
65-
[0] => stdClass Object(
150+
[REPTILE] => Array
151+
(
152+
[0] => stdClass Object
153+
(
66154
[animal] => crocodile
67155
[type] => REPTILE
68-
[number] => 4
156+
[animalCount] => 4
69157
)
158+
70159
)
71160

72-
[BIRD] => Array(
73-
[0] => stdClass Object(
161+
[BIRD] => Array
162+
(
163+
[0] => stdClass Object
164+
(
74165
[animal] => duck
75166
[type] => BIRD
76-
[number] => 2
167+
[animalCount] => 2
77168
)
169+
78170
)
79171

80-
[MAMMAL] => Array (
81-
[0] => stdClass Object(
172+
[MAMMAL] => Array
173+
(
174+
[0] => stdClass Object
175+
(
82176
[animal] => koala
83177
[type] => MAMMAL
84-
[number] => 4
178+
[animalCount] => 4
85179
)
86-
[1] => stdClass Object(
180+
181+
[1] => stdClass Object
182+
(
87183
[animal] => lion
88184
[type] => MAMMAL
89-
[number] => 5
185+
[animalCount] => 5
90186
)
91-
)
92187

93-
[FISH] => Array
94-
(
95-
[0] => stdClass Object(
96-
[animal] => áéíóú
97-
[type] => FISH
98-
[number] => 3
99-
)
100188
)
189+
101190
)
102191
*/
103192

104193

105194
For the same file lets parse with:
106-
- convert encoding to ISO-9959-1
195+
- convert encoding from ISO-9959-1 to UTF-8
107196
- convert lines to arrays
108197
- remove animals with count 0
109198
- group by type
110199

111200
$objectsArr = \Jupitern\Parser\FileParser::instance()
112201
->setFile("csv.txt", ',')
113202
->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;
116205
})
117206
->group(function ($line) {
118207
return $line[1];
@@ -128,40 +217,44 @@ Array
128217
(
129218
[reptile] => Array
130219
(
131-
[0] => Array(
220+
[0] => Array
221+
(
132222
[0] => crocodile
133223
[1] => reptile
134224
[2] => 4
135225
)
226+
136227
)
137-
[bird] => Array(
138-
[0] => Array(
228+
229+
[bird] => Array
230+
(
231+
[0] => Array
232+
(
139233
[0] => duck
140234
[1] => bird
141235
[2] => 2
142236
)
237+
143238
)
239+
144240
[mammal] => Array
145241
(
146-
[0] => Array(
242+
[0] => Array
243+
(
147244
[0] => koala
148245
[1] => mammal
149246
[2] => 4
150247
)
151-
[1] => Array(
248+
249+
[1] => Array
250+
(
152251
[0] => lion
153252
[1] => mammal
154253
[2] => 5
155254
)
255+
156256
)
157-
[fish] => Array
158-
(
159-
[0] => Array(
160-
[0] => áéíóú
161-
[1] => fish
162-
[2] => 3
163-
)
164-
)
257+
165258
)
166259
*/
167260

@@ -174,7 +267,7 @@ Given a dsv file "file.txt" with contents (empolyee number, birth date, monthly
174267
05luís manuel 1983-01-01 1323.52
175268

176269
lets parse the file doing:
177-
- convert encoding to ISO-9959-1
270+
- convert encoding from ISO-9959-1 to UTF-8
178271
- convert lines to objects
179272
- format person name capitalize first letters
180273
- group by wage bellow or above 1000
@@ -205,47 +298,68 @@ print_r($objectsArr);
205298
Output:
206299
Array
207300
(
208-
[bellow 1000] => Array(
209-
[0] => stdClass Object(
301+
[bellow 1000] => Array
302+
(
303+
[0] => stdClass Object
304+
(
210305
[Number] => 01
211306
[Name] => John Doe
212307
[BirthDate] => 1980-01-01
213308
[MonthlyIncome] => 923.5
214309
)
215-
[1] => stdClass Object(
310+
311+
[1] => stdClass Object
312+
(
216313
[Number] => 02
217314
[Name] => Jaqueline Wayne
218315
[BirthDate] => 1983-01-01
219316
[MonthlyIncome] => 822.44
220317
)
318+
319+
[2] => stdClass Object
320+
(
321+
[Number] => 05
322+
[Name] => Luís Manuel
323+
[BirthDate] => 1983-01-0
324+
[MonthlyIncome] => 1
325+
)
326+
221327
)
222-
[above 1000] => Array(
223-
[0] => stdClass Object(
328+
329+
[above 1000] => Array
330+
(
331+
[0] => stdClass Object
332+
(
224333
[Number] => 01
225334
[Name] => Luis West
226335
[BirthDate] => 1976-01-01
227336
[MonthlyIncome] => 1143.3
228337
)
229-
[1] => stdClass Object(
338+
339+
[1] => stdClass Object
340+
(
230341
[Number] => 01
231342
[Name] => Madalena
232343
[BirthDate] => 1983-01-01
233344
[MonthlyIncome] => 2173.6
234345
)
235-
[2] => stdClass Object(
236-
[Number] => 05
237-
[Name] => Luís Manuel
238-
[BirthDate] => 1983-01-01
239-
[MonthlyIncome] => 1323.52
240-
)
346+
241347
)
348+
242349
)
243350
*/
244351

245352
```
246353

247354
## ChangeLog
248355

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
249363
- initial release
250364

251365
## Contributing

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"issues": "https://github.com/jupitern/file-parser/issues"
1717
},
1818
"require" :{
19-
"php":">=5.4"
19+
"php":">=8.0"
2020
},
2121
"autoload": {
2222
"psr-4": {

0 commit comments

Comments
 (0)