Introduce Managers for readers

Some helper classes were more managers than helpers...
This commit is contained in:
Adrien Loison 2017-08-27 03:44:51 +02:00
parent 61f2addefa
commit 696492e6d9
19 changed files with 376 additions and 339 deletions

View File

@ -4,9 +4,8 @@ namespace Box\Spout\Reader;
use Box\Spout\Common\Creator\HelperFactory; use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Exception\UnsupportedTypeException; use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory; use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
/** /**
* Class ReaderFactory * Class ReaderFactory
@ -56,12 +55,12 @@ class ReaderFactory
private static function getXLSXReader() private static function getXLSXReader()
{ {
$optionsManager = new XLSX\Manager\OptionsManager(); $optionsManager = new XLSX\Manager\OptionsManager();
$cachingStrategyFactory = new CachingStrategyFactory(); $helperFactory = new XLSX\Creator\HelperFactory();
$helperFactory = new XLSX\Creator\HelperFactory($cachingStrategyFactory); $managerFactory = new XLSX\Creator\ManagerFactory($helperFactory, new CachingStrategyFactory());
$entityFactory = new XLSX\Creator\EntityFactory($helperFactory); $entityFactory = new XLSX\Creator\EntityFactory($managerFactory, $helperFactory);
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
return new XLSX\Reader($optionsManager, $globalFunctionsHelper, $entityFactory, $helperFactory); return new XLSX\Reader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory);
} }
/** /**

View File

@ -5,11 +5,12 @@ namespace Box\Spout\Reader\XLSX\Creator;
use Box\Spout\Reader\Common\Creator\EntityFactoryInterface; use Box\Spout\Reader\Common\Creator\EntityFactoryInterface;
use Box\Spout\Reader\Common\Entity\Options; use Box\Spout\Reader\Common\Entity\Options;
use Box\Spout\Reader\Common\XMLProcessor; use Box\Spout\Reader\Common\XMLProcessor;
use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper; use Box\Spout\Reader\XLSX\Manager\SharedStringsManager;
use Box\Spout\Reader\XLSX\RowIterator; use Box\Spout\Reader\XLSX\RowIterator;
use Box\Spout\Reader\XLSX\Sheet; use Box\Spout\Reader\XLSX\Sheet;
use Box\Spout\Reader\XLSX\SheetIterator; use Box\Spout\Reader\XLSX\SheetIterator;
use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\Wrapper\XMLReader;
use MongoDB\Driver\Manager;
/** /**
* Class EntityFactory * Class EntityFactory
@ -22,24 +23,29 @@ class EntityFactory implements EntityFactoryInterface
/** @var HelperFactory */ /** @var HelperFactory */
private $helperFactory; private $helperFactory;
/** @var ManagerFactory */
private $managerFactory;
/** /**
* @param ManagerFactory $managerFactory
* @param HelperFactory $helperFactory * @param HelperFactory $helperFactory
*/ */
public function __construct(HelperFactory $helperFactory) public function __construct(ManagerFactory $managerFactory, HelperFactory $helperFactory)
{ {
$this->managerFactory = $managerFactory;
$this->helperFactory = $helperFactory; $this->helperFactory = $helperFactory;
} }
/** /**
* @param string $filePath Path of the file to be read * @param string $filePath Path of the file to be read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings * @param SharedStringsManager $sharedStringsManager Manages shared strings
* @return SheetIterator * @return SheetIterator
*/ */
public function createSheetIterator($filePath, $optionsManager, $sharedStringsHelper) public function createSheetIterator($filePath, $optionsManager, $sharedStringsManager)
{ {
$sheetHelper = $this->helperFactory->createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $this); $sheetManager = $this->managerFactory->createSheetManager($filePath, $optionsManager, $sharedStringsManager, $this);
return new SheetIterator($sheetHelper); return new SheetIterator($sheetManager);
} }
/** /**
@ -49,7 +55,7 @@ class EntityFactory implements EntityFactoryInterface
* @param string $sheetName Name of the sheet * @param string $sheetName Name of the sheet
* @param bool $isSheetActive Whether the sheet was defined as active * @param bool $isSheetActive Whether the sheet was defined as active
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings * @param SharedStringsManager $sharedStringsManager Manages shared strings
* @return Sheet * @return Sheet
*/ */
public function createSheet( public function createSheet(
@ -59,9 +65,9 @@ class EntityFactory implements EntityFactoryInterface
$sheetName, $sheetName,
$isSheetActive, $isSheetActive,
$optionsManager, $optionsManager,
$sharedStringsHelper) $sharedStringsManager)
{ {
$rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper); $rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsManager);
return new Sheet($rowIterator, $sheetIndex, $sheetName, $isSheetActive); return new Sheet($rowIterator, $sheetIndex, $sheetName, $isSheetActive);
} }
@ -69,17 +75,17 @@ class EntityFactory implements EntityFactoryInterface
* @param string $filePath Path of the XLSX file being read * @param string $filePath Path of the XLSX file being read
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings * @param SharedStringsManager $sharedStringsManager Manages shared strings
* @return RowIterator * @return RowIterator
*/ */
private function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper) private function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsManager)
{ {
$xmlReader = $this->createXMLReader(); $xmlReader = $this->createXMLReader();
$xmlProcessor = $this->createXMLProcessor($xmlReader); $xmlProcessor = $this->createXMLProcessor($xmlReader);
$styleHelper = $this->helperFactory->createStyleHelper($filePath, $this); $styleManager = $this->managerFactory->createStyleManager($filePath, $this);
$shouldFormatDates = $optionsManager->getOption(Options::SHOULD_FORMAT_DATES); $shouldFormatDates = $optionsManager->getOption(Options::SHOULD_FORMAT_DATES);
$cellValueFormatter = $this->helperFactory->createCellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates); $cellValueFormatter = $this->helperFactory->createCellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates);
$shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS); $shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);

View File

