Add ReaderOptions for all readers

Instead of passing every single option down the chain
This commit is contained in:
Adrien Loison 2016-10-17 22:09:20 -07:00
parent b61323d7d2
commit 4e33303990
19 changed files with 332 additions and 130 deletions

View File

@ -19,11 +19,15 @@ abstract class AbstractReader implements ReaderInterface
/** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
protected $globalFunctionsHelper; protected $globalFunctionsHelper;
/** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ /** @var \Box\Spout\Reader\ReaderOptionsCommon Reader's customized options */
protected $shouldFormatDates = false; protected $options;
/** @var bool Whether empty rows should be returned or skipped */ /**
protected $shouldPreserveEmptyRows = false; * Returns the reader's current options
*
* @return \Box\Spout\Reader\ReaderOptionsCommon
*/
abstract protected function getOptions();
/** /**
* Returns whether stream wrappers are supported * Returns whether stream wrappers are supported
@ -73,7 +77,7 @@ abstract class AbstractReader implements ReaderInterface
*/ */
public function setShouldFormatDates($shouldFormatDates) public function setShouldFormatDates($shouldFormatDates)
{ {
$this->shouldFormatDates = $shouldFormatDates; $this->getOptions()->setShouldFormatDates($shouldFormatDates);
return $this; return $this;
} }
@ -86,7 +90,7 @@ abstract class AbstractReader implements ReaderInterface
*/ */
public function setShouldPreserveEmptyRows($shouldPreserveEmptyRows) public function setShouldPreserveEmptyRows($shouldPreserveEmptyRows)
{ {
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; $this->getOptions()->setShouldPreserveEmptyRows($shouldPreserveEmptyRows);
return $this; return $this;
} }

View File

@ -4,7 +4,6 @@ namespace Box\Spout\Reader\CSV;
use Box\Spout\Reader\AbstractReader; use Box\Spout\Reader\AbstractReader;
use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\EncodingHelper;
/** /**
* Class Reader * Class Reader
@ -20,20 +19,21 @@ class Reader extends AbstractReader
/** @var SheetIterator To iterator over the CSV unique "sheet" */ /** @var SheetIterator To iterator over the CSV unique "sheet" */
protected $sheetIterator; protected $sheetIterator;
/** @var string Defines the character used to delimit fields (one character only) */ /** @var string Original value for the "auto_detect_line_endings" INI value */
protected $fieldDelimiter = ','; protected $originalAutoDetectLineEndings;
/** @var string Defines the character used to enclose fields (one character only) */ /**
protected $fieldEnclosure = '"'; * Returns the reader's current options
*
/** @var string Encoding of the CSV file to be read */ * @return ReaderOptions
protected $encoding = EncodingHelper::ENCODING_UTF8; */
protected function getOptions()
/** @var string Defines the End of line */ {
protected $endOfLineCharacter = "\n"; if (!isset($this->options)) {
$this->options = new ReaderOptions();
/** @var string */ }
protected $autoDetectLineEndings; return $this->options;
}
/** /**
* Sets the field delimiter for the CSV. * Sets the field delimiter for the CSV.
@ -44,7 +44,7 @@ class Reader extends AbstractReader
*/ */
public function setFieldDelimiter($fieldDelimiter) public function setFieldDelimiter($fieldDelimiter)
{ {
$this->fieldDelimiter = $fieldDelimiter; $this->getOptions()->setFieldDelimiter($fieldDelimiter);
return $this; return $this;
} }
@ -57,7 +57,7 @@ class Reader extends AbstractReader
*/ */
public function setFieldEnclosure($fieldEnclosure) public function setFieldEnclosure($fieldEnclosure)
{ {
$this->fieldEnclosure = $fieldEnclosure; $this->getOptions()->setFieldEnclosure($fieldEnclosure);
return $this; return $this;
} }
@ -70,7 +70,7 @@ class Reader extends AbstractReader
*/ */
public function setEncoding($encoding) public function setEncoding($encoding)
{ {
$this->encoding = $encoding; $this->getOptions()->setEncoding($encoding);
return $this; return $this;
} }
@ -83,7 +83,7 @@ class Reader extends AbstractReader
*/ */
public function setEndOfLineCharacter($endOfLineCharacter) public function setEndOfLineCharacter($endOfLineCharacter)
{ {
$this->endOfLineCharacter = $endOfLineCharacter; $this->getOptions()->setEndOfLineCharacter($endOfLineCharacter);
return $this; return $this;
} }
@ -107,7 +107,7 @@ class Reader extends AbstractReader
*/ */
protected function openReader($filePath) protected function openReader($filePath)
{ {
$this->autoDetectLineEndings = ini_get('auto_detect_line_endings'); $this->originalAutoDetectLineEndings = ini_get('auto_detect_line_endings');
ini_set('auto_detect_line_endings', '1'); ini_set('auto_detect_line_endings', '1');
$this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r'); $this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r');
@ -117,11 +117,7 @@ class Reader extends AbstractReader
$this->sheetIterator = new SheetIterator( $this->sheetIterator = new SheetIterator(
$this->filePointer, $this->filePointer,
$this->fieldDelimiter, $this->getOptions(),
$this->fieldEnclosure,
$this->endOfLineCharacter,
$this->encoding,
$this->shouldPreserveEmptyRows,
$this->globalFunctionsHelper $this->globalFunctionsHelper
); );
} }
@ -148,6 +144,6 @@ class Reader extends AbstractReader
$this->globalFunctionsHelper->fclose($this->filePointer); $this->globalFunctionsHelper->fclose($this->filePointer);
} }
ini_set('auto_detect_line_endings', $this->autoDetectLineEndings); ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings);
} }
} }

