From 8f1387d16f648c10018aa770db8f2553f3bd526c Mon Sep 17 00:00:00 2001 From: Salavat Salakhutdinov Date: Sat, 31 Oct 2020 06:20:43 +0300 Subject: [PATCH] extract formula helper to CellFormulaFormatter class --- .../Reader/XLSX/Creator/HelperFactory.php | 8 ++++++ .../XLSX/Creator/InternalEntityFactory.php | 2 ++ .../XLSX/Helper/CellFormulaFormatter.php | 28 +++++++++++++++++++ .../Reader/XLSX/Helper/CellValueFormatter.php | 18 +----------- src/Spout/Reader/XLSX/RowIterator.php | 8 +++++- 5 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 src/Spout/Reader/XLSX/Helper/CellFormulaFormatter.php diff --git a/src/Spout/Reader/XLSX/Creator/HelperFactory.php b/src/Spout/Reader/XLSX/Creator/HelperFactory.php index 80ee692..e1304bf 100644 --- a/src/Spout/Reader/XLSX/Creator/HelperFactory.php +++ b/src/Spout/Reader/XLSX/Creator/HelperFactory.php @@ -4,6 +4,7 @@ namespace Box\Spout\Reader\XLSX\Creator; use Box\Spout\Common\Helper\Escaper; use Box\Spout\Reader\XLSX\Helper\CellValueFormatter; +use Box\Spout\Reader\XLSX\Helper\CellFormulaFormatter; use Box\Spout\Reader\XLSX\Manager\SharedStringsManager; use Box\Spout\Reader\XLSX\Manager\StyleManager; @@ -26,6 +27,13 @@ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory return new CellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates, $shouldUse1904Dates, $escaper); } + /** + * @return CellFormulaFormatter + */ + public function createCellFormulaFormatter() + { + return new CellFormulaFormatter(); + } /** * @return Escaper\XLSX diff --git a/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php b/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php index 4dc2dc7..74f5b8d 100644 --- a/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php +++ b/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php @@ -102,6 +102,7 @@ class InternalEntityFactory implements InternalEntityFactoryInterface $shouldFormatDates, $shouldUse1904Dates ); + $cellFormulaFormatter = $this->helperFactory->createCellFormulaFormatter(); $shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS); @@ -112,6 +113,7 @@ class InternalEntityFactory implements InternalEntityFactoryInterface $xmlReader, $xmlProcessor, $cellValueFormatter, + $cellFormulaFormatter, $rowManager, $this ); diff --git a/src/Spout/Reader/XLSX/Helper/CellFormulaFormatter.php b/src/Spout/Reader/XLSX/Helper/CellFormulaFormatter.php new file mode 100644 index 0000000..a683837 --- /dev/null +++ b/src/Spout/Reader/XLSX/Helper/CellFormulaFormatter.php @@ -0,0 +1,28 @@ +getElementsByTagName(self::XML_NODE_FORMULA)->item(0); + + return ($vNode !== null) ? $vNode->nodeValue : ''; + } +} \ No newline at end of file diff --git a/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php b/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php index baf39a2..a0f2f6d 100644 --- a/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php +++ b/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php @@ -23,7 +23,6 @@ class CellValueFormatter /** Definition of XML nodes names used to parse data */ const XML_NODE_VALUE = 'v'; - const XML_NODE_FORMULA = 'f'; const XML_NODE_INLINE_STRING_VALUE = 't'; /** Definition of XML attributes used to parse data */ @@ -107,22 +106,7 @@ class CellValueFormatter throw new InvalidValueException($vNodeValue); } } - - /** - * Returns the cell formula associated to the given XML node. - * - * @param \DOMNode $node - * @return string The formula associated with the cell - */ - public function extractNodeFormula($node) - { - // for cell types having a "f" tag containing the formula. - // if not, the returned formula should be empty string. - $vNode = $node->getElementsByTagName(self::XML_NODE_FORMULA)->item(0); - - return ($vNode !== null) ? $vNode->nodeValue : ''; - } - + /** * Returns the cell's string value from a node's nested value node * diff --git a/src/Spout/Reader/XLSX/RowIterator.php b/src/Spout/Reader/XLSX/RowIterator.php index 4395f2b..283f15e 100644 --- a/src/Spout/Reader/XLSX/RowIterator.php +++ b/src/Spout/Reader/XLSX/RowIterator.php @@ -14,6 +14,7 @@ use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; use Box\Spout\Reader\XLSX\Helper\CellHelper; use Box\Spout\Reader\XLSX\Helper\CellValueFormatter; +use Box\Spout\Reader\XLSX\Helper\CellFormulaFormatter; /** * Class RowIterator @@ -47,6 +48,9 @@ class RowIterator implements IteratorInterface /** @var Helper\CellValueFormatter Helper to format cell values */ protected $cellValueFormatter; + /** @var Helper\CellFormulaFormatter Helper to format cell formulas */ + protected $cellFormulaFormatter; + /** @var \Box\Spout\Reader\Common\Manager\RowManager Manages rows */ protected $rowManager; @@ -100,6 +104,7 @@ class RowIterator implements IteratorInterface $xmlReader, XMLProcessor $xmlProcessor, CellValueFormatter $cellValueFormatter, + CellFormulaFormatter $cellFormulaFormatter, RowManager $rowManager, InternalEntityFactory $entityFactory ) { @@ -108,6 +113,7 @@ class RowIterator implements IteratorInterface $this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; $this->xmlReader = $xmlReader; $this->cellValueFormatter = $cellValueFormatter; + $this->cellFormulaFormatter = $cellFormulaFormatter; $this->rowManager = $rowManager; $this->entityFactory = $entityFactory; @@ -359,7 +365,7 @@ class RowIterator implements IteratorInterface { try { $cellValue = $this->cellValueFormatter->extractAndFormatNodeValue($node); - $cellFormula = $this->cellValueFormatter->extractNodeFormula($node); + $cellFormula = $this->cellFormulaFormatter->extractNodeFormula($node); $cell = $this->entityFactory->createCell($cellValue); $cell->setFormula($cellFormula); } catch (InvalidValueException $exception) {