diff --git a/src/Spout/Writer/AbstractMultiSheetsWriter.php b/src/Spout/Writer/AbstractMultiSheetsWriter.php index d119660..b7d7d1b 100644 --- a/src/Spout/Writer/AbstractMultiSheetsWriter.php +++ b/src/Spout/Writer/AbstractMultiSheetsWriter.php @@ -2,6 +2,7 @@ namespace Box\Spout\Writer; +use Box\Spout\Writer\Common\Options; use Box\Spout\Writer\Exception\WriterNotOpenedException; /** @@ -12,9 +13,6 @@ use Box\Spout\Writer\Exception\WriterNotOpenedException; */ abstract class AbstractMultiSheetsWriter extends AbstractWriter { - /** @var bool Whether new sheets should be automatically created when the max rows limit per sheet is reached */ - protected $shouldCreateNewSheetsAutomatically = true; - /** * @return Common\Internal\WorkbookInterface The workbook representing the file to be written */ @@ -33,7 +31,7 @@ abstract class AbstractMultiSheetsWriter extends AbstractWriter { $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); - $this->shouldCreateNewSheetsAutomatically = $shouldCreateNewSheetsAutomatically; + $this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, $shouldCreateNewSheetsAutomatically); return $this; } diff --git a/src/Spout/Writer/AbstractWriter.php b/src/Spout/Writer/AbstractWriter.php index 83e190f..7f7a7a3 100644 --- a/src/Spout/Writer/AbstractWriter.php +++ b/src/Spout/Writer/AbstractWriter.php @@ -6,9 +6,10 @@ use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\SpoutException; use Box\Spout\Common\Helper\FileSystemHelper; +use Box\Spout\Writer\Common\Manager\OptionsManagerInterface; +use Box\Spout\Writer\Common\Options; use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException; -use Box\Spout\Writer\Style\StyleBuilder; /** * Class AbstractWriter @@ -30,12 +31,12 @@ abstract class AbstractWriter implements WriterInterface /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ protected $globalFunctionsHelper; + /** @var \Box\Spout\Writer\Common\Manager\OptionsManagerInterface Writer options manager */ + protected $optionsManager; + /** @var Style\Style Style to be applied to the next written row(s) */ protected $rowStyle; - /** @var Style\Style Default row style. Each writer can have its own default style */ - protected $defaultRowStyle; - /** @var string Content-Type value for the header - to be defined by child class */ protected static $headerContentType; @@ -65,11 +66,11 @@ abstract class AbstractWriter implements WriterInterface abstract protected function closeWriter(); /** - * + * @param \Box\Spout\Writer\Common\Manager\OptionsManagerInterface $optionsManager */ - public function __construct() + public function __construct(OptionsManagerInterface $optionsManager) { - $this->defaultRowStyle = $this->getDefaultRowStyle(); + $this->optionsManager = $optionsManager; $this->resetRowStyleToDefault(); } @@ -83,7 +84,7 @@ abstract class AbstractWriter implements WriterInterface */ public function setDefaultRowStyle($defaultStyle) { - $this->defaultRowStyle = $defaultStyle; + $this->optionsManager->setOption(Options::DEFAULT_ROW_STYLE, $defaultStyle); $this->resetRowStyleToDefault(); return $this; } @@ -307,17 +308,6 @@ abstract class AbstractWriter implements WriterInterface return $this; } - /** - * Returns the default style to be applied to rows. - * Can be overriden by children to have a custom style. - * - * @return Style\Style - */ - protected function getDefaultRowStyle() - { - return (new StyleBuilder())->build(); - } - /** * Sets the style to be applied to the next written rows * until it is changed or reset. @@ -328,7 +318,8 @@ abstract class AbstractWriter implements WriterInterface private function setRowStyle($style) { // Merge given style with the default one to inherit custom properties - $this->rowStyle = $style->mergeWith($this->defaultRowStyle); + $defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE); + $this->rowStyle = $style->mergeWith($defaultRowStyle); } /** @@ -338,7 +329,7 @@ abstract class AbstractWriter implements WriterInterface */ private function resetRowStyleToDefault() { - $this->rowStyle = $this->defaultRowStyle; + $this->rowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE); } /** diff --git a/src/Spout/Writer/CSV/Manager/OptionsManager.php b/src/Spout/Writer/CSV/Manager/OptionsManager.php new file mode 100644 index 0000000..a9cbfab --- /dev/null +++ b/src/Spout/Writer/CSV/Manager/OptionsManager.php @@ -0,0 +1,36 @@ +setOption(Options::FIELD_DELIMITER, ','); + $this->setOption(Options::FIELD_ENCLOSURE, '"'); + $this->setOption(Options::SHOULD_ADD_BOM, true); + } +} \ No newline at end of file diff --git a/src/Spout/Writer/CSV/Writer.php b/src/Spout/Writer/CSV/Writer.php index f7f1fda..1c8b7a0 100644 --- a/src/Spout/Writer/CSV/Writer.php +++ b/src/Spout/Writer/CSV/Writer.php @@ -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'); } diff --git a/src/Spout/Writer/Common/Cell.php b/src/Spout/Writer/Common/Cell.php index bbb0d91..e08b958 100644 --- a/src/Spout/Writer/Common/Cell.php +++ b/src/Spout/Writer/Common/Cell.php @@ -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) diff --git a/src/Spout/Writer/Common/Internal/AbstractWorkbook.php b/src/Spout/Writer/Common/Internal/AbstractWorkbook.php index e852e1a..50c4fa7 100644 --- a/src/Spout/Writer/Common/Internal/AbstractWorkbook.php +++ b/src/Spout/Writer/Common/Internal/AbstractWorkbook.php @@ -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); diff --git a/src/Spout/Writer/Common/Manager/OptionsManager.php b/src/Spout/Writer/Common/Manager/OptionsManager.php new file mode 100644 index 0000000..9e2bd8b --- /dev/null +++ b/src/Spout/Writer/Common/Manager/OptionsManager.php @@ -0,0 +1,71 @@ + 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; + } +} diff --git a/src/Spout/Writer/Common/Manager/OptionsManagerInterface.php b/src/Spout/Writer/Common/Manager/OptionsManagerInterface.php new file mode 100644 index 0000000..80ac9b4 --- /dev/null +++ b/src/Spout/Writer/Common/Manager/OptionsManagerInterface.php @@ -0,0 +1,25 @@ +getOption(Options::TEMP_FOLDER); $this->fileSystemHelper = new FileSystemHelper($tempFolder); $this->fileSystemHelper->createBaseFilesAndFolders(); + $defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE); $this->styleHelper = new StyleHelper($defaultRowStyle); } diff --git a/src/Spout/Writer/ODS/Manager/OptionsManager.php b/src/Spout/Writer/ODS/Manager/OptionsManager.php new file mode 100644 index 0000000..7681cdc --- /dev/null +++ b/src/Spout/Writer/ODS/Manager/OptionsManager.php @@ -0,0 +1,50 @@ +styleBuilder = $styleBuilder; + parent::__construct(); + } + + /** + * @inheritdoc + */ + protected function getSupportedOptions() + { + return [ + Options::TEMP_FOLDER, + Options::DEFAULT_ROW_STYLE, + Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, + ]; + } + + /** + * @inheritdoc + */ + protected function setDefaultOptions() + { + $this->setOption(Options::TEMP_FOLDER, sys_get_temp_dir()); + $this->setOption(Options::DEFAULT_ROW_STYLE, $this->styleBuilder->build()); + $this->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, true); + } +} \ No newline at end of file diff --git a/src/Spout/Writer/ODS/Writer.php b/src/Spout/Writer/ODS/Writer.php index 6571d00..fa0805e 100644 --- a/src/Spout/Writer/ODS/Writer.php +++ b/src/Spout/Writer/ODS/Writer.php @@ -4,6 +4,7 @@ namespace Box\Spout\Writer\ODS; use Box\Spout\Writer\AbstractMultiSheetsWriter; use Box\Spout\Writer\Common; +use Box\Spout\Writer\Common\Options; use Box\Spout\Writer\ODS\Internal\Workbook; /** @@ -17,9 +18,6 @@ class Writer extends AbstractMultiSheetsWriter /** @var string Content-Type value for the header */ protected static $headerContentType = 'application/vnd.oasis.opendocument.spreadsheet'; - /** @var string Temporary folder where the files to create the ODS will be stored */ - protected $tempFolder; - /** @var Internal\Workbook The workbook for the ODS file */ protected $book; @@ -36,7 +34,7 @@ class Writer extends AbstractMultiSheetsWriter { $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); - $this->tempFolder = $tempFolder; + $this->optionsManager->setOption(Options::TEMP_FOLDER, $tempFolder); return $this; } @@ -48,8 +46,7 @@ class Writer extends AbstractMultiSheetsWriter */ protected function openWriter() { - $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir(); - $this->book = new Workbook($tempFolder, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle); + $this->book = new Workbook($this->optionsManager); $this->book->addNewSheetAndMakeItCurrent(); } diff --git a/src/Spout/Writer/Style/Style.php b/src/Spout/Writer/Style/Style.php index b408ad3..a1f7626 100644 --- a/src/Spout/Writer/Style/Style.php +++ b/src/Spout/Writer/Style/Style.php @@ -61,14 +61,10 @@ class Style /** @var bool Whether the wrap text property was set */ protected $hasSetWrapText = false; - /** - * @var Border - */ + /** @var Border */ protected $border = null; - /** - * @var bool Whether border properties should be applied - */ + /** @var bool Whether border properties should be applied */ protected $shouldApplyBorder = false; /** @var string Background color */ diff --git a/src/Spout/Writer/WriterFactory.php b/src/Spout/Writer/WriterFactory.php index 7588940..a6408cb 100644 --- a/src/Spout/Writer/WriterFactory.php +++ b/src/Spout/Writer/WriterFactory.php @@ -5,6 +5,7 @@ namespace Box\Spout\Writer; use Box\Spout\Common\Exception\UnsupportedTypeException; use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Type; +use Box\Spout\Writer\Style\StyleBuilder; /** * Class WriterFactory @@ -29,13 +30,13 @@ class WriterFactory switch ($writerType) { case Type::CSV: - $writer = new CSV\Writer(); + $writer = self::getCSVWriter(); break; case Type::XLSX: - $writer = new XLSX\Writer(); + $writer = self::getXLSXWriter(); break; case Type::ODS: - $writer = new ODS\Writer(); + $writer = self::getODSWriter(); break; default: throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType); @@ -45,4 +46,36 @@ class WriterFactory return $writer; } + + /** + * @return CSV\Writer + */ + private static function getCSVWriter() + { + $optionsManager = new CSV\Manager\OptionsManager(); + + return new CSV\Writer($optionsManager); + } + + /** + * @return XLSX\Writer + */ + private static function getXLSXWriter() + { + $styleBuilder = new StyleBuilder(); + $optionsManager = new XLSX\Manager\OptionsManager($styleBuilder); + + return new XLSX\Writer($optionsManager); + } + + /** + * @return ODS\Writer + */ + private static function getODSWriter() + { + $styleBuilder = new StyleBuilder(); + $optionsManager = new ODS\Manager\OptionsManager($styleBuilder); + + return new ODS\Writer($optionsManager); + } } diff --git a/src/Spout/Writer/XLSX/Internal/Workbook.php b/src/Spout/Writer/XLSX/Internal/Workbook.php index bcdce7f..532cd95 100644 --- a/src/Spout/Writer/XLSX/Internal/Workbook.php +++ b/src/Spout/Writer/XLSX/Internal/Workbook.php @@ -3,10 +3,13 @@ namespace Box\Spout\Writer\XLSX\Internal; use Box\Spout\Writer\Common\Internal\AbstractWorkbook; +use Box\Spout\Writer\Common\Manager\OptionsManagerInterface; +use Box\Spout\Writer\Common\Options; use Box\Spout\Writer\XLSX\Helper\FileSystemHelper; use Box\Spout\Writer\XLSX\Helper\SharedStringsHelper; use Box\Spout\Writer\XLSX\Helper\StyleHelper; use Box\Spout\Writer\Common\Sheet; +use Box\Spout\Writer\XLSX\Manager\OptionsManager; /** * Class Workbook @@ -36,21 +39,18 @@ class Workbook extends AbstractWorkbook protected $styleHelper; /** - * @param string $tempFolder - * @param bool $shouldUseInlineStrings - * @param bool $shouldCreateNewSheetsAutomatically - * @param \Box\Spout\Writer\Style\Style $defaultRowStyle + * @param \Box\Spout\Writer\Common\Manager\OptionsManagerInterface $optionsManager Options manager * @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders */ - public function __construct($tempFolder, $shouldUseInlineStrings, $shouldCreateNewSheetsAutomatically, $defaultRowStyle) + public function __construct(OptionsManagerInterface $optionsManager) { - parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle); - - $this->shouldUseInlineStrings = $shouldUseInlineStrings; + parent::__construct($optionsManager); + $tempFolder = $optionsManager->getOption(Options::TEMP_FOLDER); $this->fileSystemHelper = new FileSystemHelper($tempFolder); $this->fileSystemHelper->createBaseFilesAndFolders(); + $defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE); $this->styleHelper = new StyleHelper($defaultRowStyle); // This helper will be shared by all sheets @@ -86,7 +86,7 @@ class Workbook extends AbstractWorkbook $sheet = new Sheet($newSheetIndex, $this->internalId); $worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); - $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->styleHelper, $this->shouldUseInlineStrings); + $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->styleHelper, $this->optionManager); $this->worksheets[] = $worksheet; return $worksheet; diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index 7f71829..3e6dfa0 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -8,6 +8,8 @@ use Box\Spout\Common\Helper\StringHelper; use Box\Spout\Writer\Common\Cell; use Box\Spout\Writer\Common\Helper\CellHelper; use Box\Spout\Writer\Common\Internal\WorksheetInterface; +use Box\Spout\Writer\Common\Manager\OptionsManagerInterface; +use Box\Spout\Writer\Common\Options; /** * Class Worksheet @@ -62,16 +64,16 @@ EOD; * @param \Box\Spout\Writer\Common\Sheet $externalSheet The associated "external" sheet * @param string $worksheetFilesFolder Temporary folder where the files to create the XLSX will be stored * @param \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper $sharedStringsHelper Helper for shared strings - * @param \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to work with styles - * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used + * @param \Box\Spout\Writer\XLSX\Helper\StyleHelper $styleHelper Helper to work with styles + * @param \Box\Spout\Writer\Common\Manager\OptionsManagerInterface $optionsManager Options manager * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing */ - public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $styleHelper, $shouldUseInlineStrings) + public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $styleHelper, OptionsManagerInterface $optionsManager) { $this->externalSheet = $externalSheet; $this->sharedStringsHelper = $sharedStringsHelper; $this->styleHelper = $styleHelper; - $this->shouldUseInlineStrings = $shouldUseInlineStrings; + $this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS); /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ $this->stringsEscaper = \Box\Spout\Common\Escaper\XLSX::getInstance(); diff --git a/src/Spout/Writer/XLSX/Manager/OptionsManager.php b/src/Spout/Writer/XLSX/Manager/OptionsManager.php new file mode 100644 index 0000000..d2a7663 --- /dev/null +++ b/src/Spout/Writer/XLSX/Manager/OptionsManager.php @@ -0,0 +1,61 @@ +styleBuilder = $styleBuilder; + parent::__construct(); + } + + /** + * @inheritdoc + */ + protected function getSupportedOptions() + { + return [ + Options::TEMP_FOLDER, + Options::DEFAULT_ROW_STYLE, + Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, + Options::SHOULD_USE_INLINE_STRINGS, + ]; + } + + /** + * @inheritdoc + */ + protected function setDefaultOptions() + { + $defaultRowStyle = $this->styleBuilder + ->setFontSize(self::DEFAULT_FONT_SIZE) + ->setFontName(self::DEFAULT_FONT_NAME) + ->build(); + + $this->setOption(Options::TEMP_FOLDER, sys_get_temp_dir()); + $this->setOption(Options::DEFAULT_ROW_STYLE, $defaultRowStyle); + $this->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, true); + $this->setOption(Options::SHOULD_USE_INLINE_STRINGS, true); + } +} \ No newline at end of file diff --git a/src/Spout/Writer/XLSX/Writer.php b/src/Spout/Writer/XLSX/Writer.php index 965955a..1fdb650 100644 --- a/src/Spout/Writer/XLSX/Writer.php +++ b/src/Spout/Writer/XLSX/Writer.php @@ -3,6 +3,7 @@ namespace Box\Spout\Writer\XLSX; use Box\Spout\Writer\AbstractMultiSheetsWriter; +use Box\Spout\Writer\Common\Options; use Box\Spout\Writer\Style\StyleBuilder; use Box\Spout\Writer\XLSX\Internal\Workbook; @@ -14,19 +15,9 @@ use Box\Spout\Writer\XLSX\Internal\Workbook; */ class Writer extends AbstractMultiSheetsWriter { - /** Default style font values */ - const DEFAULT_FONT_SIZE = 12; - const DEFAULT_FONT_NAME = 'Calibri'; - /** @var string Content-Type value for the header */ protected static $headerContentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; - /** @var string Temporary folder where the files to create the XLSX will be stored */ - protected $tempFolder; - - /** @var bool Whether inline or shared strings should be used - inline string is more memory efficient */ - protected $shouldUseInlineStrings = true; - /** @var Internal\Workbook The workbook for the XLSX file */ protected $book; @@ -43,7 +34,7 @@ class Writer extends AbstractMultiSheetsWriter { $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); - $this->tempFolder = $tempFolder; + $this->optionsManager->setOption(Options::TEMP_FOLDER, $tempFolder); return $this; } @@ -60,7 +51,7 @@ class Writer extends AbstractMultiSheetsWriter { $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); - $this->shouldUseInlineStrings = $shouldUseInlineStrings; + $this->optionsManager->setOption(Options::SHOULD_USE_INLINE_STRINGS, $shouldUseInlineStrings); return $this; } @@ -73,8 +64,7 @@ class Writer extends AbstractMultiSheetsWriter protected function openWriter() { if (!$this->book) { - $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir(); - $this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle); + $this->book = new Workbook($this->optionsManager); $this->book->addNewSheetAndMakeItCurrent(); } } @@ -105,19 +95,6 @@ class Writer extends AbstractMultiSheetsWriter $this->book->addRowToCurrentWorksheet($dataRow, $style); } - /** - * Returns the default style to be applied to rows. - * - * @return \Box\Spout\Writer\Style\Style - */ - protected function getDefaultRowStyle() - { - return (new StyleBuilder()) - ->setFontSize(self::DEFAULT_FONT_SIZE) - ->setFontName(self::DEFAULT_FONT_NAME) - ->build(); - } - /** * Closes the writer, preventing any additional writing. * diff --git a/tests/Spout/Writer/CSV/WriterTest.php b/tests/Spout/Writer/CSV/WriterTest.php index c2f1d2c..ba569f1 100644 --- a/tests/Spout/Writer/CSV/WriterTest.php +++ b/tests/Spout/Writer/CSV/WriterTest.php @@ -204,6 +204,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase $this->createGeneratedFolderIfNeeded($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName); + /** @var \Box\Spout\Writer\CSV\Writer $writer */ $writer = WriterFactory::create(Type::CSV); $writer->setFieldDelimiter($fieldDelimiter); $writer->setFieldEnclosure($fieldEnclosure); diff --git a/tests/Spout/Writer/Common/Manager/OptionsManagerTest.php b/tests/Spout/Writer/Common/Manager/OptionsManagerTest.php new file mode 100644 index 0000000..66889b4 --- /dev/null +++ b/tests/Spout/Writer/Common/Manager/OptionsManagerTest.php @@ -0,0 +1,70 @@ +assertEquals('foo-val', $optionsManager->getOption('foo')); + $this->assertEquals(false, $optionsManager->getOption('bar')); + } + + /** + * @return void + */ + public function testOptionsManagerShouldReturnUpdatedOptionValue() + { + $optionsManager = new FakeOptionsManager(); + $optionsManager->setOption('foo', 'new-val'); + $this->assertEquals('new-val', $optionsManager->getOption('foo')); + } + + /** + * @return void + */ + public function testOptionsManagerShouldReturnNullIfNoDefaultValueSet() + { + $optionsManager = new FakeOptionsManager(); + $this->assertNull($optionsManager->getOption('baz')); + } + + /** + * @return void + */ + public function testOptionsManagerShouldReturnNullIfNoOptionNotSupported() + { + $optionsManager = new FakeOptionsManager(); + $optionsManager->setOption('not-supported', 'something'); + $this->assertNull($optionsManager->getOption('not-supported')); + } +} + + +// TODO: Convert this to anonymous class when PHP < 7 support is dropped +class FakeOptionsManager extends OptionsManager +{ + protected function getSupportedOptions() + { + return [ + 'foo', + 'bar', + 'baz', + ]; + } + + protected function setDefaultOptions() + { + $this->setOption('foo', 'foo-val'); + $this->setOption('bar', false); + } +} \ No newline at end of file diff --git a/tests/Spout/Writer/ODS/WriterWithStyleTest.php b/tests/Spout/Writer/ODS/WriterWithStyleTest.php index f6af4a1..b472055 100644 --- a/tests/Spout/Writer/ODS/WriterWithStyleTest.php +++ b/tests/Spout/Writer/ODS/WriterWithStyleTest.php @@ -375,7 +375,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase $this->createGeneratedFolderIfNeeded($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName); - /** @var \Box\Spout\Writer\XLSX\Writer $writer */ + /** @var \Box\Spout\Writer\ODS\Writer $writer */ $writer = WriterFactory::create(Type::ODS); $writer->setDefaultRowStyle($defaultStyle); @@ -467,7 +467,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase /** * @param string $fileName * @param string $section - * @return \DomElement + * @return \DomNode */ private function getXmlSectionFromStylesXmlFile($fileName, $section) { diff --git a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php index e4d1749..fd66796 100644 --- a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php +++ b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php @@ -11,6 +11,7 @@ use Box\Spout\Writer\Style\Color; use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Style\StyleBuilder; use Box\Spout\Writer\WriterFactory; +use Box\Spout\Writer\XLSX\Manager\OptionsManager; /** * Class WriterWithStyleTest @@ -129,9 +130,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase // First font should be the default one $defaultFontElement = $fontElements->item(0); $this->assertChildrenNumEquals(3, $defaultFontElement, 'The default font should only have 3 properties.'); - $this->assertFirstChildHasAttributeEquals((string) Writer::DEFAULT_FONT_SIZE, $defaultFontElement, 'sz', 'val'); + $this->assertFirstChildHasAttributeEquals((string) OptionsManager::DEFAULT_FONT_SIZE, $defaultFontElement, 'sz', 'val'); $this->assertFirstChildHasAttributeEquals(Color::toARGB(Style::DEFAULT_FONT_COLOR), $defaultFontElement, 'color', 'rgb'); - $this->assertFirstChildHasAttributeEquals(Writer::DEFAULT_FONT_NAME, $defaultFontElement, 'name', 'val'); + $this->assertFirstChildHasAttributeEquals(OptionsManager::DEFAULT_FONT_NAME, $defaultFontElement, 'name', 'val'); // Second font should contain data from the first created style $secondFontElement = $fontElements->item(1); @@ -140,9 +141,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase $this->assertChildExists($secondFontElement, 'i'); $this->assertChildExists($secondFontElement, 'u'); $this->assertChildExists($secondFontElement, 'strike'); - $this->assertFirstChildHasAttributeEquals((string) Writer::DEFAULT_FONT_SIZE, $secondFontElement, 'sz', 'val'); + $this->assertFirstChildHasAttributeEquals((string) OptionsManager::DEFAULT_FONT_SIZE, $secondFontElement, 'sz', 'val'); $this->assertFirstChildHasAttributeEquals(Color::toARGB(Style::DEFAULT_FONT_COLOR), $secondFontElement, 'color', 'rgb'); - $this->assertFirstChildHasAttributeEquals(Writer::DEFAULT_FONT_NAME, $secondFontElement, 'name', 'val'); + $this->assertFirstChildHasAttributeEquals(OptionsManager::DEFAULT_FONT_NAME, $secondFontElement, 'name', 'val'); // Third font should contain data from the second created style $thirdFontElement = $fontElements->item(2);