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:
Adrien Loison authored and GitHub committed 2017-05-24 13:17:50 +02:00
1 parent f9d8ad8be3
commit a366d0d0af
22 files changed
+447 -113

No files matched your search

+2 -1
View File
@@ -64,7 +64,7 @@ class Cell
}
/**
* @param $value mixed
* @param $value mixed|null
*/
public function setValue($value)
{
@@ -90,6 +90,7 @@ class Cell
/**
* Get the current value type
* @param mixed|null $value
* @return int
*/
protected function detectType($value)
@@ -2,6 +2,8 @@
namespace Box\Spout\Writer\Common\Internal;
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Options;
use Box\Spout\Writer\Exception\SheetNotFoundException;
/**
@@ -13,8 +15,8 @@ use Box\Spout\Writer\Exception\SheetNotFoundException;
*/
abstract class AbstractWorkbook implements WorkbookInterface
{
/** @var bool Whether new sheets should be automatically created when the max rows limit per sheet is reached */
protected $shouldCreateNewSheetsAutomatically;
/** @var \Box\Spout\Writer\Common\Manager\OptionsManagerInterface $optionsManager */
protected $optionManager;
/** @var string Timestamp based unique ID identifying the workbook */
protected $internalId;
@@ -26,13 +28,12 @@ abstract class AbstractWorkbook implements WorkbookInterface
protected $currentWorksheet;
/**
* @param bool $shouldCreateNewSheetsAutomatically
* @param \Box\Spout\Writer\Style\Style $defaultRowStyle
* @param \Box\Spout\Writer\Common\Manager\OptionsManagerInterface $optionsManager
* @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders
*/
public function __construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle)
public function __construct(OptionsManagerInterface $optionsManager)
{
$this->shouldCreateNewSheetsAutomatically = $shouldCreateNewSheetsAutomatically;
$this->optionManager = $optionsManager;
$this->internalId = uniqid();
}
@@ -155,7 +156,7 @@ abstract class AbstractWorkbook implements WorkbookInterface
// if we reached the maximum number of rows for the current sheet...
if ($hasReachedMaxRows) {
// ... continue writing in a new sheet if option set
if ($this->shouldCreateNewSheetsAutomatically) {
if ($this->optionManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) {
$currentWorksheet = $this->addNewSheetAndMakeItCurrent();
$updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, $dataRow);
@@ -0,0 +1,71 @@
<?php
namespace Box\Spout\Writer\Common\Manager;
/**
* Class OptionsManager
* Writer' options manager
*
* @package Box\Spout\Writer\Common\Manager
*/
abstract class OptionsManager implements OptionsManagerInterface
{
const PREFIX_OPTION = 'OPTION_';
/** @var string[] List of all supported option names */
private $supportedOptions = [];
/** @var array Associative array [OPTION_NAME => OPTION_VALUE] */
private $options = [];
/**
* WriterOptions constructor.
*/
public function __construct()
{
$this->supportedOptions = $this->getSupportedOptions();
$this->setDefaultOptions();
}
/**
* @return array List of supported options
*/
abstract protected function getSupportedOptions();
/**
* Sets the default options.
* To be overriden by child classes
*
* @return void
*/
abstract protected function setDefaultOptions();
/**
* Sets the given option, if this option is supported.
*
* @param string $optionName
* @param mixed $optionValue
* @return void
*/
public function setOption($optionName, $optionValue)
{
if (in_array($optionName, $this->supportedOptions)) {
$this->options[$optionName] = $optionValue;
}
}
/**
* @param string $optionName
* @return mixed|null The set option or NULL if no option with given name found
*/
public function getOption($optionName)
{
$optionValue = null;
if (isset($this->options[$optionName])) {
$optionValue = $this->options[$optionName];
}
return $optionValue;
}
}
@@ -0,0 +1,25 @@
<?php
namespace Box\Spout\Writer\Common\Manager;
/**
* Interface OptionsManagerInterface
* Writer' options interface
*
* @package Box\Spout\Writer\Common\Manager
*/
interface OptionsManagerInterface
{
/**
* @param string $optionName
* @param mixed $optionValue
* @return void
*/
public function setOption($optionName, $optionValue);
/**
* @param string $optionName
* @return mixed|null The set option or NULL if no option with given name found
*/
public function getOption($optionName);
}
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace Box\Spout\Writer\Common;
/**
* Class Options
* Writer' options holder
*
* @package Box\Spout\Writer\Common
*/
abstract class Options
{
// CSV specific options
const FIELD_DELIMITER = 'fieldDelimiter';
const FIELD_ENCLOSURE = 'fieldEnclosure';
const SHOULD_ADD_BOM = 'shouldAddBOM';
// Multisheets options
const TEMP_FOLDER = 'tempFolder';
const DEFAULT_ROW_STYLE = 'defaultRowStyle';
const SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY = 'shouldCreateNewSheetsAutomatically';
// XLSX specific options
const SHOULD_USE_INLINE_STRINGS = 'shouldUseInlineStrings';
}