From 4e3330399053a2a5b71619848275e7f7a19d555f Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Mon, 17 Oct 2016 22:09:20 -0700 Subject: [PATCH] Add ReaderOptions for all readers Instead of passing every single option down the chain --- src/Spout/Reader/AbstractReader.php | 16 ++- src/Spout/Reader/CSV/Reader.php | 46 ++++---- src/Spout/Reader/CSV/ReaderOptions.php | 111 ++++++++++++++++++ src/Spout/Reader/CSV/RowIterator.php | 30 ++--- src/Spout/Reader/CSV/Sheet.php | 17 +-- src/Spout/Reader/CSV/SheetIterator.php | 17 +-- src/Spout/Reader/ODS/Reader.php | 15 ++- src/Spout/Reader/ODS/ReaderOptions.php | 16 +++ src/Spout/Reader/ODS/RowIterator.php | 9 +- src/Spout/Reader/ODS/Sheet.php | 7 +- src/Spout/Reader/ODS/SheetIterator.php | 17 +-- src/Spout/Reader/ReaderOptionsCommon.php | 58 +++++++++ .../XLSX/Helper/SharedStringsHelper.php | 2 +- src/Spout/Reader/XLSX/Helper/SheetHelper.php | 19 ++- src/Spout/Reader/XLSX/Reader.php | 22 +++- src/Spout/Reader/XLSX/ReaderOptions.php | 35 ++++++ src/Spout/Reader/XLSX/RowIterator.php | 9 +- src/Spout/Reader/XLSX/Sheet.php | 9 +- src/Spout/Reader/XLSX/SheetIterator.php | 7 +- 19 files changed, 332 insertions(+), 130 deletions(-) create mode 100644 src/Spout/Reader/CSV/ReaderOptions.php create mode 100644 src/Spout/Reader/ODS/ReaderOptions.php create mode 100644 src/Spout/Reader/ReaderOptionsCommon.php create mode 100644 src/Spout/Reader/XLSX/ReaderOptions.php diff --git a/src/Spout/Reader/AbstractReader.php b/src/Spout/Reader/AbstractReader.php index ddf5032..0708ce8 100644 --- a/src/Spout/Reader/AbstractReader.php +++ b/src/Spout/Reader/AbstractReader.php @@ -19,11 +19,15 @@ abstract class AbstractReader implements ReaderInterface /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ protected $globalFunctionsHelper; - /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ - protected $shouldFormatDates = false; + /** @var \Box\Spout\Reader\ReaderOptionsCommon Reader's customized options */ + 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 @@ -73,7 +77,7 @@ abstract class AbstractReader implements ReaderInterface */ public function setShouldFormatDates($shouldFormatDates) { - $this->shouldFormatDates = $shouldFormatDates; + $this->getOptions()->setShouldFormatDates($shouldFormatDates); return $this; } @@ -86,7 +90,7 @@ abstract class AbstractReader implements ReaderInterface */ public function setShouldPreserveEmptyRows($shouldPreserveEmptyRows) { - $this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; + $this->getOptions()->setShouldPreserveEmptyRows($shouldPreserveEmptyRows); return $this; } diff --git a/src/Spout/Reader/CSV/Reader.php b/src/Spout/Reader/CSV/Reader.php index cc5b95d..648a12d 100644 --- a/src/Spout/Reader/CSV/Reader.php +++ b/src/Spout/Reader/CSV/Reader.php @@ -4,7 +4,6 @@ namespace Box\Spout\Reader\CSV; use Box\Spout\Reader\AbstractReader; use Box\Spout\Common\Exception\IOException; -use Box\Spout\Common\Helper\EncodingHelper; /** * Class Reader @@ -20,20 +19,21 @@ class Reader extends AbstractReader /** @var SheetIterator To iterator over the CSV unique "sheet" */ protected $sheetIterator; - /** @var string Defines the character used to delimit fields (one character only) */ - protected $fieldDelimiter = ','; + /** @var string Original value for the "auto_detect_line_endings" INI value */ + protected $originalAutoDetectLineEndings; - /** @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"; - - /** @var string */ - protected $autoDetectLineEndings; + /** + * Returns the reader's current options + * + * @return ReaderOptions + */ + protected function getOptions() + { + if (!isset($this->options)) { + $this->options = new ReaderOptions(); + } + return $this->options; + } /** * Sets the field delimiter for the CSV. @@ -44,7 +44,7 @@ class Reader extends AbstractReader */ public function setFieldDelimiter($fieldDelimiter) { - $this->fieldDelimiter = $fieldDelimiter; + $this->getOptions()->setFieldDelimiter($fieldDelimiter); return $this; } @@ -57,7 +57,7 @@ class Reader extends AbstractReader */ public function setFieldEnclosure($fieldEnclosure) { - $this->fieldEnclosure = $fieldEnclosure; + $this->getOptions()->setFieldEnclosure($fieldEnclosure); return $this; } @@ -70,7 +70,7 @@ class Reader extends AbstractReader */ public function setEncoding($encoding) { - $this->encoding = $encoding; + $this->getOptions()->setEncoding($encoding); return $this; } @@ -83,7 +83,7 @@ class Reader extends AbstractReader */ public function setEndOfLineCharacter($endOfLineCharacter) { - $this->endOfLineCharacter = $endOfLineCharacter; + $this->getOptions()->setEndOfLineCharacter($endOfLineCharacter); return $this; } @@ -107,7 +107,7 @@ class Reader extends AbstractReader */ 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'); $this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r'); @@ -117,11 +117,7 @@ class Reader extends AbstractReader $this->sheetIterator = new SheetIterator( $this->filePointer, - $this->fieldDelimiter, - $this->fieldEnclosure, - $this->endOfLineCharacter, - $this->encoding, - $this->shouldPreserveEmptyRows, + $this->getOptions(), $this->globalFunctionsHelper ); } @@ -148,6 +144,6 @@ class Reader extends AbstractReader $this->globalFunctionsHelper->fclose($this->filePointer); } - ini_set('auto_detect_line_endings', $this->autoDetectLineEndings); + ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings); } } diff --git a/src/Spout/Reader/CSV/ReaderOptions.php b/src/Spout/Reader/CSV/ReaderOptions.php new file mode 100644 index 0000000..7d03f63 --- /dev/null +++ b/src/Spout/Reader/CSV/ReaderOptions.php @@ -0,0 +1,111 @@ +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; + } +} diff --git a/src/Spout/Reader/CSV/RowIterator.php b/src/Spout/Reader/CSV/RowIterator.php index b805126..7aa29ab 100644 --- a/src/Spout/Reader/CSV/RowIterator.php +++ b/src/Spout/Reader/CSV/RowIterator.php @@ -40,6 +40,12 @@ class RowIterator implements IteratorInterface /** @var string Encoding of the CSV file to be read */ 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 */ protected $globalFunctionsHelper; @@ -49,29 +55,19 @@ class RowIterator implements IteratorInterface /** @var string End of line delimiter, encoded using the same encoding as the CSV */ 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 string $fieldDelimiter Character that delimits fields - * @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\Reader\CSV\ReaderOptions $options * @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->fieldDelimiter = $fieldDelimiter; - $this->fieldEnclosure = $fieldEnclosure; - $this->encoding = $encoding; - $this->inputEOLDelimiter = $endOfLineDelimiter; - $this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; + $this->fieldDelimiter = $options->getFieldDelimiter(); + $this->fieldEnclosure = $options->getFieldEnclosure(); + $this->encoding = $options->getEncoding(); + $this->inputEOLDelimiter = $options->getEndOfLineCharacter(); + $this->shouldPreserveEmptyRows = $options->shouldPreserveEmptyRows(); $this->globalFunctionsHelper = $globalFunctionsHelper; $this->encodingHelper = new EncodingHelper($globalFunctionsHelper); diff --git a/src/Spout/Reader/CSV/Sheet.php b/src/Spout/Reader/CSV/Sheet.php index 98dcc7c..baac559 100644 --- a/src/Spout/Reader/CSV/Sheet.php +++ b/src/Spout/Reader/CSV/Sheet.php @@ -16,23 +16,12 @@ class Sheet implements SheetInterface /** * @param resource $filePointer Pointer to the CSV file to read - * @param string $fieldDelimiter Character that delimits fields - * @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\Reader\CSV\ReaderOptions $options * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper */ - public function __construct( - $filePointer, $fieldDelimiter, $fieldEnclosure, - $endOfLineCharacter, $encoding, $shouldPreserveEmptyRows, - $globalFunctionsHelper) + public function __construct($filePointer, $options, $globalFunctionsHelper) { - $this->rowIterator = new RowIterator( - $filePointer, $fieldDelimiter, $fieldEnclosure, - $endOfLineCharacter, $encoding, $shouldPreserveEmptyRows, - $globalFunctionsHelper - ); + $this->rowIterator = new RowIterator($filePointer, $options, $globalFunctionsHelper); } /** diff --git a/src/Spout/Reader/CSV/SheetIterator.php b/src/Spout/Reader/CSV/SheetIterator.php index 2003599..58a9480 100644 --- a/src/Spout/Reader/CSV/SheetIterator.php +++ b/src/Spout/Reader/CSV/SheetIterator.php @@ -20,23 +20,12 @@ class SheetIterator implements IteratorInterface /** * @param resource $filePointer - * @param string $fieldDelimiter Character that delimits fields - * @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\Reader\CSV\ReaderOptions $options * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper */ - public function __construct( - $filePointer, $fieldDelimiter, $fieldEnclosure, - $endOfLineCharacter, $encoding, $shouldPreserveEmptyRows, - $globalFunctionsHelper) + public function __construct($filePointer, $options, $globalFunctionsHelper) { - $this->sheet = new Sheet( - $filePointer, $fieldDelimiter, $fieldEnclosure, - $endOfLineCharacter, $encoding, $shouldPreserveEmptyRows, - $globalFunctionsHelper - ); + $this->sheet = new Sheet($filePointer, $options, $globalFunctionsHelper); } /** diff --git a/src/Spout/Reader/ODS/Reader.php b/src/Spout/Reader/ODS/Reader.php index 6ce92be..dbdc47b 100644 --- a/src/Spout/Reader/ODS/Reader.php +++ b/src/Spout/Reader/ODS/Reader.php @@ -19,6 +19,19 @@ class Reader extends AbstractReader /** @var SheetIterator To iterator over the ODS sheets */ 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 * @@ -42,7 +55,7 @@ class Reader extends AbstractReader $this->zip = new \ZipArchive(); if ($this->zip->open($filePath) === true) { - $this->sheetIterator = new SheetIterator($filePath, $this->shouldFormatDates, $this->shouldPreserveEmptyRows); + $this->sheetIterator = new SheetIterator($filePath, $this->getOptions()); } else { throw new IOException("Could not open $filePath for reading."); } diff --git a/src/Spout/Reader/ODS/ReaderOptions.php b/src/Spout/Reader/ODS/ReaderOptions.php new file mode 100644 index 0000000..84061d7 --- /dev/null +++ b/src/Spout/Reader/ODS/ReaderOptions.php @@ -0,0 +1,16 @@ +" 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 \Box\Spout\Reader\ODS\ReaderOptions $options Reader's current options */ - public function __construct($xmlReader, $shouldFormatDates, $shouldPreserveEmptyRows) + public function __construct($xmlReader, $options) { $this->xmlReader = $xmlReader; - $this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; - $this->cellValueFormatter = new CellValueFormatter($shouldFormatDates); + $this->shouldPreserveEmptyRows = $options->shouldPreserveEmptyRows(); + $this->cellValueFormatter = new CellValueFormatter($options->shouldFormatDates()); } /** diff --git a/src/Spout/Reader/ODS/Sheet.php b/src/Spout/Reader/ODS/Sheet.php index 91669e0..fe48dd2 100644 --- a/src/Spout/Reader/ODS/Sheet.php +++ b/src/Spout/Reader/ODS/Sheet.php @@ -27,14 +27,13 @@ class Sheet implements SheetInterface /** * @param XMLReader $xmlReader XML Reader, positioned on the "" 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 \Box\Spout\Reader\ODS\ReaderOptions $options Reader's current options * @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->name = $sheetName; } diff --git a/src/Spout/Reader/ODS/SheetIterator.php b/src/Spout/Reader/ODS/SheetIterator.php index 2c1cafa..c23f844 100644 --- a/src/Spout/Reader/ODS/SheetIterator.php +++ b/src/Spout/Reader/ODS/SheetIterator.php @@ -24,11 +24,8 @@ class SheetIterator implements IteratorInterface /** @var string $filePath Path of the file to be read */ protected $filePath; - /** @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; + /** @var \Box\Spout\Reader\ODS\ReaderOptions Reader's current options */ + protected $options; /** @var XMLReader The XMLReader object that will help read sheet's XML data */ protected $xmlReader; @@ -44,15 +41,13 @@ class SheetIterator implements IteratorInterface /** * @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 bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped + * @param \Box\Spout\Reader\ODS\ReaderOptions $options Reader's current options * @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->shouldFormatDates = $shouldFormatDates; - $this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; + $this->options = $options; $this->xmlReader = new XMLReader(); /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ @@ -121,7 +116,7 @@ class SheetIterator implements IteratorInterface $escapedSheetName = $this->xmlReader->getAttribute(self::XML_ATTRIBUTE_TABLE_NAME); $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); } /** diff --git a/src/Spout/Reader/ReaderOptionsCommon.php b/src/Spout/Reader/ReaderOptionsCommon.php new file mode 100644 index 0000000..05f8fcc --- /dev/null +++ b/src/Spout/Reader/ReaderOptionsCommon.php @@ -0,0 +1,58 @@ +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; + } +} diff --git a/src/Spout/Reader/XLSX/Helper/SharedStringsHelper.php b/src/Spout/Reader/XLSX/Helper/SharedStringsHelper.php index 0f41e90..70519e8 100644 --- a/src/Spout/Reader/XLSX/Helper/SharedStringsHelper.php +++ b/src/Spout/Reader/XLSX/Helper/SharedStringsHelper.php @@ -34,7 +34,7 @@ class SharedStringsHelper /** * @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) { diff --git a/src/Spout/Reader/XLSX/Helper/SheetHelper.php b/src/Spout/Reader/XLSX/Helper/SheetHelper.php index d69fef2..abb3b0a 100644 --- a/src/Spout/Reader/XLSX/Helper/SheetHelper.php +++ b/src/Spout/Reader/XLSX/Helper/SheetHelper.php @@ -20,32 +20,27 @@ class SheetHelper /** @var string Path of the XLSX file being read */ 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 */ protected $sharedStringsHelper; /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ 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 \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\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->options = $options; $this->sharedStringsHelper = $sharedStringsHelper; $this->globalFunctionsHelper = $globalFunctionsHelper; - $this->shouldFormatDates = $shouldFormatDates; - $this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; } /** @@ -97,7 +92,7 @@ class SheetHelper $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); } /** diff --git a/src/Spout/Reader/XLSX/Reader.php b/src/Spout/Reader/XLSX/Reader.php index 0609aa4..76e8e32 100644 --- a/src/Spout/Reader/XLSX/Reader.php +++ b/src/Spout/Reader/XLSX/Reader.php @@ -14,9 +14,6 @@ use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper; */ class Reader extends AbstractReader { - /** @var string Temporary folder where the temporary files will be created */ - protected $tempFolder; - /** @var \ZipArchive */ protected $zip; @@ -27,13 +24,26 @@ class Reader extends AbstractReader 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 * @return Reader */ public function setTempFolder($tempFolder) { - $this->tempFolder = $tempFolder; + $this->getOptions()->setTempFolder($tempFolder); return $this; } @@ -62,14 +72,14 @@ class Reader extends AbstractReader $this->zip = new \ZipArchive(); 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()) { // Extracts all the strings from the sheets for easy access in the future $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 { throw new IOException("Could not open $filePath for reading."); } diff --git a/src/Spout/Reader/XLSX/ReaderOptions.php b/src/Spout/Reader/XLSX/ReaderOptions.php new file mode 100644 index 0000000..35321c4 --- /dev/null +++ b/src/Spout/Reader/XLSX/ReaderOptions.php @@ -0,0 +1,35 @@ +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; + } +} diff --git a/src/Spout/Reader/XLSX/RowIterator.php b/src/Spout/Reader/XLSX/RowIterator.php index e9ff507..50c4402 100644 --- a/src/Spout/Reader/XLSX/RowIterator.php +++ b/src/Spout/Reader/XLSX/RowIterator.php @@ -74,11 +74,10 @@ class RowIterator implements IteratorInterface /** * @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 \Box\Spout\Reader\XLSX\ReaderOptions $options Reader's current options * @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->sheetDataXMLFilePath = $this->normalizeSheetDataXMLFilePath($sheetDataXMLFilePath); @@ -86,9 +85,9 @@ class RowIterator implements IteratorInterface $this->xmlReader = new XMLReader(); $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(); } /** diff --git a/src/Spout/Reader/XLSX/Sheet.php b/src/Spout/Reader/XLSX/Sheet.php index b2405ae..32d1ea6 100644 --- a/src/Spout/Reader/XLSX/Sheet.php +++ b/src/Spout/Reader/XLSX/Sheet.php @@ -24,15 +24,14 @@ class Sheet implements SheetInterface /** * @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 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 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->name = $sheetName; } diff --git a/src/Spout/Reader/XLSX/SheetIterator.php b/src/Spout/Reader/XLSX/SheetIterator.php index 88cd350..7ba07d3 100644 --- a/src/Spout/Reader/XLSX/SheetIterator.php +++ b/src/Spout/Reader/XLSX/SheetIterator.php @@ -22,16 +22,15 @@ class SheetIterator implements IteratorInterface /** * @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\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 */ - public function __construct($filePath, $sharedStringsHelper, $globalFunctionsHelper, $shouldFormatDates, $shouldPreserveEmptyRows) + public function __construct($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper) { // Fetch all available sheets - $sheetHelper = new SheetHelper($filePath, $sharedStringsHelper, $globalFunctionsHelper, $shouldFormatDates, $shouldPreserveEmptyRows); + $sheetHelper = new SheetHelper($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper); $this->sheets = $sheetHelper->getSheets(); if (count($this->sheets) === 0) {