Introduce Managers for readers
Some helper classes were more managers than helpers...
This commit is contained in:
parent
61f2addefa
commit
668c10a30d
@ -4,9 +4,8 @@ namespace Box\Spout\Reader;
|
||||
|
||||
use Box\Spout\Common\Creator\HelperFactory;
|
||||
use Box\Spout\Common\Exception\UnsupportedTypeException;
|
||||
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory;
|
||||
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
|
||||
|
||||
/**
|
||||
* Class ReaderFactory
|
||||
@ -56,12 +55,12 @@ class ReaderFactory
|
||||
private static function getXLSXReader()
|
||||
{
|
||||
$optionsManager = new XLSX\Manager\OptionsManager();
|
||||
$cachingStrategyFactory = new CachingStrategyFactory();
|
||||
$helperFactory = new XLSX\Creator\HelperFactory($cachingStrategyFactory);
|
||||
$entityFactory = new XLSX\Creator\EntityFactory($helperFactory);
|
||||
$helperFactory = new XLSX\Creator\HelperFactory();
|
||||
$managerFactory = new XLSX\Creator\ManagerFactory($helperFactory, new CachingStrategyFactory());
|
||||
$entityFactory = new XLSX\Creator\EntityFactory($managerFactory, $helperFactory);
|
||||
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
|
||||
|
||||
return new XLSX\Reader($optionsManager, $globalFunctionsHelper, $entityFactory, $helperFactory);
|
||||
return new XLSX\Reader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,11 +5,12 @@ namespace Box\Spout\Reader\XLSX\Creator;
|
||||
use Box\Spout\Reader\Common\Creator\EntityFactoryInterface;
|
||||
use Box\Spout\Reader\Common\Entity\Options;
|
||||
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\Sheet;
|
||||
use Box\Spout\Reader\XLSX\SheetIterator;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use MongoDB\Driver\Manager;
|
||||
|
||||
/**
|
||||
* Class EntityFactory
|
||||
@ -22,24 +23,29 @@ class EntityFactory implements EntityFactoryInterface
|
||||
/** @var HelperFactory */
|
||||
private $helperFactory;
|
||||
|
||||
/** @var ManagerFactory */
|
||||
private $managerFactory;
|
||||
|
||||
/**
|
||||
* @param ManagerFactory $managerFactory
|
||||
* @param HelperFactory $helperFactory
|
||||
*/
|
||||
public function __construct(HelperFactory $helperFactory)
|
||||
public function __construct(ManagerFactory $managerFactory, HelperFactory $helperFactory)
|
||||
{
|
||||
$this->managerFactory = $managerFactory;
|
||||
$this->helperFactory = $helperFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filePath Path of the file to be read
|
||||
* @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
|
||||
*/
|
||||
public function createSheetIterator($filePath, $optionsManager, $sharedStringsHelper)
|
||||
public function createSheetIterator($filePath, $optionsManager, $sharedStringsManager)
|
||||
{
|
||||
$sheetHelper = $this->helperFactory->createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $this);
|
||||
return new SheetIterator($sheetHelper);
|
||||
$sheetManager = $this->managerFactory->createSheetManager($filePath, $optionsManager, $sharedStringsManager, $this);
|
||||
return new SheetIterator($sheetManager);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,7 +55,7 @@ class EntityFactory implements EntityFactoryInterface
|
||||
* @param string $sheetName Name of the sheet
|
||||
* @param bool $isSheetActive Whether the sheet was defined as active
|
||||
* @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
|
||||
*/
|
||||
public function createSheet(
|
||||
@ -59,9 +65,9 @@ class EntityFactory implements EntityFactoryInterface
|
||||
$sheetName,
|
||||
$isSheetActive,
|
||||
$optionsManager,
|
||||
$sharedStringsHelper)
|
||||
$sharedStringsManager)
|
||||
{
|
||||
$rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper);
|
||||
$rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsManager);
|
||||
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 $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 SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
|
||||
* @param SharedStringsManager $sharedStringsManager Manages shared strings
|
||||
* @return RowIterator
|
||||
*/
|
||||
private function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper)
|
||||
private function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsManager)
|
||||
{
|
||||
$xmlReader = $this->createXMLReader();
|
||||
$xmlProcessor = $this->createXMLProcessor($xmlReader);
|
||||
|
||||
$styleHelper = $this->helperFactory->createStyleHelper($filePath, $this);
|
||||
$styleManager = $this->managerFactory->createStyleManager($filePath, $this);
|
||||
$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);
|
||||
|
||||
|
@ -4,75 +4,30 @@ 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\SharedStringsCaching\CachingStrategyFactory;
|
||||
use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper;
|
||||
use Box\Spout\Reader\XLSX\Helper\SheetHelper;
|
||||
use Box\Spout\Reader\XLSX\Helper\StyleHelper;
|
||||
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 EntityFactory
|
||||
* Class HelperFactory
|
||||
* Factory to create helpers
|
||||
*
|
||||
* @package Box\Spout\Reader\XLSX\Creator
|
||||
*/
|
||||
class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory
|
||||
{
|
||||
/** @var CachingStrategyFactory */
|
||||
private $cachingStrategyFactory;
|
||||
|
||||
/**
|
||||
* @param CachingStrategyFactory $cachingStrategyFactory Factory to create shared strings caching strategies
|
||||
*/
|
||||
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 SharedStringsManager $sharedStringsManager Manages shared strings
|
||||
* @param StyleManager $styleManager Manages styles
|
||||
* @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings
|
||||
* @return CellValueFormatter
|
||||
*/
|
||||
public function createCellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates)
|
||||
public function createCellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates)
|
||||
{
|
||||
$escaper = $this->createStringsEscaper();
|
||||
return new CellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates, $escaper);
|
||||
return new CellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates, $escaper);
|
||||
}
|
||||
|
||||
/**
|
||||
|
68
src/Spout/Reader/XLSX/Creator/ManagerFactory.php
Normal file
68
src/Spout/Reader/XLSX/Creator/ManagerFactory.php
Normal 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);
|
||||
}
|
||||
}
|
@ -2,6 +2,9 @@
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper;
|
||||
|
||||
use Box\Spout\Reader\XLSX\Manager\SharedStringsManager;
|
||||
use Box\Spout\Reader\XLSX\Manager\StyleManager;
|
||||
|
||||
/**
|
||||
* Class CellValueFormatter
|
||||
* This class provides helper functions to format cell values
|
||||
@ -38,11 +41,11 @@ class CellValueFormatter
|
||||
*/
|
||||
const ERRONEOUS_EXCEL_LEAP_YEAR_DAY = 60;
|
||||
|
||||
/** @var SharedStringsHelper Helper to work with shared strings */
|
||||
protected $sharedStringsHelper;
|
||||
/** @var SharedStringsManager Manages shared strings */
|
||||
protected $sharedStringsManager;
|
||||
|
||||
/** @var StyleHelper Helper to work with styles */
|
||||
protected $styleHelper;
|
||||
/** @var StyleManager Manages styles */
|
||||
protected $styleManager;
|
||||
|
||||
/** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */
|
||||
protected $shouldFormatDates;
|
||||
@ -51,15 +54,15 @@ class CellValueFormatter
|
||||
protected $escaper;
|
||||
|
||||
/**
|
||||
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
|
||||
* @param StyleHelper $styleHelper Helper to work with styles
|
||||
* @param SharedStringsManager $sharedStringsManager Manages shared strings
|
||||
* @param StyleManager $styleManager Manages styles
|
||||
* @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
|
||||
*/
|
||||
public function __construct($sharedStringsHelper, $styleHelper, $shouldFormatDates, $escaper)
|
||||
public function __construct($sharedStringsManager, $styleManager, $shouldFormatDates, $escaper)
|
||||
{
|
||||
$this->sharedStringsHelper = $sharedStringsHelper;
|
||||
$this->styleHelper = $styleHelper;
|
||||
$this->sharedStringsManager = $sharedStringsManager;
|
||||
$this->styleManager = $styleManager;
|
||||
$this->shouldFormatDates = $shouldFormatDates;
|
||||
$this->escaper = $escaper;
|
||||
}
|
||||
@ -139,7 +142,7 @@ class CellValueFormatter
|
||||
// shared strings are formatted this way:
|
||||
// <c r="A1" t="s"><v>[SHARED_STRING_INDEX]</v></c>
|
||||
$sharedStringIndex = intval($nodeValue);
|
||||
$escapedCellValue = $this->sharedStringsHelper->getStringAtIndex($sharedStringIndex);
|
||||
$escapedCellValue = $this->sharedStringsManager->getStringAtIndex($sharedStringIndex);
|
||||
$cellValue = $this->escaper->unescape($escapedCellValue);
|
||||
return $cellValue;
|
||||
}
|
||||
@ -169,7 +172,7 @@ class CellValueFormatter
|
||||
{
|
||||
// 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.
|
||||
$shouldFormatAsDate = $this->styleHelper->shouldFormatNumericValueAsDate($cellStyleId);
|
||||
$shouldFormatAsDate = $this->styleManager->shouldFormatNumericValueAsDate($cellStyleId);
|
||||
|
||||
if ($shouldFormatAsDate) {
|
||||
return $this->formatExcelTimestampValue(floatval($nodeValue), $cellStyleId);
|
||||
@ -227,7 +230,7 @@ class CellValueFormatter
|
||||
$dateObj->setTime($hours, $minutes, $seconds);
|
||||
|
||||
if ($this->shouldFormatDates) {
|
||||
$styleNumberFormatCode = $this->styleHelper->getNumberFormatCode($cellStyleId);
|
||||
$styleNumberFormatCode = $this->styleManager->getNumberFormatCode($cellStyleId);
|
||||
$phpDateFormat = DateFormatHelper::toPHPDateFormat($styleNumberFormatCode);
|
||||
return $dateObj->format($phpDateFormat);
|
||||
} else {
|
||||
@ -256,7 +259,7 @@ class CellValueFormatter
|
||||
$dateObj->modify('+' . $secondsRemainder . 'seconds');
|
||||
|
||||
if ($this->shouldFormatDates) {
|
||||
$styleNumberFormatCode = $this->styleHelper->getNumberFormatCode($cellStyleId);
|
||||
$styleNumberFormatCode = $this->styleManager->getNumberFormatCode($cellStyleId);
|
||||
$phpDateFormat = DateFormatHelper::toPHPDateFormat($styleNumberFormatCode);
|
||||
return $dateObj->format($phpDateFormat);
|
||||
} else {
|
||||
|
@ -1,12 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching;
|
||||
namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
|
||||
|
||||
use Box\Spout\Reader\XLSX\Creator\HelperFactory;
|
||||
|
||||
/**
|
||||
* Class CachingStrategyFactory
|
||||
*
|
||||
* @package Box\Spout\Reader\XLSX\Helper\SharedStringsCaching
|
||||
* @package Box\Spout\Reader\XLSX\Manager\SharedStringsCaching
|
||||
*/
|
||||
class CachingStrategyFactory
|
||||
{
|
@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching;
|
||||
namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
|
||||
|
||||
/**
|
||||
* Interface CachingStrategyInterface
|
||||
*
|
||||
* @package Box\Spout\Reader\XLSX\Helper\SharedStringsCaching
|
||||
* @package Box\Spout\Reader\XLSX\Manager\SharedStringsCaching
|
||||
*/
|
||||
interface CachingStrategyInterface
|
||||
{
|
@ -1,9 +1,7 @@
|
||||
<?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\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).
|
||||
* 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
|
||||
{
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching;
|
||||
namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
|
||||
|
||||
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 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
|
||||
{
|
@ -1,22 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper;
|
||||
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\Helper\SharedStringsCaching\CachingStrategyFactory;
|
||||
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyInterface;
|
||||
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
|
||||
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyInterface;
|
||||
|
||||
/**
|
||||
* Class SharedStringsHelper
|
||||
* This class provides helper functions for reading sharedStrings XML file
|
||||
* Class SharedStringsManager
|
||||
* 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 */
|
||||
const SHARED_STRINGS_XML_FILE_PATH = 'xl/sharedStrings.xml';
|
@ -1,18 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper;
|
||||
namespace Box\Spout\Reader\XLSX\Manager;
|
||||
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use Box\Spout\Reader\XLSX\Creator\EntityFactory;
|
||||
use Box\Spout\Reader\XLSX\Sheet;
|
||||
|
||||
/**
|
||||
* Class SheetHelper
|
||||
* This class provides helper functions related to XLSX sheets
|
||||
* Class SheetManager
|
||||
* 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 */
|
||||
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 */
|
||||
protected $optionsManager;
|
||||
|
||||
/** @var \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings */
|
||||
protected $sharedStringsHelper;
|
||||
/** @var \Box\Spout\Reader\XLSX\Manager\SharedStringsManager Manages shared strings */
|
||||
protected $sharedStringsManager;
|
||||
|
||||
/** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
|
||||
protected $globalFunctionsHelper;
|
||||
@ -52,15 +52,15 @@ class SheetHelper
|
||||
/**
|
||||
* @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 \Box\Spout\Reader\XLSX\Manager\SharedStringsManager Manages shared strings
|
||||
* @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data
|
||||
* @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->optionsManager = $optionsManager;
|
||||
$this->sharedStringsHelper = $sharedStringsHelper;
|
||||
$this->sharedStringsManager = $sharedStringsManager;
|
||||
$this->escaper = $escaper;
|
||||
$this->entityFactory = $entityFactory;
|
||||
}
|
||||
@ -126,7 +126,7 @@ class SheetHelper
|
||||
$sheetName,
|
||||
$isSheetActive,
|
||||
$this->optionsManager,
|
||||
$this->sharedStringsHelper
|
||||
$this->sharedStringsManager
|
||||
);
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper;
|
||||
namespace Box\Spout\Reader\XLSX\Manager;
|
||||
|
||||
use Box\Spout\Reader\XLSX\Creator\EntityFactory;
|
||||
|
||||
/**
|
||||
* Class StyleHelper
|
||||
* This class provides helper functions related to XLSX styles
|
||||
* Class StyleManager
|
||||
* 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 */
|
||||
const STYLES_XML_FILE_PATH = 'xl/styles.xml';
|
@ -9,7 +9,7 @@ use Box\Spout\Reader\Common\Creator\EntityFactoryInterface;
|
||||
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\HelperFactory;
|
||||
use Box\Spout\Reader\XLSX\Creator\ManagerFactory;
|
||||
|
||||
/**
|
||||
* Class Reader
|
||||
@ -19,14 +19,14 @@ use Box\Spout\Reader\XLSX\Creator\HelperFactory;
|
||||
*/
|
||||
class Reader extends ReaderAbstract
|
||||
{
|
||||
/** @var HelperFactory */
|
||||
protected $helperFactory;
|
||||
/** @var ManagerFactory */
|
||||
protected $managerFactory;
|
||||
|
||||
/** @var \ZipArchive */
|
||||
protected $zip;
|
||||
|
||||
/** @var \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings */
|
||||
protected $sharedStringsHelper;
|
||||
/** @var \Box\Spout\Reader\XLSX\Manager\SharedStringsManager Manages shared strings */
|
||||
protected $sharedStringsManager;
|
||||
|
||||
/** @var SheetIterator To iterator over the XLSX sheets */
|
||||
protected $sheetIterator;
|
||||
@ -36,16 +36,16 @@ class Reader extends ReaderAbstract
|
||||
* @param OptionsManagerInterface $optionsManager
|
||||
* @param GlobalFunctionsHelper $globalFunctionsHelper
|
||||
* @param EntityFactoryInterface $entityFactory
|
||||
* @param HelperFactory $helperFactory
|
||||
* @param ManagerFactory $managerFactory
|
||||
*/
|
||||
public function __construct(
|
||||
OptionsManagerInterface $optionsManager,
|
||||
GlobalFunctionsHelper $globalFunctionsHelper,
|
||||
EntityFactoryInterface $entityFactory,
|
||||
HelperFactory $helperFactory)
|
||||
ManagerFactory $managerFactory)
|
||||
{
|
||||
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) {
|
||||
$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
|
||||
$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 {
|
||||
throw new IOException("Could not open $filePath for reading.");
|
||||
}
|
||||
@ -121,8 +121,8 @@ class Reader extends ReaderAbstract
|
||||
$this->zip->close();
|
||||
}
|
||||
|
||||
if ($this->sharedStringsHelper) {
|
||||
$this->sharedStringsHelper->cleanup();
|
||||
if ($this->sharedStringsManager) {
|
||||
$this->sharedStringsManager->cleanup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,9 +3,7 @@
|
||||
namespace Box\Spout\Reader\XLSX;
|
||||
|
||||
use Box\Spout\Reader\IteratorInterface;
|
||||
use Box\Spout\Reader\XLSX\Creator\EntityFactory;
|
||||
use Box\Spout\Reader\XLSX\Creator\HelperFactory;
|
||||
use Box\Spout\Reader\XLSX\Helper\SheetHelper;
|
||||
use Box\Spout\Reader\XLSX\Manager\SheetManager;
|
||||
use Box\Spout\Reader\Exception\NoSheetsFoundException;
|
||||
|
||||
/**
|
||||
@ -23,13 +21,13 @@ class SheetIterator implements IteratorInterface
|
||||
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
|
||||
*/
|
||||
public function __construct($sheetHelper)
|
||||
public function __construct($sheetManager)
|
||||
{
|
||||
// Fetch all available sheets
|
||||
$this->sheets = $sheetHelper->getSheets();
|
||||
$this->sheets = $sheetManager->getSheets();
|
||||
|
||||
if (count($this->sheets) === 0) {
|
||||
throw new NoSheetsFoundException('The file must contain at least one sheet.');
|
||||
|
@ -65,15 +65,16 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
|
||||
->with(CellValueFormatter::XML_NODE_VALUE)
|
||||
->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())
|
||||
->method('shouldFormatNumericValueAsDate')
|
||||
->with(123)
|
||||
->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);
|
||||
|
||||
if ($expectedDateAsString === null) {
|
||||
@ -120,13 +121,14 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testFormatNumericCellValueWithNumbers($value, $expectedFormattedValue, $expectedType)
|
||||
{
|
||||
$styleHelperMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Helper\StyleHelper')->disableOriginalConstructor()->getMock();
|
||||
$styleHelperMock
|
||||
/** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */
|
||||
$styleManagerMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Manager\StyleManager')->disableOriginalConstructor()->getMock();
|
||||
$styleManagerMock
|
||||
->expects($this->once())
|
||||
->method('shouldFormatNumericValueAsDate')
|
||||
->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);
|
||||
|
||||
$this->assertEquals($expectedFormattedValue, $formattedValue);
|
||||
|
@ -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: " - ampersand: &', $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);
|
||||
}
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper\SharedStringsCaching;
|
||||
namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
|
||||
|
||||
use Box\Spout\Reader\XLSX\Creator\HelperFactory;
|
||||
|
||||
/**
|
||||
* Class CachingStrategyFactoryTest
|
||||
*
|
||||
* @package Box\Spout\Reader\XLSX\Helper\SharedStringsCaching
|
||||
* @package Box\Spout\Reader\XLSX\Manager\SharedStringsCaching
|
||||
*/
|
||||
class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -38,7 +39,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var CachingStrategyFactory|\PHPUnit_Framework_MockObject_MockObject $factoryStub */
|
||||
$factoryStub = $this
|
||||
->getMockBuilder('\Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory')
|
||||
->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(['getMemoryLimitInKB'])
|
||||
->getMock();
|
||||
@ -49,7 +50,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
$helperFactory = new HelperFactory($factoryStub);
|
||||
$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));
|
||||
|
||||
$strategy->clearCache();
|
||||
@ -86,7 +87,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var CachingStrategyFactory|\PHPUnit_Framework_MockObject_MockObject $factoryStub */
|
||||
$factoryStub = $this
|
||||
->getMockBuilder('\Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory')
|
||||
->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(['getMemoryLimitFromIni'])
|
||||
->getMock();
|
156
tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php
Normal file
156
tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php
Normal 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: " - ampersand: &', $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);
|
||||
}
|
||||
}
|
@ -1,37 +1,41 @@
|
||||
<?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\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
|
||||
*
|
||||
* @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 $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 */
|
||||
$styleHelper = $this->getMockBuilder('\Box\Spout\Reader\XLSX\Helper\StyleHelper')
|
||||
->setConstructorArgs(['/path/to/file.xlsx', $entityFactory])
|
||||
->setMethods(['getCustomNumberFormats', 'getStylesAttributes'])
|
||||
->getMock();
|
||||
/** @var StyleManager $styleManager */
|
||||
$styleManager = $this->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\StyleManager')
|
||||
->setConstructorArgs(['/path/to/file.xlsx', $entityFactory])
|
||||
->setMethods(['getCustomNumberFormats', 'getStylesAttributes'])
|
||||
->getMock();
|
||||
|
||||
$styleHelper->method('getStylesAttributes')->willReturn($styleAttributes);
|
||||
$styleHelper->method('getCustomNumberFormats')->willReturn($customNumberFormats);
|
||||
$styleManager->method('getStylesAttributes')->willReturn($styleAttributes);
|
||||
$styleManager->method('getCustomNumberFormats')->willReturn($customNumberFormats);
|
||||
|
||||
return $styleHelper;
|
||||
return $styleManager;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,8 +43,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithDefaultStyle()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock();
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(0);
|
||||
$styleManager = $this->getStyleManagerMock();
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(0);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
@ -49,8 +53,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenShouldNotApplyNumberFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => false, 'numFmtId' => 14]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => false, 'numFmtId' => 14]]);
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
@ -59,8 +63,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithGeneralFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 0]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => 0]]);
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
@ -69,8 +73,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithNonDateBuiltinFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 9]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => 9]]);
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
@ -79,8 +83,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithNoNumFmtId()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => null]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => null]]);
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
|
||||
$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];
|
||||
|
||||
foreach ($builtinNumFmtIdsForDate as $builtinNumFmtIdForDate) {
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => $builtinNumFmtIdForDate]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => $builtinNumFmtIdForDate]]);
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertTrue($shouldFormatAsDate);
|
||||
}
|
||||
@ -104,8 +108,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinDateFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => null, 'numFmtId' => 14]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => null, 'numFmtId' => 14]]);
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertTrue($shouldFormatAsDate);
|
||||
}
|
||||
@ -115,8 +119,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinNonDateFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => null, 'numFmtId' => 9]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => null, 'numFmtId' => 9]]);
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
@ -126,8 +130,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenCustomNumberFormatNotFound()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 165]], [166 => []]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => 165]], [166 => []]);
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
@ -176,8 +180,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
public function testShouldFormatNumericValueAsDateWithCustomDateFormats($numberFormat, $expectedResult)
|
||||
{
|
||||
$numFmtId = 165;
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => $numFmtId]], [$numFmtId => $numberFormat]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => $numFmtId]], [$numFmtId => $numberFormat]);
|
||||
$shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertEquals($expectedResult, $shouldFormatAsDate);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user