Add ReaderOptions for all readers (#338)

Instead of passing every single option down the chain
This commit is contained in:
Adrien Loison authored and GitHub committed 2016-10-17 22:41:36 -07:00
1 parent b61323d7d2
commit 752f4bf64e
19 files changed
+332 -130

No files matched your search

+21 -25
View File
@@ -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);
}
}
+111
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;
}
}
+13 -17
View File
@@ -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);
+3 -14
View File
@@ -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);
}
/**
+3 -14
View File
@@ -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);
}
/**