View File

@ -0,0 +1,111 @@
<?php
namespace Box\Spout\Reader\CSV;
use Box\Spout\Common\Helper\EncodingHelper;
use Box\Spout\Reader\ReaderOptionsCommon;
/**
* Class ReaderOptions
* This class is used to customize the reader's behavior
*
* @package Box\Spout\Reader\CSV
*/
class ReaderOptions extends ReaderOptionsCommon
{
/** @var string Defines the character used to delimit fields (one character only) */
protected $fieldDelimiter = ',';
/** @var string Defines the character used to enclose fields (one character only) */
protected $fieldEnclosure = '"';
/** @var string Encoding of the CSV file to be read */
protected $encoding = EncodingHelper::ENCODING_UTF8;
/** @var string Defines the End of line */
protected $endOfLineCharacter = "\n";
/**
* @return string
*/
public function getFieldDelimiter()
{
return $this->fieldDelimiter;
}
/**
* Sets the field delimiter for the CSV.
* Needs to be called before opening the reader.
*
* @param string $fieldDelimiter Character that delimits fields
* @return ReaderOptions
*/
public function setFieldDelimiter($fieldDelimiter)
{
$this->fieldDelimiter = $fieldDelimiter;
return $this;
}
/**
* @return string
*/
public function getFieldEnclosure()
{
return $this->fieldEnclosure;
}
/**
* Sets the field enclosure for the CSV.
* Needs to be called before opening the reader.
*
* @param string $fieldEnclosure Character that enclose fields
* @return ReaderOptions
*/
public function setFieldEnclosure($fieldEnclosure)
{
$this->fieldEnclosure = $fieldEnclosure;
return $this;
}
/**
* @return string
*/
public function getEncoding()
{
return $this->encoding;
}
/**
* Sets the encoding of the CSV file to be read.
* Needs to be called before opening the reader.
*
* @param string $encoding Encoding of the CSV file to be read
* @return ReaderOptions
*/
public function setEncoding($encoding)
{
$this->encoding = $encoding;
return $this;
}
/**
* @return string EOL for the CSV
*/
public function getEndOfLineCharacter()
{
return $this->endOfLineCharacter;
}
/**
* Sets the EOL for the CSV.
* Needs to be called before opening the reader.
*
* @param string $endOfLineCharacter used to properly get lines from the CSV file.
* @return ReaderOptions
*/
public function setEndOfLineCharacter($endOfLineCharacter)
{
$this->endOfLineCharacter = $endOfLineCharacter;
return $this;
}
}

View File

