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\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

View File

@ -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
);

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 */
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 */
@ -108,21 +107,6 @@ class CellValueFormatter
}
}
/**
* 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
*

View File

@ -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) {