@@ -53,13 +53,13 @@ public function toJson(string $filename): string|false
5353 * @return array<int,array<string,string>>
5454 * @throws FileHandlerException
5555 */
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
5757 {
5858 if (!file_exists ($ filename )) {
5959 throw new FileHandlerException ('file not found ' );
6060 }
6161
62- return iterator_to_array ($ this ->getRows ($ filename , $ hideColumns ));
62+ return iterator_to_array ($ this ->getRows ($ filename , $ hideColumns, $ limit ));
6363 }
6464
6565 public function findAndReplaceInCsv (
@@ -148,22 +148,22 @@ private function isValidCsvFileFormat(array $row): bool
148148 }
149149
150150 /**
151- * @param array<string> $row
151+ * @param array<string> $headers
152152 * @param array<string> $hideColumns
153- * @return array<int<0, max>,int|string >
153+ * @return array<int<0, max>,int>
154154 */
155- private function setColumnsToHide (array &$ row , array $ hideColumns ): array
155+ private function setColumnsToHide (array &$ headers , array $ hideColumns ): array
156156 {
157157 $ indices = [];
158158 if (!empty ($ hideColumns )) {
159159 foreach ($ hideColumns as $ hideColumn ) {
160- $ index = array_search ($ hideColumn , $ row );
160+ $ index = array_search ($ hideColumn , $ headers );
161161 if ($ index !== false ) {
162- $ indices [] = $ index ;
163- unset($ row [$ index ]);
162+ $ indices [] = ( int ) $ index ;
163+ unset($ headers [$ index ]);
164164 }
165165 }
166- $ row = array_values ($ row );
166+ $ headers = array_values ($ headers );
167167 }
168168 return $ indices ;
169169 }
@@ -174,7 +174,7 @@ private function setColumnsToHide(array &$row, array $hideColumns): array
174174 * @return Generator
175175 * @throws FileHandlerException
176176 */
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
178178 {
179179 $ csvFile = fopen ($ filename , 'r ' );
180180 if (!$ csvFile ) {
@@ -191,6 +191,7 @@ private function getRows(string $filename, array|false $hideColumns = false): Ge
191191
192192
193193 $ isEmptyFile = true ;
194+ $ count = 0 ;
194195 try {
195196 while (($ row = fgetcsv ($ csvFile )) !== false ) {
196197 $ isEmptyFile = false ;
@@ -199,18 +200,17 @@ private function getRows(string $filename, array|false $hideColumns = false): Ge
199200 }
200201
201202 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 );
209204 }
210205
211- $ item = array_combine ($ headers , $ row );
212206
207+ $ item = array_combine ($ headers , $ row );
213208 yield $ item ;
209+ $ count ++;
210+
211+ if (is_int ($ limit ) && $ limit <= $ count ) {
212+ break ;
213+ }
214214 }
215215 } finally {
216216 fclose ($ csvFile );
@@ -222,6 +222,22 @@ private function getRows(string $filename, array|false $hideColumns = false): Ge
222222 }
223223 }
224224
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+
225241 /**
226242 * @param array<string> $row
227243 * @param string $keyword
0 commit comments