Introduce an options manager for all writers (#423)
This will improve the management of options and simplify some methods' signatures. This commit will also help moving the code to a better state regarding Dependency Injection.
This commit is contained in:
1 parent
f9d8ad8be3
commit
a366d0d0af
22 files changed
+447
-113
No files matched your search
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\CSV\Manager;
|
||||
|
||||
use Box\Spout\Writer\Common\Options;
|
||||
|
||||
/**
|
||||
* Class OptionsManager
|
||||
* CSV Writer options manager
|
||||
*
|
||||
* @package Box\Spout\Writer\CSV\Manager
|
||||
*/
|
||||
class OptionsManager extends \Box\Spout\Writer\Common\Manager\OptionsManager
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getSupportedOptions()
|
||||
{
|
||||
return [
|
||||
Options::FIELD_DELIMITER,
|
||||
Options::FIELD_ENCLOSURE,
|
||||
Options::SHOULD_ADD_BOM,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setDefaultOptions()
|
||||
{
|
||||
$this->setOption(Options::FIELD_DELIMITER, ',');
|
||||
$this->setOption(Options::FIELD_ENCLOSURE, '"');
|
||||
$this->setOption(Options::SHOULD_ADD_BOM, true);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\CSV;
|
||||
use Box\Spout\Writer\AbstractWriter;
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Helper\EncodingHelper;
|
||||
use Box\Spout\Writer\Common\Options;
|
||||
|
||||
/**
|
||||
* Class Writer
|
||||
@@ -20,18 +21,9 @@ class Writer extends AbstractWriter
|
||||
/** @var string Content-Type value for the header */
|
||||
protected static $headerContentType = 'text/csv; charset=UTF-8';
|
||||
|
||||
/** @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 int */
|
||||
protected $lastWrittenRowIndex = 0;
|
||||
|
||||
/** @var bool */
|
||||
protected $shouldAddBOM = true;
|
||||
|
||||
/**
|
||||
* Sets the field delimiter for the CSV
|
||||
*
|
||||
@@ -41,7 +33,7 @@ class Writer extends AbstractWriter
|
||||
*/
|
||||
public function setFieldDelimiter($fieldDelimiter)
|
||||
{
|
||||
$this->fieldDelimiter = $fieldDelimiter;
|
||||
$this->optionsManager->setOption(Options::FIELD_DELIMITER, $fieldDelimiter);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -54,19 +46,20 @@ class Writer extends AbstractWriter
|
||||
*/
|
||||
public function setFieldEnclosure($fieldEnclosure)
|
||||
{
|
||||
$this->fieldEnclosure = $fieldEnclosure;
|
||||
$this->optionsManager->setOption(Options::FIELD_ENCLOSURE, $fieldEnclosure);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if a BOM has to be added to the file
|
||||
*
|
||||
* @api
|
||||
* @param bool $shouldAddBOM
|
||||
* @return Writer
|
||||
*/
|
||||
public function setShouldAddBOM($shouldAddBOM)
|
||||
{
|
||||
$this->shouldAddBOM = (bool) $shouldAddBOM;
|
||||
$this->optionsManager->setOption(Options::SHOULD_ADD_BOM, (bool) $shouldAddBOM);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -77,7 +70,7 @@ class Writer extends AbstractWriter
|
||||
*/
|
||||
protected function openWriter()
|
||||
{
|
||||
if ($this->shouldAddBOM) {
|
||||
if ($this->optionsManager->getOption(Options::SHOULD_ADD_BOM)) {
|
||||
// Adds UTF-8 BOM for Unicode compatibility
|
||||
$this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8);
|
||||
}
|
||||
@@ -94,7 +87,10 @@ class Writer extends AbstractWriter
|
||||
*/
|
||||
protected function addRowToWriter(array $dataRow, $style)
|
||||
{
|
||||
$wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $dataRow, $this->fieldDelimiter, $this->fieldEnclosure);
|
||||
$fieldDelimiter = $this->optionsManager->getOption(Options::FIELD_DELIMITER);
|
||||
$fieldEnclosure = $this->optionsManager->getOption(Options::FIELD_ENCLOSURE);
|
||||
|
||||
$wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $dataRow, $fieldDelimiter, $fieldEnclosure);
|
||||
if ($wasWriteSuccessful === false) {
|
||||
throw new IOException('Unable to write data');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user