From c63c9e640203c17bac540928bf539845ad6ba72b Mon Sep 17 00:00:00 2001 From: atymic Date: Mon, 27 Dec 2021 12:56:17 +1100 Subject: [PATCH] feat: limit imports --- .gitignore | 1 + README.md | 6 ++++++ src/FastExcel.php | 15 +++++++++++++++ src/Importable.php | 7 ++++++- tests/LimitTest.php | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/LimitTest.php diff --git a/.gitignore b/.gitignore index f879d83..37976f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor /.idea /build +.phpunit.result.cache diff --git a/README.md b/README.md index 6302729..610a614 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,12 @@ Import a `csv` with specific delimiter, enclosure characters and "gbk" encoding: $collection = (new FastExcel)->configureCsv(';', '#', 'gbk')->import('file.csv'); ``` +Importing only the first 100 lines from a `csv` : + +```php +$collection = (new FastExcel)->limitRows(100)->import('file.csv'); +``` + Import and insert to database: ```php diff --git a/src/FastExcel.php b/src/FastExcel.php index 8fe127f..f00f814 100644 --- a/src/FastExcel.php +++ b/src/FastExcel.php @@ -36,6 +36,11 @@ class FastExcel */ private $start_row = 1; + /** + * @var ?int + */ + private $end_row = null; + /** * @var bool */ @@ -127,6 +132,16 @@ public function startRow(int $row) return $this; } + /** + * @return $this + */ + public function limitRows(int $row = null) + { + $this->end_row = $row; + + return $this; + } + /** * @return $this */ diff --git a/src/Importable.php b/src/Importable.php index 0cc34f0..2393b9b 100644 --- a/src/Importable.php +++ b/src/Importable.php @@ -12,6 +12,7 @@ * Trait Importable. * * @property int $start_row + * @property ?int $end_row * @property bool $transpose * @property bool $with_header */ @@ -140,7 +141,7 @@ private function importSheet(SheetInterface $sheet, callable $callback = null) foreach ($sheet->getRowIterator() as $k => $rowAsObject) { $row = $rowAsObject->toArray(); - if ($k >= $this->start_row) { + if ($k >= $this->start_row ) { if ($this->with_header) { if ($k == $this->start_row) { $headers = $this->toStrings($row); @@ -161,6 +162,10 @@ private function importSheet(SheetInterface $sheet, callable $callback = null) $collection[] = empty($headers) ? $row : array_combine($headers, $row); } } + + if ($k > $this->end_row) { + break; + } } if ($this->transpose) { diff --git a/tests/LimitTest.php b/tests/LimitTest.php new file mode 100644 index 0000000..fbca082 --- /dev/null +++ b/tests/LimitTest.php @@ -0,0 +1,36 @@ + 'b', 'c' => 'd']); + } + } + + public function arrayGenerator($n) + { + for ($i = 1; $i <= $n; $i++) { + yield ['a' => 'b', 'c' => 'd']; + } + } + + public function testWithGenerator() + { + (new FastExcel($this->collectionGenerator(100)))->export(__DIR__.'/test-generator.xlsx'); + $result = (new FastExcel())->limitRows(10)->import(__DIR__.'/test-generator.xlsx'); + $this->assertEquals( + collect($this->arrayGenerator(10)), + $result + ); + unlink(__DIR__.'/test-generator.xlsx'); + } +}