@ -40,6 +40,12 @@ class RowIterator implements IteratorInterface
/** @var string Encoding of the CSV file to be read */ /** @var string Encoding of the CSV file to be read */
protected $encoding; protected $encoding;
/** @var string End of line delimiter, given by the user as input. */
protected $inputEOLDelimiter;
/** @var bool Whether empty rows should be returned or skipped */
protected $shouldPreserveEmptyRows;
/** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
protected $globalFunctionsHelper; protected $globalFunctionsHelper;
@ -49,29 +55,19 @@ class RowIterator implements IteratorInterface
/** @var string End of line delimiter, encoded using the same encoding as the CSV */ /** @var string End of line delimiter, encoded using the same encoding as the CSV */
protected $encodedEOLDelimiter; protected $encodedEOLDelimiter;
/** @var string End of line delimiter, given by the user as input. */
protected $inputEOLDelimiter;
/** @var bool Whether empty rows should be returned or skipped */
protected $shouldPreserveEmptyRows;
/** /**
* @param resource $filePointer Pointer to the CSV file to read * @param resource $filePointer Pointer to the CSV file to read
* @param string $fieldDelimiter Character that delimits fields * @param \Box\Spout\Reader\CSV\ReaderOptions $options
* @param string $fieldEnclosure Character that enclose fields
* @param string $endOfLineDelimiter End of line delimiter
* @param string $encoding Encoding of the CSV file to be read
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
*/ */
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $endOfLineDelimiter, $encoding, $shouldPreserveEmptyRows, $globalFunctionsHelper) public function __construct($filePointer, $options, $globalFunctionsHelper)
{ {
$this->filePointer = $filePointer; $this->filePointer = $filePointer;
$this->fieldDelimiter = $fieldDelimiter; $this->fieldDelimiter = $options->getFieldDelimiter();
$this->fieldEnclosure = $fieldEnclosure; $this->fieldEnclosure = $options->getFieldEnclosure();
$this->encoding = $encoding; $this->encoding = $options->getEncoding();
$this->inputEOLDelimiter = $endOfLineDelimiter; $this->inputEOLDelimiter = $options->getEndOfLineCharacter();
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; $this->shouldPreserveEmptyRows = $options->shouldPreserveEmptyRows();
$this->globalFunctionsHelper = $globalFunctionsHelper; $this->globalFunctionsHelper = $globalFunctionsHelper;
$this->encodingHelper = new EncodingHelper($globalFunctionsHelper); $this->encodingHelper = new EncodingHelper($globalFunctionsHelper);

View File

@ -16,23 +16,12 @@ class Sheet implements SheetInterface
/** /**
* @param resource $filePointer Pointer to the CSV file to read * @param resource $filePointer Pointer to the CSV file to read
* @param string $fieldDelimiter Character that delimits fields * @param \Box\Spout\Reader\CSV\ReaderOptions $options
* @param string $fieldEnclosure Character that enclose fields
* @param string $endOfLineCharacter Character defining the end of a line
* @param string $encoding Encoding of the CSV file to be read
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
*/ */
public function __construct( public function __construct($filePointer, $options, $globalFunctionsHelper)
$filePointer, $fieldDelimiter, $fieldEnclosure,
$endOfLineCharacter, $encoding, $shouldPreserveEmptyRows,
$globalFunctionsHelper)
{ {
$this->rowIterator = new RowIterator( $this->rowIterator = new RowIterator($filePointer, $options, $globalFunctionsHelper);
$filePointer, $fieldDelimiter, $fieldEnclosure,
$endOfLineCharacter, $encoding, $shouldPreserveEmptyRows,
$globalFunctionsHelper
);
} }
/** /**

View File

@ -20,23 +20,12 @@ class SheetIterator implements IteratorInterface
/** /**
* @param resource $filePointer * @param resource $filePointer
* @param string $fieldDelimiter Character that delimits fields * @param \Box\Spout\Reader\CSV\ReaderOptions $options
* @param string $fieldEnclosure Character that enclose fields
* @param string $endOfLineCharacter Character defining the end of a line
* @param string $encoding Encoding of the CSV file to be read
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
*/ */
public function __construct( public function __construct($filePointer, $options, $globalFunctionsHelper)
$filePointer, $fieldDelimiter, $fieldEnclosure,
$endOfLineCharacter, $encoding, $shouldPreserveEmptyRows,
$globalFunctionsHelper)
{ {
$this->sheet = new Sheet( $this->sheet = new Sheet($filePointer, $options, $globalFunctionsHelper);
$filePointer, $fieldDelimiter, $fieldEnclosure,
$endOfLineCharacter, $encoding, $shouldPreserveEmptyRows,
$globalFunctionsHelper
);
} }
/** /**

View File

@ -19,6 +19,19 @@ class Reader extends AbstractReader
/** @var SheetIterator To iterator over the ODS sheets */ /** @var SheetIterator To iterator over the ODS sheets */
protected $sheetIterator; protected $sheetIterator;
/**
* Returns the reader's current options
*
* @return ReaderOptions
*/
protected function getOptions()
{
if (!isset($this->options)) {
$this->options = new ReaderOptions();
}
return $this->options;
}
/** /**
* Returns whether stream wrappers are supported * Returns whether stream wrappers are supported
* *
@ -42,7 +55,7 @@ class Reader extends AbstractReader
$this->zip = new \ZipArchive(); $this->zip = new \ZipArchive();
if ($this->zip->open($filePath) === true) { if ($this->zip->open($filePath) === true) {
$this->sheetIterator = new SheetIterator($filePath, $this->shouldFormatDates, $this->shouldPreserveEmptyRows); $this->sheetIterator = new SheetIterator($filePath, $this->getOptions());
} else { } else {
throw new IOException("Could not open $filePath for reading."); throw new IOException("Could not open $filePath for reading.");
} }

View File

@ -0,0 +1,16 @@
<?php
namespace Box\Spout\Reader\ODS;
use Box\Spout\Reader\ReaderOptionsCommon;
/**
* Class ReaderOptions
* This class is used to customize the reader's behavior
*
* @package Box\Spout\Reader\ODS
*/
class ReaderOptions extends ReaderOptionsCommon
{
// No extra options
}

View File

@ -65,14 +65,13 @@ class RowIterator implements IteratorInterface
/** /**
* @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element * @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings * @param \Box\Spout\Reader\ODS\ReaderOptions $options Reader's current options
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
*/ */
public function __construct($xmlReader, $shouldFormatDates, $shouldPreserveEmptyRows) public function __construct($xmlReader, $options)
{ {
$this->xmlReader = $xmlReader; $this->xmlReader = $xmlReader;
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; $this->shouldPreserveEmptyRows = $options->shouldPreserveEmptyRows();
$this->cellValueFormatter = new CellValueFormatter($shouldFormatDates); $this->cellValueFormatter = new CellValueFormatter($options->shouldFormatDates());
} }
/** /**

View File

@ -27,14 +27,13 @@ class Sheet implements SheetInterface
/** /**
* @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element * @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based) * @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
* @param \Box\Spout\Reader\ODS\ReaderOptions $options Reader's current options
* @param string $sheetName Name of the sheet * @param string $sheetName Name of the sheet
*/ */
public function __construct($xmlReader, $shouldFormatDates, $shouldPreserveEmptyRows, $sheetIndex, $sheetName) public function __construct($xmlReader, $sheetIndex, $sheetName, $options)
{ {
$this->rowIterator = new RowIterator($xmlReader, $shouldFormatDates, $shouldPreserveEmptyRows); $this->rowIterator = new RowIterator($xmlReader, $options);
$this->index = $sheetIndex; $this->index = $sheetIndex;
$this->name = $sheetName; $this->name = $sheetName;
} }

View File

@ -24,11 +24,8 @@ class SheetIterator implements IteratorInterface
/** @var string $filePath Path of the file to be read */ /** @var string $filePath Path of the file to be read */
protected $filePath; protected $filePath;
/** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ /** @var \Box\Spout\Reader\ODS\ReaderOptions Reader's current options */
protected $shouldFormatDates; protected $options;
/** @var bool Whether empty rows should be returned or skipped */
protected $shouldPreserveEmptyRows;
/** @var XMLReader The XMLReader object that will help read sheet's XML data */ /** @var XMLReader The XMLReader object that will help read sheet's XML data */
protected $xmlReader; protected $xmlReader;
@ -44,15 +41,13 @@ class SheetIterator implements IteratorInterface
/** /**
* @param string $filePath Path of the file to be read * @param string $filePath Path of the file to be read
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings * @param \Box\Spout\Reader\ODS\ReaderOptions $options Reader's current options
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
* @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
*/ */
public function __construct($filePath, $shouldFormatDates, $shouldPreserveEmptyRows) public function __construct($filePath, $options)
{ {
$this->filePath = $filePath; $this->filePath = $filePath;
$this->shouldFormatDates = $shouldFormatDates; $this->options = $options;
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows;
$this->xmlReader = new XMLReader(); $this->xmlReader = new XMLReader();
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
@ -121,7 +116,7 @@ class SheetIterator implements IteratorInterface
$escapedSheetName = $this->xmlReader->getAttribute(self::XML_ATTRIBUTE_TABLE_NAME); $escapedSheetName = $this->xmlReader->getAttribute(self::XML_ATTRIBUTE_TABLE_NAME);
$sheetName = $this->escaper->unescape($escapedSheetName); $sheetName = $this->escaper->unescape($escapedSheetName);
return new Sheet($this->xmlReader, $this->shouldFormatDates, $this->shouldPreserveEmptyRows, $sheetName, $this->currentSheetIndex); return new Sheet($this->xmlReader, $sheetName, $this->currentSheetIndex, $this->options);
} }
/** /**

View File

@ -0,0 +1,58 @@
<?php
namespace Box\Spout\Reader;
/**
* Class ReaderOptionsCommon
* Readers' common options
*
* @package Box\Spout\Reader
*/
class ReaderOptionsCommon
{
/** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */
protected $shouldFormatDates = false;
/** @var bool Whether empty rows should be returned or skipped */
protected $shouldPreserveEmptyRows = false;
/**
* @return bool Whether date/time values should be returned as PHP objects or be formatted as strings.
*/
public function shouldFormatDates()
{
return $this->shouldFormatDates;
}
/**
* Sets whether date/time values should be returned as PHP objects or be formatted as strings.
*
* @param bool $shouldFormatDates
* @return ReaderOptionsCommon
*/
public function setShouldFormatDates($shouldFormatDates)
{
$this->shouldFormatDates = $shouldFormatDates;
return $this;
}
/**
* @return bool Whether empty rows should be returned or skipped.
*/
public function shouldPreserveEmptyRows()
{
return $this->shouldPreserveEmptyRows;
}
/**
* Sets whether empty rows should be returned or skipped.
*
* @param bool $shouldPreserveEmptyRows
* @return ReaderOptionsCommon
*/
public function setShouldPreserveEmptyRows($shouldPreserveEmptyRows)
{
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows;
return $this;
}
}

View File

@ -34,7 +34,7 @@ class SharedStringsHelper
/** /**
* @param string $filePath Path of the XLSX file being read * @param string $filePath Path of the XLSX file being read
* @param string|void $tempFolder Temporary folder where the temporary files to store shared strings will be stored * @param string|null|void $tempFolder Temporary folder where the temporary files to store shared strings will be stored
*/ */
public function __construct($filePath, $tempFolder = null) public function __construct($filePath, $tempFolder = null)
{ {

View File

@ -20,32 +20,27 @@ class SheetHelper
/** @var string Path of the XLSX file being read */ /** @var string Path of the XLSX file being read */
protected $filePath; protected $filePath;
/** @var \Box\Spout\Reader\XLSX\ReaderOptions Reader's current options */
protected $options;
/** @var \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings */ /** @var \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings */
protected $sharedStringsHelper; protected $sharedStringsHelper;
/** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
protected $globalFunctionsHelper; protected $globalFunctionsHelper;
/** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */
protected $shouldFormatDates;
/** @var bool Whether empty rows should be returned or skipped */
protected $shouldPreserveEmptyRows;
/** /**
* @param string $filePath Path of the XLSX file being read * @param string $filePath Path of the XLSX file being read
* @param \Box\Spout\Reader\XLSX\ReaderOptions $options Reader's current options
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings * @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
*/ */
public function __construct($filePath, $sharedStringsHelper, $globalFunctionsHelper, $shouldFormatDates, $shouldPreserveEmptyRows) public function __construct($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper)
{ {
$this->filePath = $filePath; $this->filePath = $filePath;
$this->options = $options;
$this->sharedStringsHelper = $sharedStringsHelper; $this->sharedStringsHelper = $sharedStringsHelper;
$this->globalFunctionsHelper = $globalFunctionsHelper; $this->globalFunctionsHelper = $globalFunctionsHelper;
$this->shouldFormatDates = $shouldFormatDates;
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows;
} }
/** /**
@ -97,7 +92,7 @@ class SheetHelper
$sheetDataXMLFilePath = $this->getSheetDataXMLFilePathForSheetId($sheetId); $sheetDataXMLFilePath = $this->getSheetDataXMLFilePathForSheetId($sheetId);
return new Sheet($this->filePath, $sheetDataXMLFilePath, $this->sharedStringsHelper, $this->shouldFormatDates, $this->shouldPreserveEmptyRows, $sheetIndexZeroBased, $sheetName); return new Sheet($this->filePath, $sheetDataXMLFilePath, $sheetIndexZeroBased, $sheetName, $this->options, $this->sharedStringsHelper);
} }
/** /**

View File

@ -14,9 +14,6 @@ use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper;
*/ */
class Reader extends AbstractReader class Reader extends AbstractReader
{ {
/** @var string Temporary folder where the temporary files will be created */
protected $tempFolder;
/** @var \ZipArchive */ /** @var \ZipArchive */
protected $zip; protected $zip;
@ -27,13 +24,26 @@ class Reader extends AbstractReader
protected $sheetIterator; protected $sheetIterator;
/**
* Returns the reader's current options
*
* @return ReaderOptions
*/
protected function getOptions()
{
if (!isset($this->options)) {
$this->options = new ReaderOptions();
}
return $this->options;
}
/** /**
* @param string $tempFolder Temporary folder where the temporary files will be created * @param string $tempFolder Temporary folder where the temporary files will be created
* @return Reader * @return Reader
*/ */
public function setTempFolder($tempFolder) public function setTempFolder($tempFolder)
{ {
$this->tempFolder = $tempFolder; $this->getOptions()->setTempFolder($tempFolder);
return $this; return $this;
} }
@ -62,14 +72,14 @@ class Reader extends AbstractReader
$this->zip = new \ZipArchive(); $this->zip = new \ZipArchive();
if ($this->zip->open($filePath) === true) { if ($this->zip->open($filePath) === true) {
$this->sharedStringsHelper = new SharedStringsHelper($filePath, $this->tempFolder); $this->sharedStringsHelper = new SharedStringsHelper($filePath, $this->getOptions()->getTempFolder());
if ($this->sharedStringsHelper->hasSharedStrings()) { if ($this->sharedStringsHelper->hasSharedStrings()) {
// Extracts all the strings from the sheets for easy access in the future // Extracts all the strings from the sheets for easy access in the future
$this->sharedStringsHelper->extractSharedStrings(); $this->sharedStringsHelper->extractSharedStrings();
} }
$this->sheetIterator = new SheetIterator($filePath, $this->sharedStringsHelper, $this->globalFunctionsHelper, $this->shouldFormatDates, $this->shouldPreserveEmptyRows); $this->sheetIterator = new SheetIterator($filePath, $this->getOptions(), $this->sharedStringsHelper, $this->globalFunctionsHelper);
} else { } else {
throw new IOException("Could not open $filePath for reading."); throw new IOException("Could not open $filePath for reading.");
} }

View File

@ -0,0 +1,35 @@
<?php
namespace Box\Spout\Reader\XLSX;
use Box\Spout\Reader\ReaderOptionsCommon;
/**
* Class ReaderOptions
* This class is used to customize the reader's behavior
*
* @package Box\Spout\Reader\XLSX
*/
class ReaderOptions extends ReaderOptionsCommon
{
/** @var string|null Temporary folder where the temporary files will be created */
protected $tempFolder = null;
/**
* @return string|null Temporary folder where the temporary files will be created
*/
public function getTempFolder()
{
return $this->tempFolder;
}
/**
* @param string|null $tempFolder Temporary folder where the temporary files will be created
* @return ReaderOptions
*/
public function setTempFolder($tempFolder)
{
$this->tempFolder = $tempFolder;
return $this;
}
}

View File

@ -74,11 +74,10 @@ class RowIterator implements IteratorInterface
/** /**
* @param string $filePath Path of the XLSX file being read * @param string $filePath Path of the XLSX file being read
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
* @param \Box\Spout\Reader\XLSX\ReaderOptions $options Reader's current options
* @param Helper\SharedStringsHelper $sharedStringsHelper Helper to work with shared strings * @param Helper\SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
*/ */
public function __construct($filePath, $sheetDataXMLFilePath, $sharedStringsHelper, $shouldFormatDates, $shouldPreserveEmptyRows) public function __construct($filePath, $sheetDataXMLFilePath, $options, $sharedStringsHelper)
{ {
$this->filePath = $filePath; $this->filePath = $filePath;
$this->sheetDataXMLFilePath = $this->normalizeSheetDataXMLFilePath($sheetDataXMLFilePath); $this->sheetDataXMLFilePath = $this->normalizeSheetDataXMLFilePath($sheetDataXMLFilePath);
@ -86,9 +85,9 @@ class RowIterator implements IteratorInterface
$this->xmlReader = new XMLReader(); $this->xmlReader = new XMLReader();
$this->styleHelper = new StyleHelper($filePath); $this->styleHelper = new StyleHelper($filePath);
$this->cellValueFormatter = new CellValueFormatter($sharedStringsHelper, $this->styleHelper, $shouldFormatDates); $this->cellValueFormatter = new CellValueFormatter($sharedStringsHelper, $this->styleHelper, $options->shouldFormatDates());
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; $this->shouldPreserveEmptyRows = $options->shouldPreserveEmptyRows();
} }
/** /**

View File

@ -24,15 +24,14 @@ class Sheet implements SheetInterface
/** /**
* @param string $filePath Path of the XLSX file being read * @param string $filePath Path of the XLSX file being read
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
* @param Helper\SharedStringsHelper Helper to work with shared strings
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based) * @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
* @param string $sheetName Name of the sheet * @param string $sheetName Name of the sheet
* @param \Box\Spout\Reader\XLSX\ReaderOptions $options Reader's current options
* @param Helper\SharedStringsHelper Helper to work with shared strings
*/ */
public function __construct($filePath, $sheetDataXMLFilePath, $sharedStringsHelper, $shouldFormatDates, $shouldPreserveEmptyRows, $sheetIndex, $sheetName) public function __construct($filePath, $sheetDataXMLFilePath, $sheetIndex, $sheetName, $options, $sharedStringsHelper)
{ {
$this->rowIterator = new RowIterator($filePath, $sheetDataXMLFilePath, $sharedStringsHelper, $shouldFormatDates, $shouldPreserveEmptyRows); $this->rowIterator = new RowIterator($filePath, $sheetDataXMLFilePath, $options, $sharedStringsHelper);
$this->index = $sheetIndex; $this->index = $sheetIndex;
$this->name = $sheetName; $this->name = $sheetName;
} }

View File

@ -22,16 +22,15 @@ class SheetIterator implements IteratorInterface
/** /**
* @param string $filePath Path of the file to be read * @param string $filePath Path of the file to be read
* @param \Box\Spout\Reader\XLSX\ReaderOptions $options Reader's current options
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper $sharedStringsHelper * @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper $sharedStringsHelper
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings
* @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped
* @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
*/ */
public function __construct($filePath, $sharedStringsHelper, $globalFunctionsHelper, $shouldFormatDates, $shouldPreserveEmptyRows) public function __construct($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper)
{ {
// Fetch all available sheets // Fetch all available sheets
$sheetHelper = new SheetHelper($filePath, $sharedStringsHelper, $globalFunctionsHelper, $shouldFormatDates, $shouldPreserveEmptyRows); $sheetHelper = new SheetHelper($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper);
$this->sheets = $sheetHelper->getSheets(); $this->sheets = $sheetHelper->getSheets();
if (count($this->sheets) === 0) { if (count($this->sheets) === 0) {