extract formula helper to CellFormulaFormatter class

This commit is contained in:
Salavat Salakhutdinov 2020-10-31 06:20:43 +03:00
parent f8baef148f
commit 8f1387d16f
5 changed files with 46 additions and 18 deletions

View File

@ -4,6 +4,7 @@ namespace Box\Spout\Reader\XLSX\Creator;
use Box\Spout\Common\Helper\Escaper; use Box\Spout\Common\Helper\Escaper;
use Box\Spout\Reader\XLSX\Helper\CellValueFormatter; 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\SharedStringsManager;
use Box\Spout\Reader\XLSX\Manager\StyleManager; 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 new CellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates, $shouldUse1904Dates, $escaper);
} }
/**
* @return CellFormulaFormatter
*/
public function createCellFormulaFormatter()
{
return new CellFormulaFormatter();
}
/** /**
* @return Escaper\XLSX * @return Escaper\XLSX

View File

@ -102,6 +102,7 @@ class InternalEntityFactory implements InternalEntityFactoryInterface
$shouldFormatDates, $shouldFormatDates,
$shouldUse1904Dates $shouldUse1904Dates
); );
$cellFormulaFormatter = $this->helperFactory->createCellFormulaFormatter();
$shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS); $shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);
@ -112,6 +113,7 @@ class InternalEntityFactory implements InternalEntityFactoryInterface
$xmlReader, $xmlReader,
$xmlProcessor, $xmlProcessor,
$cellValueFormatter, $cellValueFormatter,
$cellFormulaFormatter,
$rowManager, $rowManager,
$this $this
); );

View File

@ -0,0 +1,28 @@
<?php
namespace Box\Spout\Reader\XLSX\Helper;
/**
* Class CellFormulaFormatter
* This class provides helper functions to format cell formulas
*/
class CellFormulaFormatter
{
/** Definition of XML nodes names used to parse data */
const XML_NODE_FORMULA = 'f';
/**
* 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 : '';
}
}

View File

@ -23,7 +23,6 @@ class CellValueFormatter
/** Definition of XML nodes names used to parse data */ /** Definition of XML nodes names used to parse data */
const XML_NODE_VALUE = 'v'; const XML_NODE_VALUE = 'v';
const XML_NODE_FORMULA = 'f';
const XML_NODE_INLINE_STRING_VALUE = 't'; const XML_NODE_INLINE_STRING_VALUE = 't';
/** Definition of XML attributes used to parse data */ /** Definition of XML attributes used to parse data */
@ -107,22 +106,7 @@ class CellValueFormatter
throw new InvalidValueException($vNodeValue); 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 * Returns the cell's string value from a node's nested value node
* *

View File

@ -14,6 +14,7 @@ use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory;
use Box\Spout\Reader\XLSX\Helper\CellHelper; use Box\Spout\Reader\XLSX\Helper\CellHelper;
use Box\Spout\Reader\XLSX\Helper\CellValueFormatter; use Box\Spout\Reader\XLSX\Helper\CellValueFormatter;
use Box\Spout\Reader\XLSX\Helper\CellFormulaFormatter;
/** /**
* Class RowIterator * Class RowIterator
@ -47,6 +48,9 @@ class RowIterator implements IteratorInterface
/** @var Helper\CellValueFormatter Helper to format cell values */ /** @var Helper\CellValueFormatter Helper to format cell values */
protected $cellValueFormatter; protected $cellValueFormatter;
/** @var Helper\CellFormulaFormatter Helper to format cell formulas */
protected $cellFormulaFormatter;
/** @var \Box\Spout\Reader\Common\Manager\RowManager Manages rows */ /** @var \Box\Spout\Reader\Common\Manager\RowManager Manages rows */
protected $rowManager; protected $rowManager;
@ -100,6 +104,7 @@ class RowIterator implements IteratorInterface
$xmlReader, $xmlReader,
XMLProcessor $xmlProcessor, XMLProcessor $xmlProcessor,
CellValueFormatter $cellValueFormatter, CellValueFormatter $cellValueFormatter,
CellFormulaFormatter $cellFormulaFormatter,
RowManager $rowManager, RowManager $rowManager,
InternalEntityFactory $entityFactory InternalEntityFactory $entityFactory
) { ) {
@ -108,6 +113,7 @@ class RowIterator implements IteratorInterface
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows; $this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows;
$this->xmlReader = $xmlReader; $this->xmlReader = $xmlReader;
$this->cellValueFormatter = $cellValueFormatter; $this->cellValueFormatter = $cellValueFormatter;
$this->cellFormulaFormatter = $cellFormulaFormatter;
$this->rowManager = $rowManager; $this->rowManager = $rowManager;
$this->entityFactory = $entityFactory; $this->entityFactory = $entityFactory;
@ -359,7 +365,7 @@ class RowIterator implements IteratorInterface
{ {
try { try {
$cellValue = $this->cellValueFormatter->extractAndFormatNodeValue($node); $cellValue = $this->cellValueFormatter->extractAndFormatNodeValue($node);
$cellFormula = $this->cellValueFormatter->extractNodeFormula($node); $cellFormula = $this->cellFormulaFormatter->extractNodeFormula($node);
$cell = $this->entityFactory->createCell($cellValue); $cell = $this->entityFactory->createCell($cellValue);
$cell->setFormula($cellFormula); $cell->setFormula($cellFormula);
} catch (InvalidValueException $exception) { } catch (InvalidValueException $exception) {