Skip to content

Commit

Permalink
fix: package isolation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarlovi committed Feb 28, 2020
1 parent 54c896b commit a92701c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion IteratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

interface Spreadsheet
{
public static function fromFile(\SplFileObject $file): self;

/**
* @param int $startRowIndex row index where this iterator starts, one-based
*/
Expand Down
26 changes: 14 additions & 12 deletions SpreadsheetIteratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,30 @@ final class SpreadsheetIteratorFactory implements IteratorFactory
*/
private $denormalizer;

public function __construct(Denormalizer $denormalizer)
/**
* @var array<class-string<Spreadsheet>>
*/
private $spreadsheetClasses;

/**
* @param array<class-string<Spreadsheet>> $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
Expand Down
3 changes: 3 additions & 0 deletions Test/SpreadsheetIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a92701c

Please sign in to comment.