Enable support to XLSM spreadsheets
This commit is contained in:
parent
eb88bb4c3a
commit
f8d14d3f05
@ -28,6 +28,16 @@ use Box\Spout\Reader\XLSX\Reader as XLSXReader;
|
||||
*/
|
||||
class ReaderFactory
|
||||
{
|
||||
/**
|
||||
* File extensions and readers mapped
|
||||
*/
|
||||
protected const MAPPED_EXTENSIONS = [
|
||||
'xlsx' => 'xlsx',
|
||||
'csv' => 'csv',
|
||||
'ods' => 'ods',
|
||||
'xlsm' => 'xlsx',
|
||||
];
|
||||
|
||||
/**
|
||||
* Creates a reader by file extension
|
||||
*
|
||||
@ -42,6 +52,15 @@ class ReaderFactory
|
||||
return self::createFromType($extension);
|
||||
}
|
||||
|
||||
public static function getMappedFormats(string $fileExtension)
|
||||
{
|
||||
if (!(array_key_exists($fileExtension, self::MAPPED_EXTENSIONS))) {
|
||||
throw new UnsupportedTypeException('No readers supporting the given type: ' . $fileExtension);
|
||||
}
|
||||
|
||||
return self::MAPPED_EXTENSIONS[$fileExtension];
|
||||
}
|
||||
|
||||
/**
|
||||
* This creates an instance of the appropriate reader, given the type of the file to be read
|
||||
*
|
||||
@ -51,13 +70,10 @@ class ReaderFactory
|
||||
*/
|
||||
public static function createFromType($readerType)
|
||||
{
|
||||
switch ($readerType) {
|
||||
case Type::CSV: return self::createCSVReader();
|
||||
case Type::XLSX: return self::createXLSXReader();
|
||||
case Type::ODS: return self::createODSReader();
|
||||
default:
|
||||
throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType);
|
||||
}
|
||||
$readerType = self::getMappedFormats($readerType);
|
||||
$methodName = 'create' . strtoupper($readerType) . 'Reader';
|
||||
|
||||
return self::$methodName();
|
||||
}
|
||||
|
||||
/**
|
||||
|
73
tests/Spout/Reader/XLSM/ReaderTest.php
Normal file
73
tests/Spout/Reader/XLSM/ReaderTest.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class ReaderTest
|
||||
*/
|
||||
class ReaderTest extends TestCase
|
||||
{
|
||||
use TestUsingResource;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReaderFactoryShouldReturnXLSXReader()
|
||||
{
|
||||
$resourcePath = $this->getResourcePath('simple-xlsm-sample.xlsm');
|
||||
$reader = ReaderEntityFactory::createReaderFromFile($resourcePath);
|
||||
|
||||
$this->assertInstanceOf(Box\Spout\Reader\ReaderInterface::class, $reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReadXLSMSpreadsheetShouldReturnData()
|
||||
{
|
||||
$this->assertNotEmpty($this->getAllRowsForFile('simple-xlsm-sample.xlsm'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSpreadsheetRowsShouldMatch()
|
||||
{
|
||||
$expectedRows = [
|
||||
['a1', 'b1', 'c1'],
|
||||
['a2', 'b2', 'c2'],
|
||||
['a3', 'b3', 'c3'],
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedRows, $this->getAllRowsForFile('simple-xlsm-sample.xlsm'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @param bool $shouldFormatDates
|
||||
* @param bool $shouldPreserveEmptyRows
|
||||
* @return array All the read rows the given file
|
||||
*/
|
||||
private function getAllRowsForFile($fileName, $shouldFormatDates = false, $shouldPreserveEmptyRows = false)
|
||||
{
|
||||
$allRows = [];
|
||||
$resourcePath = $this->getResourcePath($fileName);
|
||||
|
||||
$reader = ReaderEntityFactory::createXLSXReader();
|
||||
$reader->setShouldFormatDates($shouldFormatDates);
|
||||
$reader->setShouldPreserveEmptyRows($shouldPreserveEmptyRows);
|
||||
$reader->open($resourcePath);
|
||||
|
||||
foreach ($reader->getSheetIterator() as $sheetIndex => $sheet) {
|
||||
foreach ($sheet->getRowIterator() as $rowIndex => $row) {
|
||||
$allRows[] = $row->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
$reader->close();
|
||||
|
||||
return $allRows;
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ namespace Box\Spout;
|
||||
trait TestUsingResource
|
||||
{
|
||||
/** @var string Path to the test resources folder */
|
||||
private $resourcesPath = 'tests/resources';
|
||||
private $resourcesPath = __DIR__ . '/../../tests/resources';
|
||||
|
||||
/** @var string Path to the test generated resources folder */
|
||||
private $generatedResourcesPath = 'tests/resources/generated';
|
||||
|
BIN
tests/resources/xlsm/simple-xlsm-sample.xlsm
Normal file
BIN
tests/resources/xlsm/simple-xlsm-sample.xlsm
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user