@@ -53,13 +53,13 @@ public function toJson(string $filename): string|false
53
53
* @return array<int,array<string,string>>
54
54
* @throws FileHandlerException
55
55
*/
56
- public function toArray (string $ filename , array |false $ hideColumns = false ): array
56
+ public function toArray (string $ filename , array |false $ hideColumns = false , int | false $ limit = false ): array
57
57
{
58
58
if (!file_exists ($ filename )) {
59
59
throw new FileHandlerException ('file not found ' );
60
60
}
61
61
62
- return iterator_to_array ($ this ->getRows ($ filename , $ hideColumns ));
62
+ return iterator_to_array ($ this ->getRows ($ filename , $ hideColumns, $ limit ));
63
63
}
64
64
65
65
public function findAndReplaceInCsv (
@@ -148,22 +148,22 @@ private function isValidCsvFileFormat(array $row): bool
148
148
}
149
149
150
150
/**
151
- * @param array<string> $row
151
+ * @param array<string> $headers
152
152
* @param array<string> $hideColumns
153
- * @return array<int<0, max>,int|string >
153
+ * @return array<int<0, max>,int>
154
154
*/
155
- private function setColumnsToHide (array &$ row , array $ hideColumns ): array
155
+ private function setColumnsToHide (array &$ headers , array $ hideColumns ): array
156
156
{
157
157
$ indices = [];
158
158
if (!empty ($ hideColumns )) {
159
159
foreach ($ hideColumns as $ hideColumn ) {
160
- $ index = array_search ($ hideColumn , $ row );
160
+ $ index = array_search ($ hideColumn , $ headers );
161
161
if ($ index !== false ) {
162
- $ indices [] = $ index ;
163
- unset($ row [$ index ]);
162
+ $ indices [] = ( int ) $ index ;
163
+ unset($ headers [$ index ]);
164
164
}
165
165
}
166
- $ row = array_values ($ row );
166
+ $ headers = array_values ($ headers );
167
167
}
168
168
return $ indices ;
169
169
}
@@ -174,7 +174,7 @@ private function setColumnsToHide(array &$row, array $hideColumns): array
174
174
* @return Generator
175
175
* @throws FileHandlerException
176
176
*/
177
- private function getRows (string $ filename , array |false $ hideColumns = false ): Generator
177
+ private function getRows (string $ filename , array |false $ hideColumns = false , int | false $ limit = false ): Generator
178
178
{
179
179
$ csvFile = fopen ($ filename , 'r ' );
180
180
if (!$ csvFile ) {
@@ -191,6 +191,7 @@ private function getRows(string $filename, array|false $hideColumns = false): Ge
191
191
192
192
193
193
$ isEmptyFile = true ;
194
+ $ count = 0 ;
194
195
try {
195
196
while (($ row = fgetcsv ($ csvFile )) !== false ) {
196
197
$ isEmptyFile = false ;
@@ -199,18 +200,17 @@ private function getRows(string $filename, array|false $hideColumns = false): Ge
199
200
}
200
201
201
202
if (!empty ($ indices )) {
202
- foreach ($ indices as $ index ) {
203
- if (isset ($ row [$ index ])) {
204
- unset($ row [$ index ]);
205
- }
206
- }
207
-
208
- $ row = array_values ($ row );
203
+ $ this ->removeElementByIndex ($ row , $ indices );
209
204
}
210
205
211
- $ item = array_combine ($ headers , $ row );
212
206
207
+ $ item = array_combine ($ headers , $ row );
213
208
yield $ item ;
209
+ $ count ++;
210
+
211
+ if (is_int ($ limit ) && $ limit <= $ count ) {
212
+ break ;
213
+ }
214
214
}
215
215
} finally {
216
216
fclose ($ csvFile );
@@ -222,6 +222,22 @@ private function getRows(string $filename, array|false $hideColumns = false): Ge
222
222
}
223
223
}
224
224
225
+ /**
226
+ * @param array<int,string> $row
227
+ * @param array<int<0, max>, int> $indices
228
+ * @return void
229
+ */
230
+ private function removeElementByIndex (array &$ row , array $ indices ): void
231
+ {
232
+ foreach ($ indices as $ index ) {
233
+ if (isset ($ row [$ index ])) {
234
+ unset($ row [$ index ]);
235
+ }
236
+ }
237
+
238
+ $ row = array_values ($ row );
239
+ }
240
+
225
241
/**
226
242
* @param array<string> $row
227
243
* @param string $keyword
0 commit comments