@ -4,75 +4,30 @@ 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\SharedStringsCaching\CachingStrategyFactory; use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper; use Box\Spout\Reader\XLSX\Manager\SharedStringsManager;
use Box\Spout\Reader\XLSX\Helper\SheetHelper; use Box\Spout\Reader\XLSX\Manager\SheetManager;
use Box\Spout\Reader\XLSX\Helper\StyleHelper; use Box\Spout\Reader\XLSX\Manager\StyleManager;
/** /**
* Class EntityFactory * Class HelperFactory
* Factory to create helpers * Factory to create helpers
* *
* @package Box\Spout\Reader\XLSX\Creator * @package Box\Spout\Reader\XLSX\Creator
*/ */
class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory
{ {
/** @var CachingStrategyFactory */
private $cachingStrategyFactory;
/** /**
* @param CachingStrategyFactory $cachingStrategyFactory Factory to create shared strings caching strategies * @param SharedStringsManager $sharedStringsManager Manages shared strings
*/ * @param StyleManager $styleManager Manages styles
public function __construct(CachingStrategyFactory $cachingStrategyFactory)
{
$this->cachingStrategyFactory = $cachingStrategyFactory;
}
/**
* @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
* @return SharedStringsHelper
*/
public function createSharedStringsHelper($filePath, $tempFolder, $entityFactory)
{
return new SharedStringsHelper($filePath, $tempFolder, $entityFactory, $this, $this->cachingStrategyFactory);
}
/**
* @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\Helper\SharedStringsHelper Helper to work with shared strings
* @param EntityFactory $entityFactory Factory to create entities
* @return SheetHelper
*/
public function createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $entityFactory)
{
$escaper = $this->createStringsEscaper();
return new SheetHelper($filePath, $optionsManager, $sharedStringsHelper, $escaper, $entityFactory);
}
/**
* @param string $filePath Path of the XLSX file being read
* @param EntityFactory $entityFactory Factory to create entities
* @return StyleHelper
*/
public function createStyleHelper($filePath, $entityFactory)
{
return new StyleHelper($filePath, $entityFactory);
}
/**
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @param StyleHelper $styleHelper Helper to work with styles
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings
* @return CellValueFormatter * @return CellValueFormatter
*/ */
public function createCellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates) public function createCellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates)
{ {
$escaper = $this->createStringsEscaper(); $escaper = $this->createStringsEscaper();
return new CellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates, $escaper); return new CellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates, $escaper);
} }
/** /**

View File

@ -0,0 +1,68 @@
<?php
namespace Box\Spout\Reader\XLSX\Creator;
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
use Box\Spout\Reader\XLSX\Manager\SharedStringsManager;
use Box\Spout\Reader\XLSX\Manager\SheetManager;
use Box\Spout\Reader\XLSX\Manager\StyleManager;
/**
* Class ManagerFactory
* Factory to create managers
*
* @package Box\Spout\Reader\XLSX\Creator
*/
class ManagerFactory
{
/** @var HelperFactory */
private $helperFactory;
/** @var CachingStrategyFactory */
private $cachingStrategyFactory;
/**
* @param HelperFactory $helperFactory Factory to create helpers
* @param CachingStrategyFactory $cachingStrategyFactory Factory to create shared strings caching strategies
*/
public function __construct(HelperFactory $helperFactory, CachingStrategyFactory $cachingStrategyFactory)
{
$this->helperFactory = $helperFactory;
$this->cachingStrategyFactory = $cachingStrategyFactory;
}
/**
* @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
* @return SharedStringsManager
*/
public function createSharedStringsManager($filePath, $tempFolder, $entityFactory)
{
return new SharedStringsManager($filePath, $tempFolder, $entityFactory, $this->helperFactory, $this->cachingStrategyFactory);
}
/**
* @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
* @return SheetManager
*/
public function createSheetManager($filePath, $optionsManager, $sharedStringsManager, $entityFactory)
{
$escaper = $this->helperFactory->createStringsEscaper();
return new SheetManager($filePath, $optionsManager, $sharedStringsManager, $escaper, $entityFactory);
}
/**
* @param string $filePath Path of the XLSX file being read
* @param EntityFactory $entityFactory Factory to create entities
* @return StyleManager
*/
public function createStyleManager($filePath, $entityFactory)
{
return new StyleManager($filePath, $entityFactory);
}
}

View File

