From a665b974fae0c17134e50989ead749d4f55a217b Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Sat, 18 Nov 2017 18:32:11 +0100 Subject: [PATCH] Make CSV reader return Row objects --- src/Spout/Common/Helper/CellTypeHelper.php | 54 ++++++ ...yFactory.php => InternalEntityFactory.php} | 21 ++- src/Spout/Reader/CSV/Reader.php | 4 +- src/Spout/Reader/CSV/RowIterator.php | 37 ++-- .../Reader/Common/Creator/EntityFactory.php | 25 +++ ...php => InternalEntityFactoryInterface.php} | 2 +- src/Spout/Reader/Common/Entity/Cell.php | 159 ++++++++++++++++++ src/Spout/Reader/Common/Entity/Row.php | 64 +++++++ .../Reader/ODS/Creator/HelperFactory.php | 2 +- ...yFactory.php => InternalEntityFactory.php} | 4 +- .../Reader/ODS/Helper/SettingsHelper.php | 6 +- src/Spout/Reader/ODS/Reader.php | 6 +- src/Spout/Reader/ODS/SheetIterator.php | 6 +- src/Spout/Reader/ReaderAbstract.php | 8 +- src/Spout/Reader/ReaderFactory.php | 6 +- ...yFactory.php => InternalEntityFactory.php} | 5 +- .../Reader/XLSX/Creator/ManagerFactory.php | 8 +- .../XLSX/Manager/SharedStringsManager.php | 6 +- .../Reader/XLSX/Manager/SheetManager.php | 6 +- .../Reader/XLSX/Manager/StyleManager.php | 6 +- .../Manager/WorkbookRelationshipsManager.php | 6 +- src/Spout/Reader/XLSX/Reader.php | 10 +- .../Writer/Common/Creator/EntityFactory.php | 2 +- src/Spout/Writer/Common/Entity/Cell.php | 10 +- src/Spout/Writer/Common/Helper/CellHelper.php | 44 ----- .../Common/Helper/CellTypeHelperTest.php | 84 +++++++++ tests/Spout/Reader/CSV/ReaderTest.php | 16 +- tests/Spout/Reader/CSV/SheetTest.php | 4 +- tests/Spout/Reader/Common/Entity/CellTest.php | 64 +++++++ tests/Spout/Reader/Common/Entity/RowTest.php | 90 ++++++++++ tests/Spout/Reader/ODS/ReaderTest.php | 12 +- tests/Spout/Reader/ODS/SheetTest.php | 4 +- .../XLSX/Manager/SharedStringsManagerTest.php | 4 +- .../Reader/XLSX/Manager/StyleManagerTest.php | 4 +- tests/Spout/Reader/XLSX/ReaderTest.php | 10 +- tests/Spout/Reader/XLSX/SheetTest.php | 4 +- .../Writer/Common/Helper/CellHelperTest.php | 75 --------- 37 files changed, 664 insertions(+), 214 deletions(-) create mode 100644 src/Spout/Common/Helper/CellTypeHelper.php rename src/Spout/Reader/CSV/Creator/{EntityFactory.php => InternalEntityFactory.php} (76%) create mode 100644 src/Spout/Reader/Common/Creator/EntityFactory.php rename src/Spout/Reader/Common/Creator/{EntityFactoryInterface.php => InternalEntityFactoryInterface.php} (70%) create mode 100644 src/Spout/Reader/Common/Entity/Cell.php create mode 100644 src/Spout/Reader/Common/Entity/Row.php rename src/Spout/Reader/ODS/Creator/{EntityFactory.php => InternalEntityFactory.php} (95%) rename src/Spout/Reader/XLSX/Creator/{EntityFactory.php => InternalEntityFactory.php} (96%) create mode 100644 tests/Spout/Common/Helper/CellTypeHelperTest.php create mode 100644 tests/Spout/Reader/Common/Entity/CellTest.php create mode 100644 tests/Spout/Reader/Common/Entity/RowTest.php diff --git a/src/Spout/Common/Helper/CellTypeHelper.php b/src/Spout/Common/Helper/CellTypeHelper.php new file mode 100644 index 0000000..b9a6407 --- /dev/null +++ b/src/Spout/Common/Helper/CellTypeHelper.php @@ -0,0 +1,54 @@ +helperFactory->createEncodingHelper($globalFunctionsHelper); - return new RowIterator($filePointer, $optionsManager, $encodingHelper, $globalFunctionsHelper); + return new RowIterator($filePointer, $optionsManager, $encodingHelper, $this, $globalFunctionsHelper); + } + + /** + * @param array $cellValues + * @return Row + */ + public function createRowFromArray(array $cellValues = []) + { + $cells = array_map(function ($cellValue) { + return new Cell($cellValue); + }, $cellValues); + + return new Row($cells); } } diff --git a/src/Spout/Reader/CSV/Reader.php b/src/Spout/Reader/CSV/Reader.php index ec43405..77249c1 100644 --- a/src/Spout/Reader/CSV/Reader.php +++ b/src/Spout/Reader/CSV/Reader.php @@ -4,7 +4,7 @@ namespace Box\Spout\Reader\CSV; use Box\Spout\Common\Exception\IOException; use Box\Spout\Reader\Common\Entity\Options; -use Box\Spout\Reader\CSV\Creator\EntityFactory; +use Box\Spout\Reader\CSV\Creator\InternalEntityFactory; use Box\Spout\Reader\ReaderAbstract; /** @@ -92,7 +92,7 @@ class Reader extends ReaderAbstract throw new IOException("Could not open file $filePath for reading."); } - /** @var EntityFactory $entityFactory */ + /** @var InternalEntityFactory $entityFactory */ $entityFactory = $this->entityFactory; $this->sheetIterator = $entityFactory->createSheetIterator( diff --git a/src/Spout/Reader/CSV/RowIterator.php b/src/Spout/Reader/CSV/RowIterator.php index d1ac3f6..d768936 100644 --- a/src/Spout/Reader/CSV/RowIterator.php +++ b/src/Spout/Reader/CSV/RowIterator.php @@ -3,7 +3,11 @@ namespace Box\Spout\Reader\CSV; use Box\Spout\Common\Helper\EncodingHelper; +use Box\Spout\Common\Helper\GlobalFunctionsHelper; +use Box\Spout\Common\Manager\OptionsManagerInterface; use Box\Spout\Reader\Common\Entity\Options; +use Box\Spout\Reader\Common\Entity\Row; +use Box\Spout\Reader\CSV\Creator\InternalEntityFactory; use Box\Spout\Reader\IteratorInterface; /** @@ -23,7 +27,7 @@ class RowIterator implements IteratorInterface /** @var int Number of read rows */ protected $numReadRows = 0; - /** @var array|null Buffer used to store the row data, while checking if there are more rows to read */ + /** @var Row|null Buffer used to store the row data, while checking if there are more rows to read */ protected $rowDataBuffer; /** @var bool Indicates whether all rows have been read */ @@ -41,26 +45,36 @@ class RowIterator implements IteratorInterface /** @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; - /** @var \Box\Spout\Common\Helper\EncodingHelper Helper to work with different encodings */ protected $encodingHelper; + /** @var \Box\Spout\Reader\CSV\Creator\InternalEntityFactory Factory to create entities */ + protected $entityFactory; + + /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ + protected $globalFunctionsHelper; + /** * @param resource $filePointer Pointer to the CSV file to read - * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager - * @param \Box\Spout\Common\Helper\EncodingHelper $encodingHelper - * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper + * @param OptionsManagerInterface $optionsManager + * @param EncodingHelper $encodingHelper + * @param InternalEntityFactory $entityFactory + * @param GlobalFunctionsHelper $globalFunctionsHelper */ - public function __construct($filePointer, $optionsManager, $encodingHelper, $globalFunctionsHelper) - { + public function __construct( + $filePointer, + OptionsManagerInterface $optionsManager, + EncodingHelper $encodingHelper, + InternalEntityFactory $entityFactory, + GlobalFunctionsHelper $globalFunctionsHelper + ) { $this->filePointer = $filePointer; $this->fieldDelimiter = $optionsManager->getOption(Options::FIELD_DELIMITER); $this->fieldEnclosure = $optionsManager->getOption(Options::FIELD_ENCLOSURE); $this->encoding = $optionsManager->getOption(Options::ENCODING); $this->shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS); $this->encodingHelper = $encodingHelper; + $this->entityFactory = $entityFactory; $this->globalFunctionsHelper = $globalFunctionsHelper; } @@ -133,7 +147,8 @@ class RowIterator implements IteratorInterface if ($rowData !== false) { // str_replace will replace NULL values by empty strings - $this->rowDataBuffer = str_replace(null, null, $rowData); + $rowDataBufferAsArray = str_replace(null, null, $rowData); + $this->rowDataBuffer = $this->entityFactory->createRowFromArray($rowDataBufferAsArray); $this->numReadRows++; } else { // If we reach this point, it means end of file was reached. @@ -207,7 +222,7 @@ class RowIterator implements IteratorInterface * Return the current element from the buffer * @see http://php.net/manual/en/iterator.current.php * - * @return array|null + * @return Row|null */ public function current() { diff --git a/src/Spout/Reader/Common/Creator/EntityFactory.php b/src/Spout/Reader/Common/Creator/EntityFactory.php new file mode 100644 index 0000000..949afc9 --- /dev/null +++ b/src/Spout/Reader/Common/Creator/EntityFactory.php @@ -0,0 +1,25 @@ +create($readerType); + } +} diff --git a/src/Spout/Reader/Common/Creator/EntityFactoryInterface.php b/src/Spout/Reader/Common/Creator/InternalEntityFactoryInterface.php similarity index 70% rename from src/Spout/Reader/Common/Creator/EntityFactoryInterface.php rename to src/Spout/Reader/Common/Creator/InternalEntityFactoryInterface.php index 78b7754..1df1c1e 100644 --- a/src/Spout/Reader/Common/Creator/EntityFactoryInterface.php +++ b/src/Spout/Reader/Common/Creator/InternalEntityFactoryInterface.php @@ -5,6 +5,6 @@ namespace Box\Spout\Reader\Common\Creator; /** * Interface EntityFactoryInterface */ -interface EntityFactoryInterface +interface InternalEntityFactoryInterface { } diff --git a/src/Spout/Reader/Common/Entity/Cell.php b/src/Spout/Reader/Common/Entity/Cell.php new file mode 100644 index 0000000..7b184cf --- /dev/null +++ b/src/Spout/Reader/Common/Entity/Cell.php @@ -0,0 +1,159 @@ +setValue($value); + } + + /** + * @param mixed|null $value + */ + public function setValue($value) + { + $this->value = $value; + $this->type = $this->detectType($value); + } + + /** + * @return mixed|null + */ + public function getValue() + { + return $this->value; + } + + /** + * @return int|null + */ + public function getType() + { + return $this->type; + } + + /** + * Get the current value type + * + * @param mixed|null $value + * @return int + */ + protected function detectType($value) + { + if (CellTypeHelper::isBoolean($value)) { + return self::TYPE_BOOLEAN; + } + if (CellTypeHelper::isEmpty($value)) { + return self::TYPE_EMPTY; + } + if (CellTypeHelper::isNumeric($this->getValue())) { + return self::TYPE_NUMERIC; + } + if (CellTypeHelper::isNonEmptyString($value)) { + return self::TYPE_STRING; + } + + return self::TYPE_ERROR; + } + + /** + * @return bool + */ + public function isBoolean() + { + return $this->type === self::TYPE_BOOLEAN; + } + + /** + * @return bool + */ + public function isEmpty() + { + return $this->type === self::TYPE_EMPTY; + } + + /** + * @return bool + */ + public function isNumeric() + { + return $this->type === self::TYPE_NUMERIC; + } + + /** + * @return bool + */ + public function isString() + { + return $this->type === self::TYPE_STRING; + } + + /** + * @return bool + */ + public function isError() + { + return $this->type === self::TYPE_ERROR; + } + + /** + * @return string + */ + public function __toString() + { + return (string) $this->value; + } +} diff --git a/src/Spout/Reader/Common/Entity/Row.php b/src/Spout/Reader/Common/Entity/Row.php new file mode 100644 index 0000000..1663a95 --- /dev/null +++ b/src/Spout/Reader/Common/Entity/Row.php @@ -0,0 +1,64 @@ +setCells($cells); + } + + /** + * @return Cell[] $cells + */ + public function getCells() + { + return $this->cells; + } + + /** + * @param Cell[] $cells + * @return $this + */ + public function setCells(array $cells) + { + $this->cells = []; + foreach ($cells as $cell) { + $this->addCell($cell); + } + + return $this; + } + + /** + * @param Cell $cell + * @return Row + */ + public function addCell(Cell $cell) + { + $this->cells[] = $cell; + + return $this; + } + + /** + * @return array The row values, as array + */ + public function toArray() + { + return array_map(function (Cell $cell) { + return $cell->getValue(); + }, $this->cells); + } +} diff --git a/src/Spout/Reader/ODS/Creator/HelperFactory.php b/src/Spout/Reader/ODS/Creator/HelperFactory.php index 6f128bd..e4bbab8 100644 --- a/src/Spout/Reader/ODS/Creator/HelperFactory.php +++ b/src/Spout/Reader/ODS/Creator/HelperFactory.php @@ -23,7 +23,7 @@ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory } /** - * @param EntityFactory $entityFactory + * @param InternalEntityFactory $entityFactory * @return SettingsHelper */ public function createSettingsHelper($entityFactory) diff --git a/src/Spout/Reader/ODS/Creator/EntityFactory.php b/src/Spout/Reader/ODS/Creator/InternalEntityFactory.php similarity index 95% rename from src/Spout/Reader/ODS/Creator/EntityFactory.php rename to src/Spout/Reader/ODS/Creator/InternalEntityFactory.php index 0324dd8..5e89fb7 100644 --- a/src/Spout/Reader/ODS/Creator/EntityFactory.php +++ b/src/Spout/Reader/ODS/Creator/InternalEntityFactory.php @@ -2,7 +2,7 @@ namespace Box\Spout\Reader\ODS\Creator; -use Box\Spout\Reader\Common\Creator\EntityFactoryInterface; +use Box\Spout\Reader\Common\Creator\InternalEntityFactoryInterface; use Box\Spout\Reader\Common\Entity\Options; use Box\Spout\Reader\Common\XMLProcessor; use Box\Spout\Reader\ODS\RowIterator; @@ -14,7 +14,7 @@ use Box\Spout\Reader\Wrapper\XMLReader; * Class EntityFactory * Factory to create entities */ -class EntityFactory implements EntityFactoryInterface +class InternalEntityFactory implements InternalEntityFactoryInterface { /** @var HelperFactory */ private $helperFactory; diff --git a/src/Spout/Reader/ODS/Helper/SettingsHelper.php b/src/Spout/Reader/ODS/Helper/SettingsHelper.php index 9a2b8f7..7d47821 100644 --- a/src/Spout/Reader/ODS/Helper/SettingsHelper.php +++ b/src/Spout/Reader/ODS/Helper/SettingsHelper.php @@ -3,7 +3,7 @@ namespace Box\Spout\Reader\ODS\Helper; use Box\Spout\Reader\Exception\XMLProcessingException; -use Box\Spout\Reader\ODS\Creator\EntityFactory; +use Box\Spout\Reader\ODS\Creator\InternalEntityFactory; /** * Class SettingsHelper @@ -18,11 +18,11 @@ class SettingsHelper const XML_ATTRIBUTE_CONFIG_NAME = 'config:name'; const XML_ATTRIBUTE_VALUE_ACTIVE_TABLE = 'ActiveTable'; - /** @var EntityFactory Factory to create entities */ + /** @var InternalEntityFactory Factory to create entities */ private $entityFactory; /** - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities */ public function __construct($entityFactory) { diff --git a/src/Spout/Reader/ODS/Reader.php b/src/Spout/Reader/ODS/Reader.php index 2e8ba28..d07dbb4 100644 --- a/src/Spout/Reader/ODS/Reader.php +++ b/src/Spout/Reader/ODS/Reader.php @@ -3,7 +3,7 @@ namespace Box\Spout\Reader\ODS; use Box\Spout\Common\Exception\IOException; -use Box\Spout\Reader\ODS\Creator\EntityFactory; +use Box\Spout\Reader\ODS\Creator\InternalEntityFactory; use Box\Spout\Reader\ReaderAbstract; /** @@ -38,13 +38,13 @@ class Reader extends ReaderAbstract */ protected function openReader($filePath) { - /** @var EntityFactory $entityFactory */ + /** @var InternalEntityFactory $entityFactory */ $entityFactory = $this->entityFactory; $this->zip = $entityFactory->createZipArchive(); if ($this->zip->open($filePath) === true) { - /** @var EntityFactory $entityFactory */ + /** @var InternalEntityFactory $entityFactory */ $entityFactory = $this->entityFactory; $this->sheetIterator = $entityFactory->createSheetIterator($filePath, $this->optionsManager); } else { diff --git a/src/Spout/Reader/ODS/SheetIterator.php b/src/Spout/Reader/ODS/SheetIterator.php index 6785b36..f35b852 100644 --- a/src/Spout/Reader/ODS/SheetIterator.php +++ b/src/Spout/Reader/ODS/SheetIterator.php @@ -5,7 +5,7 @@ namespace Box\Spout\Reader\ODS; use Box\Spout\Common\Exception\IOException; use Box\Spout\Reader\Exception\XMLProcessingException; use Box\Spout\Reader\IteratorInterface; -use Box\Spout\Reader\ODS\Creator\EntityFactory; +use Box\Spout\Reader\ODS\Creator\InternalEntityFactory; use Box\Spout\Reader\ODS\Helper\SettingsHelper; use Box\Spout\Reader\Wrapper\XMLReader; @@ -34,7 +34,7 @@ class SheetIterator implements IteratorInterface /** @var \Box\Spout\Common\Manager\OptionsManagerInterface Reader's options manager */ protected $optionsManager; - /** @var EntityFactory $entityFactory Factory to create entities */ + /** @var InternalEntityFactory $entityFactory Factory to create entities */ protected $entityFactory; /** @var XMLReader The XMLReader object that will help read sheet's XML data */ @@ -60,7 +60,7 @@ class SheetIterator implements IteratorInterface * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager * @param \Box\Spout\Common\Helper\Escaper\ODS $escaper Used to unescape XML data * @param SettingsHelper $settingsHelper Helper to get data from "settings.xml" - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities */ public function __construct($filePath, $optionsManager, $escaper, $settingsHelper, $entityFactory) { diff --git a/src/Spout/Reader/ReaderAbstract.php b/src/Spout/Reader/ReaderAbstract.php index b789636..dea234e 100644 --- a/src/Spout/Reader/ReaderAbstract.php +++ b/src/Spout/Reader/ReaderAbstract.php @@ -5,7 +5,7 @@ namespace Box\Spout\Reader; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Manager\OptionsManagerInterface; -use Box\Spout\Reader\Common\Creator\EntityFactoryInterface; +use Box\Spout\Reader\Common\Creator\InternalEntityFactoryInterface; use Box\Spout\Reader\Common\Entity\Options; use Box\Spout\Reader\Exception\ReaderNotOpenedException; @@ -19,7 +19,7 @@ abstract class ReaderAbstract implements ReaderInterface /** @var bool Indicates whether the stream is currently open */ protected $isStreamOpened = false; - /** @var EntityFactoryInterface Factory to create entities */ + /** @var InternalEntityFactoryInterface Factory to create entities */ protected $entityFactory; /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ @@ -60,12 +60,12 @@ abstract class ReaderAbstract implements ReaderInterface /** * @param OptionsManagerInterface $optionsManager * @param GlobalFunctionsHelper $globalFunctionsHelper - * @param EntityFactoryInterface $entityFactory + * @param InternalEntityFactoryInterface $entityFactory */ public function __construct( OptionsManagerInterface $optionsManager, GlobalFunctionsHelper $globalFunctionsHelper, - EntityFactoryInterface $entityFactory + InternalEntityFactoryInterface $entityFactory ) { $this->optionsManager = $optionsManager; $this->globalFunctionsHelper = $globalFunctionsHelper; diff --git a/src/Spout/Reader/ReaderFactory.php b/src/Spout/Reader/ReaderFactory.php index 02b4e2f..4687cd5 100644 --- a/src/Spout/Reader/ReaderFactory.php +++ b/src/Spout/Reader/ReaderFactory.php @@ -39,7 +39,7 @@ class ReaderFactory { $optionsManager = new CSV\Manager\OptionsManager(); $helperFactory = new HelperFactory(); - $entityFactory = new CSV\Creator\EntityFactory($helperFactory); + $entityFactory = new CSV\Creator\InternalEntityFactory($helperFactory); $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); return new CSV\Reader($optionsManager, $globalFunctionsHelper, $entityFactory); @@ -53,7 +53,7 @@ class ReaderFactory $optionsManager = new XLSX\Manager\OptionsManager(); $helperFactory = new XLSX\Creator\HelperFactory(); $managerFactory = new XLSX\Creator\ManagerFactory($helperFactory, new CachingStrategyFactory()); - $entityFactory = new XLSX\Creator\EntityFactory($managerFactory, $helperFactory); + $entityFactory = new XLSX\Creator\InternalEntityFactory($managerFactory, $helperFactory); $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); return new XLSX\Reader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory); @@ -66,7 +66,7 @@ class ReaderFactory { $optionsManager = new ODS\Manager\OptionsManager(); $helperFactory = new ODS\Creator\HelperFactory(); - $entityFactory = new ODS\Creator\EntityFactory($helperFactory); + $entityFactory = new ODS\Creator\InternalEntityFactory($helperFactory); $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); return new ODS\Reader($optionsManager, $globalFunctionsHelper, $entityFactory); diff --git a/src/Spout/Reader/XLSX/Creator/EntityFactory.php b/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php similarity index 96% rename from src/Spout/Reader/XLSX/Creator/EntityFactory.php rename to src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php index 8ef648a..98b7bb7 100644 --- a/src/Spout/Reader/XLSX/Creator/EntityFactory.php +++ b/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php @@ -2,7 +2,7 @@ namespace Box\Spout\Reader\XLSX\Creator; -use Box\Spout\Reader\Common\Creator\EntityFactoryInterface; +use Box\Spout\Reader\Common\Creator\InternalEntityFactoryInterface; use Box\Spout\Reader\Common\Entity\Options; use Box\Spout\Reader\Common\XMLProcessor; use Box\Spout\Reader\Wrapper\XMLReader; @@ -10,13 +10,12 @@ use Box\Spout\Reader\XLSX\Manager\SharedStringsManager; use Box\Spout\Reader\XLSX\RowIterator; use Box\Spout\Reader\XLSX\Sheet; use Box\Spout\Reader\XLSX\SheetIterator; -use MongoDB\Driver\Manager; /** * Class EntityFactory * Factory to create entities */ -class EntityFactory implements EntityFactoryInterface +class InternalEntityFactory implements InternalEntityFactoryInterface { /** @var HelperFactory */ private $helperFactory; diff --git a/src/Spout/Reader/XLSX/Creator/ManagerFactory.php b/src/Spout/Reader/XLSX/Creator/ManagerFactory.php index 58162f3..194edf7 100644 --- a/src/Spout/Reader/XLSX/Creator/ManagerFactory.php +++ b/src/Spout/Reader/XLSX/Creator/ManagerFactory.php @@ -36,7 +36,7 @@ class ManagerFactory /** * @param string $filePath Path of the XLSX file being read * @param string $tempFolder Temporary folder where the temporary files to store shared strings will be stored - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities * @return SharedStringsManager */ public function createSharedStringsManager($filePath, $tempFolder, $entityFactory) @@ -55,7 +55,7 @@ class ManagerFactory /** * @param string $filePath Path of the XLSX file being read - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities * @return WorkbookRelationshipsManager */ private function createWorkbookRelationshipsManager($filePath, $entityFactory) @@ -71,7 +71,7 @@ class ManagerFactory * @param string $filePath Path of the XLSX file being read * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager * @param \Box\Spout\Reader\XLSX\Manager\SharedStringsManager $sharedStringsManager Manages shared strings - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities * @return SheetManager */ public function createSheetManager($filePath, $optionsManager, $sharedStringsManager, $entityFactory) @@ -83,7 +83,7 @@ class ManagerFactory /** * @param string $filePath Path of the XLSX file being read - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities * @return StyleManager */ public function createStyleManager($filePath, $entityFactory) diff --git a/src/Spout/Reader/XLSX/Manager/SharedStringsManager.php b/src/Spout/Reader/XLSX/Manager/SharedStringsManager.php index 2a21862..9d4e14b 100644 --- a/src/Spout/Reader/XLSX/Manager/SharedStringsManager.php +++ b/src/Spout/Reader/XLSX/Manager/SharedStringsManager.php @@ -5,8 +5,8 @@ namespace Box\Spout\Reader\XLSX\Manager; use Box\Spout\Common\Exception\IOException; use Box\Spout\Reader\Exception\XMLProcessingException; use Box\Spout\Reader\Wrapper\XMLReader; -use Box\Spout\Reader\XLSX\Creator\EntityFactory; use Box\Spout\Reader\XLSX\Creator\HelperFactory; +use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory; use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyInterface; use Box\Spout\Writer\Common\Entity\Workbook; @@ -41,7 +41,7 @@ class SharedStringsManager /** @var WorkbookRelationshipsManager Helps retrieving workbook relationships */ protected $workbookRelationshipsManager; - /** @var EntityFactory Factory to create entities */ + /** @var InternalEntityFactory Factory to create entities */ protected $entityFactory; /** @var HelperFactory $helperFactory Factory to create helpers */ @@ -57,7 +57,7 @@ class SharedStringsManager * @param string $filePath Path of the XLSX file being read * @param string $tempFolder Temporary folder where the temporary files to store shared strings will be stored * @param WorkbookRelationshipsManager $workbookRelationshipsManager Helps retrieving workbook relationships - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities * @param HelperFactory $helperFactory Factory to create helpers * @param CachingStrategyFactory $cachingStrategyFactory Factory to create shared strings caching strategies */ diff --git a/src/Spout/Reader/XLSX/Manager/SheetManager.php b/src/Spout/Reader/XLSX/Manager/SheetManager.php index ac400e3..dd609cb 100644 --- a/src/Spout/Reader/XLSX/Manager/SheetManager.php +++ b/src/Spout/Reader/XLSX/Manager/SheetManager.php @@ -4,7 +4,7 @@ namespace Box\Spout\Reader\XLSX\Manager; use Box\Spout\Reader\Common\Entity\Options; use Box\Spout\Reader\Common\XMLProcessor; -use Box\Spout\Reader\XLSX\Creator\EntityFactory; +use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; use Box\Spout\Reader\XLSX\Sheet; /** @@ -48,7 +48,7 @@ class SheetManager /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ protected $globalFunctionsHelper; - /** @var EntityFactory Factory to create entities */ + /** @var InternalEntityFactory Factory to create entities */ protected $entityFactory; /** @var \Box\Spout\Common\Helper\Escaper\XLSX Used to unescape XML data */ @@ -68,7 +68,7 @@ class SheetManager * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager * @param \Box\Spout\Reader\XLSX\Manager\SharedStringsManager $sharedStringsManager Manages shared strings * @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities * @param mixed $sharedStringsManager */ public function __construct($filePath, $optionsManager, $sharedStringsManager, $escaper, $entityFactory) diff --git a/src/Spout/Reader/XLSX/Manager/StyleManager.php b/src/Spout/Reader/XLSX/Manager/StyleManager.php index fa33331..9232ef1 100644 --- a/src/Spout/Reader/XLSX/Manager/StyleManager.php +++ b/src/Spout/Reader/XLSX/Manager/StyleManager.php @@ -2,7 +2,7 @@ namespace Box\Spout\Reader\XLSX\Manager; -use Box\Spout\Reader\XLSX\Creator\EntityFactory; +use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; /** * Class StyleManager @@ -51,7 +51,7 @@ class StyleManager /** @var string Path of the styles XML file */ protected $stylesXMLFilePath; - /** @var EntityFactory Factory to create entities */ + /** @var InternalEntityFactory Factory to create entities */ protected $entityFactory; /** @var array Array containing the IDs of built-in number formats indicating a date */ @@ -69,7 +69,7 @@ class StyleManager /** * @param string $filePath Path of the XLSX file being read * @param WorkbookRelationshipsManager $workbookRelationshipsManager Helps retrieving workbook relationships - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities */ public function __construct($filePath, $workbookRelationshipsManager, $entityFactory) { diff --git a/src/Spout/Reader/XLSX/Manager/WorkbookRelationshipsManager.php b/src/Spout/Reader/XLSX/Manager/WorkbookRelationshipsManager.php index 17877b7..2a9b4e0 100644 --- a/src/Spout/Reader/XLSX/Manager/WorkbookRelationshipsManager.php +++ b/src/Spout/Reader/XLSX/Manager/WorkbookRelationshipsManager.php @@ -4,7 +4,7 @@ namespace Box\Spout\Reader\XLSX\Manager; use Box\Spout\Common\Exception\IOException; use Box\Spout\Reader\Wrapper\XMLReader; -use Box\Spout\Reader\XLSX\Creator\EntityFactory; +use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; /** * Class WorkbookRelationshipsManager @@ -30,7 +30,7 @@ class WorkbookRelationshipsManager /** @var string Path of the XLSX file being read */ private $filePath; - /** @var EntityFactory Factory to create entities */ + /** @var InternalEntityFactory Factory to create entities */ private $entityFactory; /** @var array Cache of the already read workbook relationships: [TYPE] => [FILE_NAME] */ @@ -38,7 +38,7 @@ class WorkbookRelationshipsManager /** * @param string $filePath Path of the XLSX file being read - * @param EntityFactory $entityFactory Factory to create entities + * @param InternalEntityFactory $entityFactory Factory to create entities */ public function __construct($filePath, $entityFactory) { diff --git a/src/Spout/Reader/XLSX/Reader.php b/src/Spout/Reader/XLSX/Reader.php index eb44cce..2e8815e 100644 --- a/src/Spout/Reader/XLSX/Reader.php +++ b/src/Spout/Reader/XLSX/Reader.php @@ -5,10 +5,10 @@ namespace Box\Spout\Reader\XLSX; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Manager\OptionsManagerInterface; -use Box\Spout\Reader\Common\Creator\EntityFactoryInterface; +use Box\Spout\Reader\Common\Creator\InternalEntityFactoryInterface; use Box\Spout\Reader\Common\Entity\Options; use Box\Spout\Reader\ReaderAbstract; -use Box\Spout\Reader\XLSX\Creator\EntityFactory; +use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; use Box\Spout\Reader\XLSX\Creator\ManagerFactory; /** @@ -32,13 +32,13 @@ class Reader extends ReaderAbstract /** * @param OptionsManagerInterface $optionsManager * @param GlobalFunctionsHelper $globalFunctionsHelper - * @param EntityFactoryInterface $entityFactory + * @param InternalEntityFactoryInterface $entityFactory * @param ManagerFactory $managerFactory */ public function __construct( OptionsManagerInterface $optionsManager, GlobalFunctionsHelper $globalFunctionsHelper, - EntityFactoryInterface $entityFactory, + InternalEntityFactoryInterface $entityFactory, ManagerFactory $managerFactory ) { parent::__construct($optionsManager, $globalFunctionsHelper, $entityFactory); @@ -78,7 +78,7 @@ class Reader extends ReaderAbstract */ protected function openReader($filePath) { - /** @var EntityFactory $entityFactory */ + /** @var InternalEntityFactory $entityFactory */ $entityFactory = $this->entityFactory; $this->zip = $entityFactory->createZipArchive(); diff --git a/src/Spout/Writer/Common/Creator/EntityFactory.php b/src/Spout/Writer/Common/Creator/EntityFactory.php index 54daa6c..76bf8da 100644 --- a/src/Spout/Writer/Common/Creator/EntityFactory.php +++ b/src/Spout/Writer/Common/Creator/EntityFactory.php @@ -14,7 +14,7 @@ use Box\Spout\Writer\WriterInterface; class EntityFactory { /** - * This creates an instance of the appropriate writer, given the type of the file to be read + * This creates an instance of the appropriate writer, given the type of the file to be written * * @param string $writerType Type of the writer to instantiate * @throws \Box\Spout\Common\Exception\UnsupportedTypeException diff --git a/src/Spout/Writer/Common/Entity/Cell.php b/src/Spout/Writer/Common/Entity/Cell.php index b03bf03..caeac53 100644 --- a/src/Spout/Writer/Common/Entity/Cell.php +++ b/src/Spout/Writer/Common/Entity/Cell.php @@ -2,8 +2,8 @@ namespace Box\Spout\Writer\Common\Entity; +use Box\Spout\Common\Helper\CellTypeHelper; use Box\Spout\Writer\Common\Entity\Style\Style; -use Box\Spout\Writer\Common\Helper\CellHelper; /** * Class Cell @@ -118,16 +118,16 @@ class Cell */ protected function detectType($value) { - if (CellHelper::isBoolean($value)) { + if (CellTypeHelper::isBoolean($value)) { return self::TYPE_BOOLEAN; } - if (CellHelper::isEmpty($value)) { + if (CellTypeHelper::isEmpty($value)) { return self::TYPE_EMPTY; } - if (CellHelper::isNumeric($this->getValue())) { + if (CellTypeHelper::isNumeric($this->getValue())) { return self::TYPE_NUMERIC; } - if (CellHelper::isNonEmptyString($value)) { + if (CellTypeHelper::isNonEmptyString($value)) { return self::TYPE_STRING; } diff --git a/src/Spout/Writer/Common/Helper/CellHelper.php b/src/Spout/Writer/Common/Helper/CellHelper.php index df935a0..472d310 100644 --- a/src/Spout/Writer/Common/Helper/CellHelper.php +++ b/src/Spout/Writer/Common/Helper/CellHelper.php @@ -42,48 +42,4 @@ class CellHelper return self::$columnIndexToCellIndexCache[$originalColumnIndex]; } - - /** - * @param $value - * @return bool Whether the given value is considered "empty" - */ - public static function isEmpty($value) - { - return ($value === null || $value === ''); - } - - /** - * @param $value - * @return bool Whether the given value is a non empty string - */ - public static function isNonEmptyString($value) - { - return (gettype($value) === 'string' && $value !== ''); - } - - /** - * Returns whether the given value is numeric. - * A numeric value is from type "integer" or "double" ("float" is not returned by gettype). - * - * @param $value - * @return bool Whether the given value is numeric - */ - public static function isNumeric($value) - { - $valueType = gettype($value); - - return ($valueType === 'integer' || $valueType === 'double'); - } - - /** - * Returns whether the given value is boolean. - * "true"/"false" and 0/1 are not booleans. - * - * @param $value - * @return bool Whether the given value is boolean - */ - public static function isBoolean($value) - { - return gettype($value) === 'boolean'; - } } diff --git a/tests/Spout/Common/Helper/CellTypeHelperTest.php b/tests/Spout/Common/Helper/CellTypeHelperTest.php new file mode 100644 index 0000000..f0d1c7a --- /dev/null +++ b/tests/Spout/Common/Helper/CellTypeHelperTest.php @@ -0,0 +1,84 @@ +assertTrue(CellTypeHelper::isEmpty(null)); + $this->assertTrue(CellTypeHelper::isEmpty('')); + + $this->assertFalse(CellTypeHelper::isEmpty('string')); + $this->assertFalse(CellTypeHelper::isEmpty(0)); + $this->assertFalse(CellTypeHelper::isEmpty(1)); + $this->assertFalse(CellTypeHelper::isEmpty(true)); + $this->assertFalse(CellTypeHelper::isEmpty(false)); + $this->assertFalse(CellTypeHelper::isEmpty(['string'])); + $this->assertFalse(CellTypeHelper::isEmpty(new \stdClass())); + } + + /** + * @return array + */ + public function testIsNonEmptyString() + { + $this->assertTrue(CellTypeHelper::isNonEmptyString('string')); + + $this->assertFalse(CellTypeHelper::isNonEmptyString('')); + $this->assertFalse(CellTypeHelper::isNonEmptyString(0)); + $this->assertFalse(CellTypeHelper::isNonEmptyString(1)); + $this->assertFalse(CellTypeHelper::isNonEmptyString(true)); + $this->assertFalse(CellTypeHelper::isNonEmptyString(false)); + $this->assertFalse(CellTypeHelper::isNonEmptyString(['string'])); + $this->assertFalse(CellTypeHelper::isNonEmptyString(new \stdClass())); + $this->assertFalse(CellTypeHelper::isNonEmptyString(null)); + } + + /** + * @return array + */ + public function testIsNumeric() + { + $this->assertTrue(CellTypeHelper::isNumeric(0)); + $this->assertTrue(CellTypeHelper::isNumeric(10)); + $this->assertTrue(CellTypeHelper::isNumeric(10.1)); + $this->assertTrue(CellTypeHelper::isNumeric(10.10000000000000000000001)); + $this->assertTrue(CellTypeHelper::isNumeric(0x539)); + $this->assertTrue(CellTypeHelper::isNumeric(02471)); + $this->assertTrue(CellTypeHelper::isNumeric(0b10100111001)); + $this->assertTrue(CellTypeHelper::isNumeric(1337e0)); + + $this->assertFalse(CellTypeHelper::isNumeric('0')); + $this->assertFalse(CellTypeHelper::isNumeric('42')); + $this->assertFalse(CellTypeHelper::isNumeric(true)); + $this->assertFalse(CellTypeHelper::isNumeric([2])); + $this->assertFalse(CellTypeHelper::isNumeric(new \stdClass())); + $this->assertFalse(CellTypeHelper::isNumeric(null)); + } + + /** + * @return array + */ + public function testIsBoolean() + { + $this->assertTrue(CellTypeHelper::isBoolean(true)); + $this->assertTrue(CellTypeHelper::isBoolean(false)); + + $this->assertFalse(CellTypeHelper::isBoolean(0)); + $this->assertFalse(CellTypeHelper::isBoolean(1)); + $this->assertFalse(CellTypeHelper::isBoolean('0')); + $this->assertFalse(CellTypeHelper::isBoolean('1')); + $this->assertFalse(CellTypeHelper::isBoolean('true')); + $this->assertFalse(CellTypeHelper::isBoolean('false')); + $this->assertFalse(CellTypeHelper::isBoolean([true])); + $this->assertFalse(CellTypeHelper::isBoolean(new \stdClass())); + $this->assertFalse(CellTypeHelper::isBoolean(null)); + } +} diff --git a/tests/Spout/Reader/CSV/ReaderTest.php b/tests/Spout/Reader/CSV/ReaderTest.php index da53cf3..6605394 100644 --- a/tests/Spout/Reader/CSV/ReaderTest.php +++ b/tests/Spout/Reader/CSV/ReaderTest.php @@ -6,7 +6,7 @@ use Box\Spout\Common\Creator\HelperFactory; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Helper\EncodingHelper; use Box\Spout\Common\Helper\GlobalFunctionsHelper; -use Box\Spout\Reader\CSV\Creator\EntityFactory; +use Box\Spout\Reader\CSV\Creator\InternalEntityFactory; use Box\Spout\Reader\CSV\Manager\OptionsManager; use Box\Spout\Reader\Exception\ReaderNotOpenedException; use Box\Spout\Reader\ReaderInterface; @@ -320,7 +320,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase foreach ($reader->getSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $row) { - $allRows[] = $row; + $allRows[] = $row->toArray(); } } @@ -351,19 +351,19 @@ class ReaderTest extends \PHPUnit_Framework_TestCase foreach ($reader->getSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $row) { - $allRows[] = $row; + $allRows[] = $row->toArray(); break; } foreach ($sheet->getRowIterator() as $row) { - $allRows[] = $row; + $allRows[] = $row->toArray(); break; } } foreach ($reader->getSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $row) { - $allRows[] = $row; + $allRows[] = $row->toArray(); break; } } @@ -444,7 +444,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase foreach ($reader->getSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $row) { - $allRows[] = $row; + $allRows[] = $row->toArray(); } } @@ -482,7 +482,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase { $optionsManager = $optionsManager ?: new OptionsManager(); $globalFunctionsHelper = $globalFunctionsHelper ?: new GlobalFunctionsHelper(); - $entityFactory = new EntityFactory(new HelperFactory()); + $entityFactory = new InternalEntityFactory(new HelperFactory()); return new Reader($optionsManager, $globalFunctionsHelper, $entityFactory); } @@ -516,7 +516,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase foreach ($reader->getSheetIterator() as $sheetIndex => $sheet) { foreach ($sheet->getRowIterator() as $rowIndex => $row) { - $allRows[] = $row; + $allRows[] = $row->toArray(); } } diff --git a/tests/Spout/Reader/CSV/SheetTest.php b/tests/Spout/Reader/CSV/SheetTest.php index 8812d89..e980de0 100644 --- a/tests/Spout/Reader/CSV/SheetTest.php +++ b/tests/Spout/Reader/CSV/SheetTest.php @@ -3,7 +3,7 @@ namespace Box\Spout\Reader\CSV; use Box\Spout\Common\Type; -use Box\Spout\Reader\ReaderFactory; +use Box\Spout\Reader\Common\Creator\EntityFactory; use Box\Spout\TestUsingResource; /** @@ -32,7 +32,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase private function openFileAndReturnSheet($fileName) { $resourcePath = $this->getResourcePath($fileName); - $reader = ReaderFactory::create(Type::CSV); + $reader = EntityFactory::createReader(Type::CSV); $reader->open($resourcePath); $sheet = $reader->getSheetIterator()->current(); diff --git a/tests/Spout/Reader/Common/Entity/CellTest.php b/tests/Spout/Reader/Common/Entity/CellTest.php new file mode 100644 index 0000000..e846f26 --- /dev/null +++ b/tests/Spout/Reader/Common/Entity/CellTest.php @@ -0,0 +1,64 @@ +assertInstanceOf(Cell::class, new Cell('cell')); + } + + /** + * @return void + */ + public function testCellTypeNumeric() + { + $this->assertTrue((new Cell(0))->isNumeric()); + $this->assertTrue((new Cell(1))->isNumeric()); + } + + /** + * @return void + */ + public function testCellTypeString() + { + $this->assertTrue((new Cell('String!'))->isString()); + } + + /** + * @return void + */ + public function testCellTypeEmptyString() + { + $this->assertTrue((new Cell(''))->isEmpty()); + } + + /** + * @return void + */ + public function testCellTypeEmptyNull() + { + $this->assertTrue((new Cell(null))->isEmpty()); + } + + /** + * @return void + */ + public function testCellTypeBool() + { + $this->assertTrue((new Cell(true))->isBoolean()); + $this->assertTrue((new Cell(false))->isBoolean()); + } + + /** + * @return void + */ + public function testCellTypeError() + { + $this->assertTrue((new Cell([]))->isError()); + } +} diff --git a/tests/Spout/Reader/Common/Entity/RowTest.php b/tests/Spout/Reader/Common/Entity/RowTest.php new file mode 100644 index 0000000..81a55ca --- /dev/null +++ b/tests/Spout/Reader/Common/Entity/RowTest.php @@ -0,0 +1,90 @@ +createMock(Cell::class); + } + + /** + * @return void + */ + public function testValidInstance() + { + $this->assertInstanceOf(Row::class, new Row([], null)); + } + + /** + * @return void + */ + public function testSetCells() + { + $row = new Row([], null); + $row->setCells([$this->getCellMock(), $this->getCellMock()]); + + $this->assertEquals(2, count($row->getCells())); + } + + /** + * @return void + */ + public function testSetCellsResets() + { + $row = new Row([], null); + $row->setCells([$this->getCellMock(), $this->getCellMock()]); + + $this->assertEquals(2, count($row->getCells())); + + $row->setCells([$this->getCellMock()]); + + $this->assertEquals(1, count($row->getCells())); + } + + /** + * @return void + */ + public function testGetCells() + { + $row = new Row([], null); + + $this->assertEquals(0, count($row->getCells())); + + $row->setCells([$this->getCellMock(), $this->getCellMock()]); + + $this->assertEquals(2, count($row->getCells())); + } + + /** + * @return void + */ + public function testAddCell() + { + $row = new Row([], null); + $row->setCells([$this->getCellMock(), $this->getCellMock()]); + + $this->assertEquals(2, count($row->getCells())); + + $row->addCell($this->getCellMock()); + + $this->assertEquals(3, count($row->getCells())); + } + + /** + * @return void + */ + public function testFluentInterface() + { + $row = new Row([], null); + $row + ->addCell($this->getCellMock()) + ->setCells([]); + + $this->assertTrue(is_object($row)); + } +} diff --git a/tests/Spout/Reader/ODS/ReaderTest.php b/tests/Spout/Reader/ODS/ReaderTest.php index 4be2c1f..44ad830 100644 --- a/tests/Spout/Reader/ODS/ReaderTest.php +++ b/tests/Spout/Reader/ODS/ReaderTest.php @@ -4,8 +4,8 @@ namespace Box\Spout\Reader\ODS; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Type; +use Box\Spout\Reader\Common\Creator\EntityFactory; use Box\Spout\Reader\Exception\IteratorNotRewindableException; -use Box\Spout\Reader\ReaderFactory; use Box\Spout\TestUsingResource; /** @@ -353,7 +353,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $this->expectException(IteratorNotRewindableException::class); $resourcePath = $this->getResourcePath('one_sheet_with_strings.ods'); - $reader = ReaderFactory::create(Type::ODS); + $reader = EntityFactory::createReader(Type::ODS); $reader->open($resourcePath); foreach ($reader->getSheetIterator() as $sheet) { @@ -377,7 +377,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $allRows = []; $resourcePath = $this->getResourcePath('two_sheets_with_strings.ods'); - $reader = ReaderFactory::create(Type::ODS); + $reader = EntityFactory::createReader(Type::ODS); $reader->open($resourcePath); foreach ($reader->getSheetIterator() as $sheet) { @@ -421,7 +421,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $this->expectException(IOException::class); /** @var \Box\Spout\Reader\ODS\Reader $reader */ - $reader = ReaderFactory::create(Type::ODS); + $reader = EntityFactory::createReader(Type::ODS); $reader->open('unsupported://foobar'); } @@ -433,7 +433,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $this->expectException(IOException::class); /** @var \Box\Spout\Reader\ODS\Reader $reader */ - $reader = ReaderFactory::create(Type::ODS); + $reader = EntityFactory::createReader(Type::ODS); $reader->open('php://memory'); } @@ -529,7 +529,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $resourcePath = $this->getResourcePath($fileName); /** @var \Box\Spout\Reader\ODS\Reader $reader */ - $reader = ReaderFactory::create(Type::ODS); + $reader = EntityFactory::createReader(Type::ODS); $reader->setShouldFormatDates($shouldFormatDates); $reader->setShouldPreserveEmptyRows($shouldPreserveEmptyRows); $reader->open($resourcePath); diff --git a/tests/Spout/Reader/ODS/SheetTest.php b/tests/Spout/Reader/ODS/SheetTest.php index d03a3ab..410e0a0 100644 --- a/tests/Spout/Reader/ODS/SheetTest.php +++ b/tests/Spout/Reader/ODS/SheetTest.php @@ -3,7 +3,7 @@ namespace Box\Spout\Reader\ODS; use Box\Spout\Common\Type; -use Box\Spout\Reader\ReaderFactory; +use Box\Spout\Reader\Common\Creator\EntityFactory; use Box\Spout\TestUsingResource; /** @@ -60,7 +60,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase private function openFileAndReturnSheets($fileName) { $resourcePath = $this->getResourcePath($fileName); - $reader = ReaderFactory::create(Type::ODS); + $reader = EntityFactory::createReader(Type::ODS); $reader->open($resourcePath); $sheets = []; diff --git a/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php b/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php index cecd208..d36cc95 100644 --- a/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php +++ b/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php @@ -3,8 +3,8 @@ namespace Box\Spout\Reader\XLSX\Manager; use Box\Spout\Reader\Exception\SharedStringNotFoundException; -use Box\Spout\Reader\XLSX\Creator\EntityFactory; use Box\Spout\Reader\XLSX\Creator\HelperFactory; +use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; use Box\Spout\Reader\XLSX\Creator\ManagerFactory; use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory; use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\FileBasedStrategy; @@ -50,7 +50,7 @@ class SharedStringsManagerTest extends \PHPUnit_Framework_TestCase $cachingStrategyFactory = new CachingStrategyFactory(); $helperFactory = new HelperFactory(); $managerFactory = new ManagerFactory($helperFactory, $cachingStrategyFactory); - $entityFactory = new EntityFactory($managerFactory, $helperFactory); + $entityFactory = new InternalEntityFactory($managerFactory, $helperFactory); $workbookRelationshipsManager = new WorkbookRelationshipsManager($resourcePath, $entityFactory); $this->sharedStringsManager = new SharedStringsManager( diff --git a/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php b/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php index b3b6510..4dd630c 100644 --- a/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php +++ b/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php @@ -2,7 +2,7 @@ namespace Box\Spout\Reader\XLSX\Manager; -use Box\Spout\Reader\XLSX\Creator\EntityFactory; +use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; /** * Class StyleManagerTest @@ -16,7 +16,7 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase */ private function getStyleManagerMock($styleAttributes = [], $customNumberFormats = []) { - $entityFactory = $this->createMock(EntityFactory::class); + $entityFactory = $this->createMock(InternalEntityFactory::class); $workbookRelationshipsManager = $this->createMock(WorkbookRelationshipsManager::class); /** @var StyleManager $styleManager */ diff --git a/tests/Spout/Reader/XLSX/ReaderTest.php b/tests/Spout/Reader/XLSX/ReaderTest.php index 49deac6..3908257 100644 --- a/tests/Spout/Reader/XLSX/ReaderTest.php +++ b/tests/Spout/Reader/XLSX/ReaderTest.php @@ -4,7 +4,7 @@ namespace Box\Spout\Reader\XLSX; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Type; -use Box\Spout\Reader\ReaderFactory; +use Box\Spout\Reader\Common\Creator\EntityFactory; use Box\Spout\TestUsingResource; /** @@ -570,7 +570,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $allRows = []; $resourcePath = $this->getResourcePath('two_sheets_with_inline_strings.xlsx'); - $reader = ReaderFactory::create(Type::XLSX); + $reader = EntityFactory::createReader(Type::XLSX); $reader->open($resourcePath); foreach ($reader->getSheetIterator() as $sheet) { @@ -624,7 +624,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $this->expectException(IOException::class); /** @var \Box\Spout\Reader\XLSX\Reader $reader */ - $reader = ReaderFactory::create(Type::XLSX); + $reader = EntityFactory::createReader(Type::XLSX); $reader->open('unsupported://foobar'); } @@ -636,7 +636,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $this->expectException(IOException::class); /** @var \Box\Spout\Reader\XLSX\Reader $reader */ - $reader = ReaderFactory::create(Type::XLSX); + $reader = EntityFactory::createReader(Type::XLSX); $reader->open('php://memory'); } @@ -701,7 +701,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $resourcePath = $this->getResourcePath($fileName); /** @var \Box\Spout\Reader\XLSX\Reader $reader */ - $reader = ReaderFactory::create(Type::XLSX); + $reader = EntityFactory::createReader(Type::XLSX); $reader->setShouldFormatDates($shouldFormatDates); $reader->setShouldPreserveEmptyRows($shouldPreserveEmptyRows); $reader->open($resourcePath); diff --git a/tests/Spout/Reader/XLSX/SheetTest.php b/tests/Spout/Reader/XLSX/SheetTest.php index 96eee4a..554397c 100644 --- a/tests/Spout/Reader/XLSX/SheetTest.php +++ b/tests/Spout/Reader/XLSX/SheetTest.php @@ -3,7 +3,7 @@ namespace Box\Spout\Reader\XLSX; use Box\Spout\Common\Type; -use Box\Spout\Reader\ReaderFactory; +use Box\Spout\Reader\Common\Creator\EntityFactory; use Box\Spout\TestUsingResource; /** @@ -48,7 +48,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase private function openFileAndReturnSheets($fileName) { $resourcePath = $this->getResourcePath($fileName); - $reader = ReaderFactory::create(Type::XLSX); + $reader = EntityFactory::createReader(Type::XLSX); $reader->open($resourcePath); $sheets = []; diff --git a/tests/Spout/Writer/Common/Helper/CellHelperTest.php b/tests/Spout/Writer/Common/Helper/CellHelperTest.php index 1468bfa..f763200 100644 --- a/tests/Spout/Writer/Common/Helper/CellHelperTest.php +++ b/tests/Spout/Writer/Common/Helper/CellHelperTest.php @@ -32,79 +32,4 @@ class CellHelperTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expectedCellIndex, CellHelper::getCellIndexFromColumnIndex($columnIndex)); } - - /** - * @return array - */ - public function testIsEmpty() - { - $this->assertTrue(CellHelper::isEmpty(null)); - $this->assertTrue(CellHelper::isEmpty('')); - - $this->assertFalse(CellHelper::isEmpty('string')); - $this->assertFalse(CellHelper::isEmpty(0)); - $this->assertFalse(CellHelper::isEmpty(1)); - $this->assertFalse(CellHelper::isEmpty(true)); - $this->assertFalse(CellHelper::isEmpty(false)); - $this->assertFalse(CellHelper::isEmpty(['string'])); - $this->assertFalse(CellHelper::isEmpty(new \stdClass())); - } - - /** - * @return array - */ - public function testIsNonEmptyString() - { - $this->assertTrue(CellHelper::isNonEmptyString('string')); - - $this->assertFalse(CellHelper::isNonEmptyString('')); - $this->assertFalse(CellHelper::isNonEmptyString(0)); - $this->assertFalse(CellHelper::isNonEmptyString(1)); - $this->assertFalse(CellHelper::isNonEmptyString(true)); - $this->assertFalse(CellHelper::isNonEmptyString(false)); - $this->assertFalse(CellHelper::isNonEmptyString(['string'])); - $this->assertFalse(CellHelper::isNonEmptyString(new \stdClass())); - $this->assertFalse(CellHelper::isNonEmptyString(null)); - } - - /** - * @return array - */ - public function testIsNumeric() - { - $this->assertTrue(CellHelper::isNumeric(0)); - $this->assertTrue(CellHelper::isNumeric(10)); - $this->assertTrue(CellHelper::isNumeric(10.1)); - $this->assertTrue(CellHelper::isNumeric(10.10000000000000000000001)); - $this->assertTrue(CellHelper::isNumeric(0x539)); - $this->assertTrue(CellHelper::isNumeric(02471)); - $this->assertTrue(CellHelper::isNumeric(0b10100111001)); - $this->assertTrue(CellHelper::isNumeric(1337e0)); - - $this->assertFalse(CellHelper::isNumeric('0')); - $this->assertFalse(CellHelper::isNumeric('42')); - $this->assertFalse(CellHelper::isNumeric(true)); - $this->assertFalse(CellHelper::isNumeric([2])); - $this->assertFalse(CellHelper::isNumeric(new \stdClass())); - $this->assertFalse(CellHelper::isNumeric(null)); - } - - /** - * @return array - */ - public function testIsBoolean() - { - $this->assertTrue(CellHelper::isBoolean(true)); - $this->assertTrue(CellHelper::isBoolean(false)); - - $this->assertFalse(CellHelper::isBoolean(0)); - $this->assertFalse(CellHelper::isBoolean(1)); - $this->assertFalse(CellHelper::isBoolean('0')); - $this->assertFalse(CellHelper::isBoolean('1')); - $this->assertFalse(CellHelper::isBoolean('true')); - $this->assertFalse(CellHelper::isBoolean('false')); - $this->assertFalse(CellHelper::isBoolean([true])); - $this->assertFalse(CellHelper::isBoolean(new \stdClass())); - $this->assertFalse(CellHelper::isBoolean(null)); - } }