diff --git a/IteratorFactory.php b/IteratorFactory.php index 24aa15c..dacb561 100644 --- a/IteratorFactory.php +++ b/IteratorFactory.php @@ -17,7 +17,7 @@ interface IteratorFactory { - public function fromFile(\SplFileObject $path, Mapping $mapping): Iterator; + public function fromFile(\SplFileObject $file, Mapping $mapping): Iterator; public function fromSpreadsheet(Spreadsheet $spreadsheet, Mapping $mapping): Iterator; } diff --git a/Spreadsheet.php b/Spreadsheet.php index 1d38d55..74612b8 100644 --- a/Spreadsheet.php +++ b/Spreadsheet.php @@ -15,6 +15,8 @@ interface Spreadsheet { + public static function fromFile(\SplFileObject $file): self; + /** * @param int $startRowIndex row index where this iterator starts, one-based */ diff --git a/SpreadsheetIteratorFactory.php b/SpreadsheetIteratorFactory.php index aded32e..851c332 100644 --- a/SpreadsheetIteratorFactory.php +++ b/SpreadsheetIteratorFactory.php @@ -22,28 +22,30 @@ final class SpreadsheetIteratorFactory implements IteratorFactory */ private $denormalizer; - public function __construct(Denormalizer $denormalizer) + /** + * @var array> + */ + private $spreadsheetClasses; + + /** + * @param array> $spreadsheetClasses + */ + public function __construct(Denormalizer $denormalizer, array $spreadsheetClasses) { $this->denormalizer = $denormalizer; + $this->spreadsheetClasses = $spreadsheetClasses; } /** * @throws \RuntimeException */ - public function fromFile(\SplFileObject $path, Mapping $mapping): Iterator + public function fromFile(\SplFileObject $file, Mapping $mapping): Iterator { - switch (true) { - case true === interface_exists(\PhpOffice\PhpSpreadsheet\Reader\IReader::class): - $spreadsheet = new Bridge\PhpSpreadsheet\Spreadsheet($path); - break; - case true === interface_exists(\Box\Spout\Reader\ReaderInterface::class): - $spreadsheet = new Bridge\Spout\Spreadsheet($path); - break; - default: - throw new \RuntimeException('Install either phpoffice/phpspreadsheet or box/spout to read Excel files'); + foreach ($this->spreadsheetClasses as $spreadsheetClass) { + return $this->fromSpreadsheet($spreadsheetClass::fromFile($file), $mapping); } - return $this->fromSpreadsheet($spreadsheet, $mapping); + throw new \RuntimeException('Install either phpoffice/phpspreadsheet or box/spout to read Excel files'); } public function fromSpreadsheet(Spreadsheet $spreadsheet, Mapping $mapping): Iterator diff --git a/Test/SpreadsheetIteratorTest.php b/Test/SpreadsheetIteratorTest.php index 2be1069..2e9a83c 100644 --- a/Test/SpreadsheetIteratorTest.php +++ b/Test/SpreadsheetIteratorTest.php @@ -73,7 +73,10 @@ public function testCanPerformNextCorrectly(): void 'prev' => ['count' => 0, 'params' => null], 'next' => ['count' => 1, 'params' => null], ])); + $key = $iterator->key(); $iterator->next(); + + static::assertGreaterThan($key, $iterator->key()); } public function testWillOfferAnDidYouMeanForInvalidHeader(): void