@ -2,6 +2,9 @@
namespace Box\Spout\Reader\XLSX\Helper; namespace Box\Spout\Reader\XLSX\Helper;
use Box\Spout\Reader\XLSX\Manager\SharedStringsManager;
use Box\Spout\Reader\XLSX\Manager\StyleManager;
/** /**
* Class CellValueFormatter * Class CellValueFormatter
* This class provides helper functions to format cell values * This class provides helper functions to format cell values
@ -38,11 +41,11 @@ class CellValueFormatter
*/ */
const ERRONEOUS_EXCEL_LEAP_YEAR_DAY = 60; const ERRONEOUS_EXCEL_LEAP_YEAR_DAY = 60;
/** @var SharedStringsHelper Helper to work with shared strings */ /** @var SharedStringsManager Manages shared strings */
protected $sharedStringsHelper; protected $sharedStringsManager;
/** @var StyleHelper Helper to work with styles */ /** @var StyleManager Manages styles */
protected $styleHelper; protected $styleManager;
/** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */
protected $shouldFormatDates; protected $shouldFormatDates;
@ -51,15 +54,15 @@ class CellValueFormatter
protected $escaper; protected $escaper;
/** /**
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings * @param SharedStringsManager $sharedStringsManager Manages shared strings
* @param StyleHelper $styleHelper Helper to work with styles * @param StyleManager $styleManager Manages styles
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings
* @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data * @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data
*/ */
public function __construct($sharedStringsHelper, $styleHelper, $shouldFormatDates, $escaper) public function __construct($sharedStringsManager, $styleManager, $shouldFormatDates, $escaper)
{ {
$this->sharedStringsHelper = $sharedStringsHelper; $this->sharedStringsManager = $sharedStringsManager;
$this->styleHelper = $styleHelper; $this->styleManager = $styleManager;
$this->shouldFormatDates = $shouldFormatDates; $this->shouldFormatDates = $shouldFormatDates;
$this->escaper = $escaper; $this->escaper = $escaper;
} }
@ -139,7 +142,7 @@ class CellValueFormatter
// shared strings are formatted this way: // shared strings are formatted this way:
// <c r="A1" t="s"><v>[SHARED_STRING_INDEX]</v></c> // <c r="A1" t="s"><v>[SHARED_STRING_INDEX]</v></c>
$sharedStringIndex = intval($nodeValue); $sharedStringIndex = intval($nodeValue);
$escapedCellValue = $this->sharedStringsHelper->getStringAtIndex($sharedStringIndex); $escapedCellValue = $this->sharedStringsManager->getStringAtIndex($sharedStringIndex);
$cellValue = $this->escaper->unescape($escapedCellValue); $cellValue = $this->escaper->unescape($escapedCellValue);
return $cellValue; return $cellValue;
} }
@ -169,7 +172,7 @@ class CellValueFormatter
{ {
// Numeric values can represent numbers as well as timestamps. // Numeric values can represent numbers as well as timestamps.
// We need to look at the style of the cell to determine whether it is one or the other. // We need to look at the style of the cell to determine whether it is one or the other.
$shouldFormatAsDate = $this->styleHelper->shouldFormatNumericValueAsDate($cellStyleId); $shouldFormatAsDate = $this->styleManager->shouldFormatNumericValueAsDate($cellStyleId);
if ($shouldFormatAsDate) { if ($shouldFormatAsDate) {
return $this->formatExcelTimestampValue(floatval($nodeValue), $cellStyleId); return $this->formatExcelTimestampValue(floatval($nodeValue), $cellStyleId);
@ -227,7 +230,7 @@ class CellValueFormatter
$dateObj->setTime($hours, $minutes, $seconds); $dateObj->setTime($hours, $minutes, $seconds);
if ($this->shouldFormatDates) { if ($this->shouldFormatDates) {
$styleNumberFormatCode = $this->styleHelper->getNumberFormatCode($cellStyleId); $styleNumberFormatCode = $this->styleManager->getNumberFormatCode($cellStyleId);
$phpDateFormat = DateFormatHelper::toPHPDateFormat($styleNumberFormatCode); $phpDateFormat = DateFormatHelper::toPHPDateFormat($styleNumberFormatCode);
return $dateObj->format($phpDateFormat); return $dateObj->format($phpDateFormat);
} else { } else {
@ -256,7 +259,7 @@ class CellValueFormatter
$dateObj->modify('+' . $secondsRemainder . 'seconds'); $dateObj->modify('+' . $secondsRemainder . 'seconds');
if ($this->shouldFormatDates) { if ($this->shouldFormatDates) {
$styleNumberFormatCode = $this->styleHelper->getNumberFormatCode($cellStyleId); $styleNumberFormatCode = $this->styleManager->getNumberFormatCode($cellStyleId);
$phpDateFormat = DateFormatHelper::toPHPDateFormat($styleNumberFormatCode); $phpDateFormat = DateFormatHelper::toPHPDateFormat($styleNumberFormatCode);
return $dateObj->format($phpDateFormat); return $dateObj->format($phpDateFormat);
} else { } else {

View File

@ -1,12 +1,13 @@
<?php <?php
namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching; namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
use Box\Spout\Reader\XLSX\Creator\HelperFactory; use Box\Spout\Reader\XLSX\Creator\HelperFactory;
/** /**
* Class CachingStrategyFactory * Class CachingStrategyFactory
* *
* @package Box\Spout\Reader\XLSX\Helper\SharedStringsCaching * @package Box\Spout\Reader\XLSX\Manager\SharedStringsCaching
*/ */
class CachingStrategyFactory class CachingStrategyFactory
{ {

View File

@ -1,11 +1,11 @@
<?php <?php
namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching; namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
/** /**
* Interface CachingStrategyInterface * Interface CachingStrategyInterface
* *
* @package Box\Spout\Reader\XLSX\Helper\SharedStringsCaching * @package Box\Spout\Reader\XLSX\Manager\SharedStringsCaching
*/ */
interface CachingStrategyInterface interface CachingStrategyInterface
{ {

View File

@ -1,9 +1,7 @@
<?php <?php
namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching; namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
use Box\Spout\Common\Helper\FileSystemHelper;
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Reader\Exception\SharedStringNotFoundException; use Box\Spout\Reader\Exception\SharedStringNotFoundException;
use Box\Spout\Reader\XLSX\Creator\HelperFactory; use Box\Spout\Reader\XLSX\Creator\HelperFactory;
@ -14,7 +12,7 @@ use Box\Spout\Reader\XLSX\Creator\HelperFactory;
* Shared strings are stored in small files (with a max number of strings per file). * Shared strings are stored in small files (with a max number of strings per file).
* This strategy is slower than an in-memory strategy but is used to avoid out of memory crashes. * This strategy is slower than an in-memory strategy but is used to avoid out of memory crashes.
* *
* @package Box\Spout\Reader\XLSX\Helper\SharedStringsCaching * @package Box\Spout\Reader\XLSX\Manager\SharedStringsCaching
*/ */
class FileBasedStrategy implements CachingStrategyInterface class FileBasedStrategy implements CachingStrategyInterface
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching; namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
use Box\Spout\Reader\Exception\SharedStringNotFoundException; use Box\Spout\Reader\Exception\SharedStringNotFoundException;
@ -10,7 +10,7 @@ use Box\Spout\Reader\Exception\SharedStringNotFoundException;
* This class implements the in-memory caching strategy for shared strings. * This class implements the in-memory caching strategy for shared strings.
* This strategy is used when the number of unique strings is low, compared to the memory available. * This strategy is used when the number of unique strings is low, compared to the memory available.
* *
* @package Box\Spout\Reader\XLSX\Helper\SharedStringsCaching * @package Box\Spout\Reader\XLSX\Manager\SharedStringsCaching
*/ */
class InMemoryStrategy implements CachingStrategyInterface class InMemoryStrategy implements CachingStrategyInterface
{ {

View File

@ -1,22 +1,22 @@
<?php <?php
namespace Box\Spout\Reader\XLSX\Helper; namespace Box\Spout\Reader\XLSX\Manager;
use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\IOException;
use Box\Spout\Reader\Exception\XMLProcessingException; use Box\Spout\Reader\Exception\XMLProcessingException;
use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\Reader\XLSX\Creator\EntityFactory; use Box\Spout\Reader\XLSX\Creator\EntityFactory;
use Box\Spout\Reader\XLSX\Creator\HelperFactory; use Box\Spout\Reader\XLSX\Creator\HelperFactory;
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory; use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyInterface; use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyInterface;
/** /**
* Class SharedStringsHelper * Class SharedStringsManager
* This class provides helper functions for reading sharedStrings XML file * This class manages the shared strings defined in the associated XML file
* *
* @package Box\Spout\Reader\XLSX\Helper * @package Box\Spout\Reader\XLSX\Manager
*/ */
class SharedStringsHelper class SharedStringsManager
{ {
/** Path of sharedStrings XML file inside the XLSX file */ /** Path of sharedStrings XML file inside the XLSX file */
const SHARED_STRINGS_XML_FILE_PATH = 'xl/sharedStrings.xml'; const SHARED_STRINGS_XML_FILE_PATH = 'xl/sharedStrings.xml';

View File

@ -1,18 +1,18 @@
<?php <?php
namespace Box\Spout\Reader\XLSX\Helper; namespace Box\Spout\Reader\XLSX\Manager;
use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\Reader\XLSX\Creator\EntityFactory; use Box\Spout\Reader\XLSX\Creator\EntityFactory;
use Box\Spout\Reader\XLSX\Sheet; use Box\Spout\Reader\XLSX\Sheet;
/** /**
* Class SheetHelper * Class SheetManager
* This class provides helper functions related to XLSX sheets * This class manages XLSX sheets
* *
* @package Box\Spout\Reader\XLSX\Helper * @package Box\Spout\Reader\XLSX\Manager
*/ */
class SheetHelper class SheetManager
{ {
/** Paths of XML files relative to the XLSX file root */ /** Paths of XML files relative to the XLSX file root */
const WORKBOOK_XML_RELS_FILE_PATH = 'xl/_rels/workbook.xml.rels'; const WORKBOOK_XML_RELS_FILE_PATH = 'xl/_rels/workbook.xml.rels';
@ -37,8 +37,8 @@ class SheetHelper
/** @var \Box\Spout\Common\Manager\OptionsManagerInterface Reader's options manager */ /** @var \Box\Spout\Common\Manager\OptionsManagerInterface Reader's options manager */
protected $optionsManager; protected $optionsManager;
/** @var \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings */ /** @var \Box\Spout\Reader\XLSX\Manager\SharedStringsManager Manages shared strings */
protected $sharedStringsHelper; protected $sharedStringsManager;
/** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
protected $globalFunctionsHelper; protected $globalFunctionsHelper;
@ -52,15 +52,15 @@ class SheetHelper
/** /**
* @param string $filePath Path of the XLSX file being read * @param string $filePath Path of the XLSX file being read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings * @param \Box\Spout\Reader\XLSX\Manager\SharedStringsManager Manages shared strings
* @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data * @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data
* @param EntityFactory $entityFactory Factory to create entities * @param EntityFactory $entityFactory Factory to create entities
*/ */
public function __construct($filePath, $optionsManager, $sharedStringsHelper, $escaper, $entityFactory) public function __construct($filePath, $optionsManager, $sharedStringsManager, $escaper, $entityFactory)
{ {
$this->filePath = $filePath; $this->filePath = $filePath;
$this->optionsManager = $optionsManager; $this->optionsManager = $optionsManager;
$this->sharedStringsHelper = $sharedStringsHelper; $this->sharedStringsManager = $sharedStringsManager;
$this->escaper = $escaper; $this->escaper = $escaper;
$this->entityFactory = $entityFactory; $this->entityFactory = $entityFactory;
} }
@ -126,7 +126,7 @@ class SheetHelper
$sheetName, $sheetName,
$isSheetActive, $isSheetActive,
$this->optionsManager, $this->optionsManager,
$this->sharedStringsHelper $this->sharedStringsManager
); );
} }

View File

@ -1,16 +1,16 @@
<?php <?php
namespace Box\Spout\Reader\XLSX\Helper; namespace Box\Spout\Reader\XLSX\Manager;
use Box\Spout\Reader\XLSX\Creator\EntityFactory; use Box\Spout\Reader\XLSX\Creator\EntityFactory;
/** /**
* Class StyleHelper * Class StyleManager
* This class provides helper functions related to XLSX styles * This class manages XLSX styles
* *
* @package Box\Spout\Reader\XLSX\Helper * @package Box\Spout\Reader\XLSX\Manager
*/ */
class StyleHelper class StyleManager
{ {
/** Paths of XML files relative to the XLSX file root */ /** Paths of XML files relative to the XLSX file root */
const STYLES_XML_FILE_PATH = 'xl/styles.xml'; const STYLES_XML_FILE_PATH = 'xl/styles.xml';

View File

@ -9,7 +9,7 @@ use Box\Spout\Reader\Common\Creator\EntityFactoryInterface;
use Box\Spout\Reader\Common\Entity\Options; use Box\Spout\Reader\Common\Entity\Options;
use Box\Spout\Reader\ReaderAbstract; use Box\Spout\Reader\ReaderAbstract;
use Box\Spout\Reader\XLSX\Creator\EntityFactory; use Box\Spout\Reader\XLSX\Creator\EntityFactory;
use Box\Spout\Reader\XLSX\Creator\HelperFactory; use Box\Spout\Reader\XLSX\Creator\ManagerFactory;
/** /**
* Class Reader * Class Reader
@ -19,14 +19,14 @@ use Box\Spout\Reader\XLSX\Creator\HelperFactory;
*/ */
class Reader extends ReaderAbstract class Reader extends ReaderAbstract
{ {
/** @var HelperFactory */ /** @var ManagerFactory */
protected $helperFactory; protected $managerFactory;
/** @var \ZipArchive */ /** @var \ZipArchive */
protected $zip; protected $zip;
/** @var \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings */ /** @var \Box\Spout\Reader\XLSX\Manager\SharedStringsManager Manages shared strings */
protected $sharedStringsHelper; protected $sharedStringsManager;
/** @var SheetIterator To iterator over the XLSX sheets */ /** @var SheetIterator To iterator over the XLSX sheets */
protected $sheetIterator; protected $sheetIterator;
@ -36,16 +36,16 @@ class Reader extends ReaderAbstract
* @param OptionsManagerInterface $optionsManager * @param OptionsManagerInterface $optionsManager
* @param GlobalFunctionsHelper $globalFunctionsHelper * @param GlobalFunctionsHelper $globalFunctionsHelper
* @param EntityFactoryInterface $entityFactory * @param EntityFactoryInterface $entityFactory
* @param HelperFactory $helperFactory * @param ManagerFactory $managerFactory
*/ */
public function __construct( public function __construct(
OptionsManagerInterface $optionsManager, OptionsManagerInterface $optionsManager,
GlobalFunctionsHelper $globalFunctionsHelper, GlobalFunctionsHelper $globalFunctionsHelper,
EntityFactoryInterface $entityFactory, EntityFactoryInterface $entityFactory,
HelperFactory $helperFactory) ManagerFactory $managerFactory)
{ {
parent::__construct($optionsManager, $globalFunctionsHelper, $entityFactory); parent::__construct($optionsManager, $globalFunctionsHelper, $entityFactory);
$this->helperFactory = $helperFactory; $this->managerFactory = $managerFactory;
} }
/** /**
@ -87,14 +87,14 @@ class Reader extends ReaderAbstract
if ($this->zip->open($filePath) === true) { if ($this->zip->open($filePath) === true) {
$tempFolder = $this->optionsManager->getOption(Options::TEMP_FOLDER); $tempFolder = $this->optionsManager->getOption(Options::TEMP_FOLDER);
$this->sharedStringsHelper = $this->helperFactory->createSharedStringsHelper($filePath, $tempFolder, $entityFactory); $this->sharedStringsManager = $this->managerFactory->createSharedStringsManager($filePath, $tempFolder, $entityFactory);
if ($this->sharedStringsHelper->hasSharedStrings()) { if ($this->sharedStringsManager->hasSharedStrings()) {
// Extracts all the strings from the sheets for easy access in the future // Extracts all the strings from the sheets for easy access in the future
$this->sharedStringsHelper->extractSharedStrings(); $this->sharedStringsManager->extractSharedStrings();
} }
$this->sheetIterator = $entityFactory->createSheetIterator($filePath, $this->optionsManager, $this->sharedStringsHelper, $this->globalFunctionsHelper); $this->sheetIterator = $entityFactory->createSheetIterator($filePath, $this->optionsManager, $this->sharedStringsManager, $this->globalFunctionsHelper);
} else { } else {
throw new IOException("Could not open $filePath for reading."); throw new IOException("Could not open $filePath for reading.");
} }
@ -121,8 +121,8 @@ class Reader extends ReaderAbstract
$this->zip->close(); $this->zip->close();
} }
if ($this->sharedStringsHelper) { if ($this->sharedStringsManager) {
$this->sharedStringsHelper->cleanup(); $this->sharedStringsManager->cleanup();
} }
} }
} }

View File

@ -3,9 +3,7 @@
namespace Box\Spout\Reader\XLSX; namespace Box\Spout\Reader\XLSX;
use Box\Spout\Reader\IteratorInterface; use Box\Spout\Reader\IteratorInterface;
use Box\Spout\Reader\XLSX\Creator\EntityFactory; use Box\Spout\Reader\XLSX\Manager\SheetManager;
use Box\Spout\Reader\XLSX\Creator\HelperFactory;
use Box\Spout\Reader\XLSX\Helper\SheetHelper;
use Box\Spout\Reader\Exception\NoSheetsFoundException; use Box\Spout\Reader\Exception\NoSheetsFoundException;
/** /**
@ -23,13 +21,13 @@ class SheetIterator implements IteratorInterface
protected $currentSheetIndex; protected $currentSheetIndex;
/** /**
* @param SheetHelper $sheetHelper Helper to work with sheets * @param SheetManager $sheetManager Manages sheets
* @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
*/ */
public function __construct($sheetHelper) public function __construct($sheetManager)
{ {
// Fetch all available sheets // Fetch all available sheets
$this->sheets = $sheetHelper->getSheets(); $this->sheets = $sheetManager->getSheets();
if (count($this->sheets) === 0) { if (count($this->sheets) === 0) {
throw new NoSheetsFoundException('The file must contain at least one sheet.'); throw new NoSheetsFoundException('The file must contain at least one sheet.');

View File

@ -65,15 +65,16 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
->with(CellValueFormatter::XML_NODE_VALUE) ->with(CellValueFormatter::XML_NODE_VALUE)
->will($this->returnValue($nodeListMock)); ->will($this->returnValue($nodeListMock));
$styleHelperMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Helper\StyleHelper')->disableOriginalConstructor()->getMock(); /** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */
$styleManagerMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Manager\StyleManager')->disableOriginalConstructor()->getMock();
$styleHelperMock $styleManagerMock
->expects($this->once()) ->expects($this->once())
->method('shouldFormatNumericValueAsDate') ->method('shouldFormatNumericValueAsDate')
->with(123) ->with(123)
->will($this->returnValue(true)); ->will($this->returnValue(true));
$formatter = new CellValueFormatter(null, $styleHelperMock, false, new Escaper\XLSX()); $formatter = new CellValueFormatter(null, $styleManagerMock, false, new Escaper\XLSX());
$result = $formatter->extractAndFormatNodeValue($nodeMock); $result = $formatter->extractAndFormatNodeValue($nodeMock);
if ($expectedDateAsString === null) { if ($expectedDateAsString === null) {
@ -120,13 +121,14 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFormatNumericCellValueWithNumbers($value, $expectedFormattedValue, $expectedType) public function testFormatNumericCellValueWithNumbers($value, $expectedFormattedValue, $expectedType)
{ {
$styleHelperMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Helper\StyleHelper')->disableOriginalConstructor()->getMock(); /** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */
$styleHelperMock $styleManagerMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Manager\StyleManager')->disableOriginalConstructor()->getMock();
$styleManagerMock
->expects($this->once()) ->expects($this->once())
->method('shouldFormatNumericValueAsDate') ->method('shouldFormatNumericValueAsDate')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$formatter = new CellValueFormatter(null, $styleHelperMock, false, new Escaper\XLSX()); $formatter = new CellValueFormatter(null, $styleManagerMock, false, new Escaper\XLSX());
$formattedValue = \ReflectionHelper::callMethodOnObject($formatter, 'formatNumericCellValue', $value, 0); $formattedValue = \ReflectionHelper::callMethodOnObject($formatter, 'formatNumericCellValue', $value, 0);
$this->assertEquals($expectedFormattedValue, $formattedValue); $this->assertEquals($expectedFormattedValue, $formattedValue);

View File

@ -1,154 +0,0 @@
<?php
namespace Box\Spout\Reader\XLSX\Helper;
use Box\Spout\Reader\XLSX\Creator\EntityFactory;
use Box\Spout\Reader\XLSX\Creator\HelperFactory;
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory;
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\FileBasedStrategy;
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\InMemoryStrategy;
use Box\Spout\TestUsingResource;
/**
* Class SharedStringsHelperTest
*
* @package Box\Spout\Reader\XLSX\Helper
*/
class SharedStringsHelperTest extends \PHPUnit_Framework_TestCase
{
use TestUsingResource;
/** @var SharedStringsHelper */
private $sharedStringsHelper;
/**
* @return void
*/
public function setUp()
{
$this->sharedStringsHelper = null;
}
/**
* @return void
*/
public function tearDown()
{
if ($this->sharedStringsHelper !== null) {
$this->sharedStringsHelper->cleanup();
}
}
/**
* @param string $resourceName
* @return SharedStringsHelper
*/
private function createSharedStringsHelper($resourceName = 'one_sheet_with_shared_strings.xlsx')
{
$resourcePath = $this->getResourcePath($resourceName);
$tempFolder = sys_get_temp_dir();
$cachingStrategyFactory = new CachingStrategyFactory();
$helperFactory = new HelperFactory($cachingStrategyFactory);
$entityFactory = new EntityFactory($helperFactory);
$this->sharedStringsHelper = new SharedStringsHelper($resourcePath, $tempFolder, $entityFactory, $helperFactory, $cachingStrategyFactory);
return $this->sharedStringsHelper;
}
/**
* @expectedException \Box\Spout\Reader\Exception\SharedStringNotFoundException
* @return void
*/
public function testGetStringAtIndexShouldThrowExceptionIfStringNotFound()
{
$sharedStringsHelper = $this->createSharedStringsHelper();
$sharedStringsHelper->extractSharedStrings();
$sharedStringsHelper->getStringAtIndex(PHP_INT_MAX);
}
/**
* @return void
*/
public function testGetStringAtIndexShouldReturnTheCorrectStringIfFound()
{
$sharedStringsHelper = $this->createSharedStringsHelper();
$sharedStringsHelper->extractSharedStrings();
$sharedString = $sharedStringsHelper->getStringAtIndex(0);
$this->assertEquals('s1--A1', $sharedString);
$sharedString = $sharedStringsHelper->getStringAtIndex(24);
$this->assertEquals('s1--E5', $sharedString);
$usedCachingStrategy = \ReflectionHelper::getValueOnObject($sharedStringsHelper, 'cachingStrategy');
$this->assertTrue($usedCachingStrategy instanceof InMemoryStrategy);
}
/**
* @return void
*/
public function testGetStringAtIndexShouldWorkWithMultilineStrings()
{
$sharedStringsHelper = $this->createSharedStringsHelper('one_sheet_with_shared_multiline_strings.xlsx');
$sharedStringsHelper->extractSharedStrings();
$sharedString = $sharedStringsHelper->getStringAtIndex(0);
$this->assertEquals("s1\nA1", $sharedString);
$sharedString = $sharedStringsHelper->getStringAtIndex(24);
$this->assertEquals("s1\nE5", $sharedString);
}
/**
* @return void
*/
public function testGetStringAtIndexShouldWorkWithStringsContainingTextAndHyperlinkInSameCell()
{
$sharedStringsHelper = $this->createSharedStringsHelper('one_sheet_with_shared_strings_containing_text_and_hyperlink_in_same_cell.xlsx');
$sharedStringsHelper->extractSharedStrings();
$sharedString = $sharedStringsHelper->getStringAtIndex(0);
$this->assertEquals('go to https://github.com please', $sharedString);
}
/**
* @return void
*/
public function testGetStringAtIndexShouldNotDoubleDecodeHTMLEntities()
{
$sharedStringsHelper = $this->createSharedStringsHelper('one_sheet_with_pre_encoded_html_entities.xlsx');
$sharedStringsHelper->extractSharedStrings();
$sharedString = $sharedStringsHelper->getStringAtIndex(0);
$this->assertEquals('quote: &#34; - ampersand: &amp;', $sharedString);
}
/**
* @return void
*/
public function testGetStringAtIndexWithFileBasedStrategy()
{
// force the file-based strategy by setting no memory limit
$originalMemoryLimit = ini_get('memory_limit');
ini_set('memory_limit', '-1');
$sharedStringsHelper = $this->createSharedStringsHelper('sheet_with_lots_of_shared_strings.xlsx');
$sharedStringsHelper->extractSharedStrings();
$sharedString = $sharedStringsHelper->getStringAtIndex(0);
$this->assertEquals('str', $sharedString);
$sharedString = $sharedStringsHelper->getStringAtIndex(CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE + 1);
$this->assertEquals('str', $sharedString);
$usedCachingStrategy = \ReflectionHelper::getValueOnObject($sharedStringsHelper, 'cachingStrategy');
$this->assertTrue($usedCachingStrategy instanceof FileBasedStrategy);
ini_set('memory_limit', $originalMemoryLimit);
}
}

View File

@ -1,12 +1,13 @@
<?php <?php
namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching; namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
use Box\Spout\Reader\XLSX\Creator\HelperFactory; use Box\Spout\Reader\XLSX\Creator\HelperFactory;
/** /**
* Class CachingStrategyFactoryTest * Class CachingStrategyFactoryTest
* *
* @package Box\Spout\Reader\XLSX\Helper\SharedStringsCaching * @package Box\Spout\Reader\XLSX\Manager\SharedStringsCaching
*/ */
class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase
{ {
@ -38,7 +39,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase
{ {
/** @var CachingStrategyFactory|\PHPUnit_Framework_MockObject_MockObject $factoryStub */ /** @var CachingStrategyFactory|\PHPUnit_Framework_MockObject_MockObject $factoryStub */
$factoryStub = $this $factoryStub = $this
->getMockBuilder('\Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory') ->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory')
->disableOriginalConstructor() ->disableOriginalConstructor()
->setMethods(['getMemoryLimitInKB']) ->setMethods(['getMemoryLimitInKB'])
->getMock(); ->getMock();
@ -49,7 +50,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase
$helperFactory = new HelperFactory($factoryStub); $helperFactory = new HelperFactory($factoryStub);
$strategy = $factoryStub->createBestCachingStrategy($sharedStringsUniqueCount, $tempFolder, $helperFactory); $strategy = $factoryStub->createBestCachingStrategy($sharedStringsUniqueCount, $tempFolder, $helperFactory);
$fullExpectedStrategyClassName = 'Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\\' . $expectedStrategyClassName; $fullExpectedStrategyClassName = 'Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\\' . $expectedStrategyClassName;
$this->assertEquals($fullExpectedStrategyClassName, get_class($strategy)); $this->assertEquals($fullExpectedStrategyClassName, get_class($strategy));
$strategy->clearCache(); $strategy->clearCache();
@ -86,7 +87,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase
{ {
/** @var CachingStrategyFactory|\PHPUnit_Framework_MockObject_MockObject $factoryStub */ /** @var CachingStrategyFactory|\PHPUnit_Framework_MockObject_MockObject $factoryStub */
$factoryStub = $this $factoryStub = $this
->getMockBuilder('\Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory') ->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory')
->disableOriginalConstructor() ->disableOriginalConstructor()
->setMethods(['getMemoryLimitFromIni']) ->setMethods(['getMemoryLimitFromIni'])
->getMock(); ->getMock();

View File

@ -0,0 +1,156 @@
<?php
namespace Box\Spout\Reader\XLSX\Manager;
use Box\Spout\Reader\XLSX\Creator\EntityFactory;
use Box\Spout\Reader\XLSX\Creator\HelperFactory;
use Box\Spout\Reader\XLSX\Creator\ManagerFactory;
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\FileBasedStrategy;
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\InMemoryStrategy;
use Box\Spout\TestUsingResource;
/**
* Class SharedStringsManagerTest
*
* @package Box\Spout\Reader\XLSX\Manager
*/
class SharedStringsManagerTest extends \PHPUnit_Framework_TestCase
{
use TestUsingResource;
/** @var SharedStringsManager */
private $sharedStringsManager;
/**
* @return void
*/
public function setUp()
{
$this->sharedStringsManager = null;
}
/**
* @return void
*/
public function tearDown()
{
if ($this->sharedStringsManager !== null) {
$this->sharedStringsManager->cleanup();
}
}
/**
* @param string $resourceName
* @return SharedStringsManager
*/
private function createSharedStringsManager($resourceName = 'one_sheet_with_shared_strings.xlsx')
{
$resourcePath = $this->getResourcePath($resourceName);
$tempFolder = sys_get_temp_dir();
$cachingStrategyFactory = new CachingStrategyFactory();
$helperFactory = new HelperFactory();
$managerFactory = new ManagerFactory($helperFactory, $cachingStrategyFactory);
$entityFactory = new EntityFactory($managerFactory, $helperFactory);
$this->sharedStringsManager = new SharedStringsManager($resourcePath, $tempFolder, $entityFactory, $helperFactory, $cachingStrategyFactory);
return $this->sharedStringsManager;
}
/**
* @expectedException \Box\Spout\Reader\Exception\SharedStringNotFoundException
* @return void
*/
public function testGetStringAtIndexShouldThrowExceptionIfStringNotFound()
{
$sharedStringsManager = $this->createSharedStringsManager();
$sharedStringsManager->extractSharedStrings();
$sharedStringsManager->getStringAtIndex(PHP_INT_MAX);
}
/**
* @return void
*/
public function testGetStringAtIndexShouldReturnTheCorrectStringIfFound()
{
$sharedStringsManager = $this->createSharedStringsManager();
$sharedStringsManager->extractSharedStrings();
$sharedString = $sharedStringsManager->getStringAtIndex(0);
$this->assertEquals('s1--A1', $sharedString);
$sharedString = $sharedStringsManager->getStringAtIndex(24);
$this->assertEquals('s1--E5', $sharedString);
$usedCachingStrategy = \ReflectionHelper::getValueOnObject($sharedStringsManager, 'cachingStrategy');
$this->assertTrue($usedCachingStrategy instanceof InMemoryStrategy);
}
/**
* @return void
*/
public function testGetStringAtIndexShouldWorkWithMultilineStrings()
{
$sharedStringsManager = $this->createSharedStringsManager('one_sheet_with_shared_multiline_strings.xlsx');
$sharedStringsManager->extractSharedStrings();
$sharedString = $sharedStringsManager->getStringAtIndex(0);
$this->assertEquals("s1\nA1", $sharedString);
$sharedString = $sharedStringsManager->getStringAtIndex(24);
$this->assertEquals("s1\nE5", $sharedString);
}
/**
* @return void
*/
public function testGetStringAtIndexShouldWorkWithStringsContainingTextAndHyperlinkInSameCell()
{
$sharedStringsManager = $this->createSharedStringsManager('one_sheet_with_shared_strings_containing_text_and_hyperlink_in_same_cell.xlsx');
$sharedStringsManager->extractSharedStrings();
$sharedString = $sharedStringsManager->getStringAtIndex(0);
$this->assertEquals('go to https://github.com please', $sharedString);
}
/**
* @return void
*/
public function testGetStringAtIndexShouldNotDoubleDecodeHTMLEntities()
{
$sharedStringsManager = $this->createSharedStringsManager('one_sheet_with_pre_encoded_html_entities.xlsx');
$sharedStringsManager->extractSharedStrings();
$sharedString = $sharedStringsManager->getStringAtIndex(0);
$this->assertEquals('quote: &#34; - ampersand: &amp;', $sharedString);
}
/**
* @return void
*/
public function testGetStringAtIndexWithFileBasedStrategy()
{
// force the file-based strategy by setting no memory limit
$originalMemoryLimit = ini_get('memory_limit');
ini_set('memory_limit', '-1');
$sharedStringsManager = $this->createSharedStringsManager('sheet_with_lots_of_shared_strings.xlsx');
$sharedStringsManager->extractSharedStrings();
$sharedString = $sharedStringsManager->getStringAtIndex(0);
$this->assertEquals('str', $sharedString);
$sharedString = $sharedStringsManager->getStringAtIndex(CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE + 1);
$this->assertEquals('str', $sharedString);
$usedCachingStrategy = \ReflectionHelper::getValueOnObject($sharedStringsManager, 'cachingStrategy');
$this->assertTrue($usedCachingStrategy instanceof FileBasedStrategy);
ini_set('memory_limit', $originalMemoryLimit);
}
}

View File

@ -1,37 +1,41 @@
<?php <?php
namespace Box\Spout\Reader\XLSX\Helper; namespace Box\Spout\Reader\XLSX\Manager;
use Box\Spout\Reader\XLSX\Creator\EntityFactory; use Box\Spout\Reader\XLSX\Creator\EntityFactory;
use Box\Spout\Reader\XLSX\Creator\HelperFactory; use Box\Spout\Reader\XLSX\Creator\HelperFactory;
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory; use Box\Spout\Reader\XLSX\Creator\ManagerFactory;
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
/** /**
* Class StyleManagerTest * Class StyleManagerTest
* *
* @package Box\Spout\Reader\XLSX\Helper * @package Box\Spout\Reader\XLSX\Manager
*/ */
class StyleHelperTest extends \PHPUnit_Framework_TestCase class StyleManagerTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @param array|void $styleAttributes * @param array|void $styleAttributes
* @param array|void $customNumberFormats * @param array|void $customNumberFormats
* @return StyleHelper * @return StyleManager
*/ */
private function getStyleHelperMock($styleAttributes = [], $customNumberFormats = []) private function getStyleManagerMock($styleAttributes = [], $customNumberFormats = [])
{ {
$entityFactory = new EntityFactory(new HelperFactory(new CachingStrategyFactory())); $helperFactory = new HelperFactory();
$managerFactory = new ManagerFactory($helperFactory, new CachingStrategyFactory());
$entityFactory = new EntityFactory($managerFactory, $helperFactory);
/** @var StyleHelper $styleHelper */ /** @var StyleManager $styleManager */
$styleHelper = $this->getMockBuilder('\Box\Spout\Reader\XLSX\Helper\StyleHelper') $styleManager = $this->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\StyleManager')
->setConstructorArgs(['/path/to/file.xlsx', $entityFactory]) ->setConstructorArgs(['/path/to/file.xlsx', $entityFactory])
->setMethods(['getCustomNumberFormats', 'getStylesAttributes']) ->setMethods(['getCustomNumberFormats', 'getStylesAttributes'])
->getMock(); ->getMock();
$styleHelper->method('getStylesAttributes')->willReturn($styleAttributes); $styleManager->method('getStylesAttributes')->willReturn($styleAttributes);
$styleHelper->method('getCustomNumberFormats')->willReturn($customNumberFormats); $styleManager->method('getCustomNumberFormats')->willReturn($customNumberFormats);
return $styleHelper; return $styleManager;
} }
/** /**
@ -39,8 +43,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testShouldFormatNumericValueAsDateWithDefaultStyle() public function testShouldFormatNumericValueAsDateWithDefaultStyle()
{ {
$styleHelper = $this->getStyleHelperMock(); $styleManager = $this->getStyleManagerMock();
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(0); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(0);
$this->assertFalse($shouldFormatAsDate); $this->assertFalse($shouldFormatAsDate);
} }
@ -49,8 +53,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testShouldFormatNumericValueAsDateWhenShouldNotApplyNumberFormat() public function testShouldFormatNumericValueAsDateWhenShouldNotApplyNumberFormat()
{ {
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => false, 'numFmtId' => 14]]); $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => false, 'numFmtId' => 14]]);
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
$this->assertFalse($shouldFormatAsDate); $this->assertFalse($shouldFormatAsDate);
} }
@ -59,8 +63,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testShouldFormatNumericValueAsDateWithGeneralFormat() public function testShouldFormatNumericValueAsDateWithGeneralFormat()
{ {
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 0]]); $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => 0]]);
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
$this->assertFalse($shouldFormatAsDate); $this->assertFalse($shouldFormatAsDate);
} }
@ -69,8 +73,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testShouldFormatNumericValueAsDateWithNonDateBuiltinFormat() public function testShouldFormatNumericValueAsDateWithNonDateBuiltinFormat()
{ {
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 9]]); $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => 9]]);
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
$this->assertFalse($shouldFormatAsDate); $this->assertFalse($shouldFormatAsDate);
} }
@ -79,8 +83,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testShouldFormatNumericValueAsDateWithNoNumFmtId() public function testShouldFormatNumericValueAsDateWithNoNumFmtId()
{ {
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => null]]); $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => null]]);
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
$this->assertFalse($shouldFormatAsDate); $this->assertFalse($shouldFormatAsDate);
} }
@ -92,8 +96,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
$builtinNumFmtIdsForDate = [14, 15, 16, 17, 18, 19, 20, 21, 22, 45, 46, 47]; $builtinNumFmtIdsForDate = [14, 15, 16, 17, 18, 19, 20, 21, 22, 45, 46, 47];
foreach ($builtinNumFmtIdsForDate as $builtinNumFmtIdForDate) { foreach ($builtinNumFmtIdsForDate as $builtinNumFmtIdForDate) {
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => $builtinNumFmtIdForDate]]); $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => $builtinNumFmtIdForDate]]);
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
$this->assertTrue($shouldFormatAsDate); $this->assertTrue($shouldFormatAsDate);
} }
@ -104,8 +108,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinDateFormat() public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinDateFormat()
{ {
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => null, 'numFmtId' => 14]]); $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => null, 'numFmtId' => 14]]);
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
$this->assertTrue($shouldFormatAsDate); $this->assertTrue($shouldFormatAsDate);
} }
@ -115,8 +119,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinNonDateFormat() public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinNonDateFormat()
{ {
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => null, 'numFmtId' => 9]]); $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => null, 'numFmtId' => 9]]);
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
$this->assertFalse($shouldFormatAsDate); $this->assertFalse($shouldFormatAsDate);
} }
@ -126,8 +130,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testShouldFormatNumericValueAsDateWhenCustomNumberFormatNotFound() public function testShouldFormatNumericValueAsDateWhenCustomNumberFormatNotFound()
{ {
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 165]], [166 => []]); $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => 165]], [166 => []]);
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
$this->assertFalse($shouldFormatAsDate); $this->assertFalse($shouldFormatAsDate);
} }
@ -176,8 +180,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
public function testShouldFormatNumericValueAsDateWithCustomDateFormats($numberFormat, $expectedResult) public function testShouldFormatNumericValueAsDateWithCustomDateFormats($numberFormat, $expectedResult)
{ {
$numFmtId = 165; $numFmtId = 165;
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => $numFmtId]], [$numFmtId => $numberFormat]); $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => $numFmtId]], [$numFmtId => $numberFormat]);
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
$this->assertEquals($expectedResult, $shouldFormatAsDate); $this->assertEquals($expectedResult, $shouldFormatAsDate);
} }