diff --git a/src/Spout/Common/Escaper/CSV.php b/src/Spout/Common/Helper/Escaper/CSV.php similarity index 89% rename from src/Spout/Common/Escaper/CSV.php rename to src/Spout/Common/Helper/Escaper/CSV.php index 4bc2d1a..f572d67 100644 --- a/src/Spout/Common/Escaper/CSV.php +++ b/src/Spout/Common/Helper/Escaper/CSV.php @@ -1,12 +1,12 @@ escapableControlCharactersPattern = $this->getEscapableControlCharactersPattern(); - $this->controlCharactersEscapingMap = $this->getControlCharactersEscapingMap(); - $this->controlCharactersEscapingReverseMap = array_flip($this->controlCharactersEscapingMap); + if (!$this->isAlreadyInitialized) { + $this->escapableControlCharactersPattern = $this->getEscapableControlCharactersPattern(); + $this->controlCharactersEscapingMap = $this->getControlCharactersEscapingMap(); + $this->controlCharactersEscapingReverseMap = array_flip($this->controlCharactersEscapingMap); + + $this->isAlreadyInitialized = true; + } } /** @@ -41,6 +44,8 @@ class XLSX implements EscaperInterface */ public function escape($string) { + $this->initIfNeeded(); + $escapedString = $this->escapeControlCharacters($string); // @NOTE: Using ENT_NOQUOTES as only XML entities ('<', '>', '&') need to be encoded. // Single and double quotes can be left as is. @@ -57,6 +62,8 @@ class XLSX implements EscaperInterface */ public function unescape($string) { + $this->initIfNeeded(); + // ============== // = WARNING = // ============== diff --git a/src/Spout/Common/Singleton.php b/src/Spout/Common/Singleton.php deleted file mode 100644 index 015ede8..0000000 --- a/src/Spout/Common/Singleton.php +++ /dev/null @@ -1,41 +0,0 @@ -init(); - } - - /** - * Initializes the singleton - * @return void - */ - protected function init() {} - - final private function __wakeup() {} - final private function __clone() {} -} diff --git a/src/Spout/Reader/ODS/Creator/HelperFactory.php b/src/Spout/Reader/ODS/Creator/HelperFactory.php index cebd732..1fad2e9 100644 --- a/src/Spout/Reader/ODS/Creator/HelperFactory.php +++ b/src/Spout/Reader/ODS/Creator/HelperFactory.php @@ -20,7 +20,8 @@ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory */ public function createCellValueFormatter($shouldFormatDates) { - return new CellValueFormatter($shouldFormatDates); + $escaper = $this->createStringsEscaper(); + return new CellValueFormatter($shouldFormatDates, $escaper); } /** @@ -30,4 +31,13 @@ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory { return new SettingsHelper(); } + + /** + * @return \Box\Spout\Common\Helper\Escaper\ODS + */ + public function createStringsEscaper() + { + /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ + return new \Box\Spout\Common\Helper\Escaper\ODS(); + } } diff --git a/src/Spout/Reader/ODS/Helper/CellValueFormatter.php b/src/Spout/Reader/ODS/Helper/CellValueFormatter.php index 0c91410..661db30 100644 --- a/src/Spout/Reader/ODS/Helper/CellValueFormatter.php +++ b/src/Spout/Reader/ODS/Helper/CellValueFormatter.php @@ -38,18 +38,17 @@ class CellValueFormatter /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ protected $shouldFormatDates; - /** @var \Box\Spout\Common\Escaper\ODS Used to unescape XML data */ + /** @var \Box\Spout\Common\Helper\Escaper\ODS Used to unescape XML data */ protected $escaper; /** * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings + * @param \Box\Spout\Common\Helper\Escaper\ODS $escaper Used to unescape XML data */ - public function __construct($shouldFormatDates) + public function __construct($shouldFormatDates, $escaper) { $this->shouldFormatDates = $shouldFormatDates; - - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $this->escaper = \Box\Spout\Common\Escaper\ODS::getInstance(); + $this->escaper = $escaper; } /** diff --git a/src/Spout/Reader/ODS/SheetIterator.php b/src/Spout/Reader/ODS/SheetIterator.php index 6d2f69a..eab49af 100644 --- a/src/Spout/Reader/ODS/SheetIterator.php +++ b/src/Spout/Reader/ODS/SheetIterator.php @@ -36,7 +36,7 @@ class SheetIterator implements IteratorInterface /** @var XMLReader The XMLReader object that will help read sheet's XML data */ protected $xmlReader; - /** @var \Box\Spout\Common\Escaper\ODS Used to unescape XML data */ + /** @var \Box\Spout\Common\Helper\Escaper\ODS Used to unescape XML data */ protected $escaper; /** @var bool Whether there are still at least a sheet to be read */ @@ -61,8 +61,7 @@ class SheetIterator implements IteratorInterface $this->entityFactory = $entityFactory; $this->xmlReader = $entityFactory->createXMLReader(); - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $this->escaper = \Box\Spout\Common\Escaper\ODS::getInstance(); + $this->escaper = $helperFactory->createStringsEscaper(); $settingsHelper = $helperFactory->createSettingsHelper(); $this->activeSheetName = $settingsHelper->getActiveSheetName($filePath); diff --git a/src/Spout/Reader/XLSX/Creator/HelperFactory.php b/src/Spout/Reader/XLSX/Creator/HelperFactory.php index 9b70d63..a18b933 100644 --- a/src/Spout/Reader/XLSX/Creator/HelperFactory.php +++ b/src/Spout/Reader/XLSX/Creator/HelperFactory.php @@ -2,6 +2,7 @@ namespace Box\Spout\Reader\XLSX\Creator; +use Box\Spout\Common\Helper\Escaper; use Box\Spout\Reader\XLSX\Helper\CellValueFormatter; use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory; use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper; @@ -49,7 +50,8 @@ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory */ public function createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $entityFactory) { - return new SheetHelper($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $entityFactory); + $escaper = $this->createStringsEscaper(); + return new SheetHelper($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $escaper, $entityFactory); } /** @@ -70,6 +72,16 @@ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory */ public function createCellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates) { - return new CellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates); + $escaper = $this->createStringsEscaper(); + return new CellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates, $escaper); + } + + /** + * @return Escaper\XLSX + */ + public function createStringsEscaper() + { + /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ + return new Escaper\XLSX(); } } diff --git a/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php b/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php index b4c6256..43300bc 100644 --- a/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php +++ b/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php @@ -47,22 +47,21 @@ class CellValueFormatter /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ protected $shouldFormatDates; - /** @var \Box\Spout\Common\Escaper\XLSX Used to unescape XML data */ + /** @var \Box\Spout\Common\Helper\Escaper\XLSX Used to unescape XML data */ protected $escaper; /** * @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 \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data */ - public function __construct($sharedStringsHelper, $styleHelper, $shouldFormatDates) + public function __construct($sharedStringsHelper, $styleHelper, $shouldFormatDates, $escaper) { $this->sharedStringsHelper = $sharedStringsHelper; $this->styleHelper = $styleHelper; $this->shouldFormatDates = $shouldFormatDates; - - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $this->escaper = \Box\Spout\Common\Escaper\XLSX::getInstance(); + $this->escaper = $escaper; } /** diff --git a/src/Spout/Reader/XLSX/Helper/SheetHelper.php b/src/Spout/Reader/XLSX/Helper/SheetHelper.php index 8b6af6c..8319050 100644 --- a/src/Spout/Reader/XLSX/Helper/SheetHelper.php +++ b/src/Spout/Reader/XLSX/Helper/SheetHelper.php @@ -46,19 +46,24 @@ class SheetHelper /** @var EntityFactory Factory to create entities */ protected $entityFactory; + /** @var \Box\Spout\Common\Helper\Escaper\XLSX Used to unescape XML data */ + protected $escaper; + /** * @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\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper + * @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, $globalFunctionsHelper, $entityFactory) + public function __construct($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $escaper, $entityFactory) { $this->filePath = $filePath; $this->optionsManager = $optionsManager; $this->sharedStringsHelper = $sharedStringsHelper; $this->globalFunctionsHelper = $globalFunctionsHelper; + $this->escaper = $escaper; $this->entityFactory = $entityFactory; } @@ -112,10 +117,7 @@ class SheetHelper { $sheetId = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_R_ID); $escapedSheetName = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_NAME); - - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $escaper = \Box\Spout\Common\Escaper\XLSX::getInstance(); - $sheetName = $escaper->unescape($escapedSheetName); + $sheetName = $this->escaper->unescape($escapedSheetName); $sheetDataXMLFilePath = $this->getSheetDataXMLFilePathForSheetId($sheetId); diff --git a/src/Spout/Writer/ODS/Creator/InternalFactory.php b/src/Spout/Writer/ODS/Creator/InternalFactory.php index 6720005..49ac6d2 100644 --- a/src/Spout/Writer/ODS/Creator/InternalFactory.php +++ b/src/Spout/Writer/ODS/Creator/InternalFactory.php @@ -13,7 +13,7 @@ use Box\Spout\Writer\ODS\Manager\Style\StyleManager; use Box\Spout\Writer\ODS\Manager\Style\StyleRegistry; use Box\Spout\Writer\ODS\Manager\WorkbookManager; use Box\Spout\Writer\ODS\Manager\WorksheetManager; -use \Box\Spout\Common\Escaper; +use Box\Spout\Common\Helper\Escaper; /** * Class InternalFactory @@ -109,7 +109,7 @@ class InternalFactory implements InternalFactoryInterface */ private function createStringsEscaper() { - return Escaper\ODS::getInstance(); + return new Escaper\ODS(); } /** diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 189ac08..4638e15 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -19,7 +19,7 @@ use Box\Spout\Writer\ODS\Manager\Style\StyleManager; */ class WorksheetManager implements WorksheetManagerInterface { - /** @var \Box\Spout\Common\Escaper\ODS Strings escaper */ + /** @var \Box\Spout\Common\Helper\Escaper\ODS Strings escaper */ private $stringsEscaper; /** @var StringHelper String helper */ @@ -28,11 +28,11 @@ class WorksheetManager implements WorksheetManagerInterface /** * WorksheetManager constructor. * - * @param \Box\Spout\Common\Escaper\ODS $stringsEscaper + * @param \Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper * @param StringHelper $stringHelper */ public function __construct( - \Box\Spout\Common\Escaper\ODS $stringsEscaper, + \Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper, StringHelper $stringHelper) { $this->stringsEscaper = $stringsEscaper; @@ -205,4 +205,4 @@ class WorksheetManager implements WorksheetManagerInterface fclose($worksheetFilePointer); } -} \ No newline at end of file +} diff --git a/src/Spout/Writer/XLSX/Creator/InternalFactory.php b/src/Spout/Writer/XLSX/Creator/InternalFactory.php index 8a2f29a..b96d323 100644 --- a/src/Spout/Writer/XLSX/Creator/InternalFactory.php +++ b/src/Spout/Writer/XLSX/Creator/InternalFactory.php @@ -2,7 +2,7 @@ namespace Box\Spout\Writer\XLSX\Creator; -use Box\Spout\Common\Escaper; +use Box\Spout\Common\Helper\Escaper; use Box\Spout\Common\Helper\StringHelper; use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; @@ -113,8 +113,9 @@ class InternalFactory implements InternalFactoryInterface { $tempFolder = $optionsManager->getOption(Options::TEMP_FOLDER); $zipHelper = $this->createZipHelper(); + $escaper = $this->createStringsEscaper(); - return new FileSystemHelper($tempFolder, $zipHelper); + return new FileSystemHelper($tempFolder, $zipHelper, $escaper); } /** @@ -130,7 +131,7 @@ class InternalFactory implements InternalFactoryInterface */ private function createStringsEscaper() { - return Escaper\XLSX::getInstance(); + return new Escaper\XLSX(); } /** diff --git a/src/Spout/Writer/XLSX/Helper/FileSystemHelper.php b/src/Spout/Writer/XLSX/Helper/FileSystemHelper.php index e640d09..c73d111 100644 --- a/src/Spout/Writer/XLSX/Helper/FileSystemHelper.php +++ b/src/Spout/Writer/XLSX/Helper/FileSystemHelper.php @@ -34,6 +34,9 @@ class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper impleme /** @var ZipHelper Helper to perform tasks with Zip archive */ private $zipHelper; + /** @var \Box\Spout\Common\Helper\Escaper\XLSX Used to escape XML data */ + private $escaper; + /** @var string Path to the root folder inside the temp folder where the files to create the XLSX will be stored */ private $rootFolder; @@ -55,11 +58,13 @@ class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper impleme /** * @param string $baseFolderPath The path of the base folder where all the I/O can occur * @param ZipHelper $zipHelper Helper to perform tasks with Zip archive + * @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to escape XML data */ - public function __construct($baseFolderPath, $zipHelper) + public function __construct($baseFolderPath, $zipHelper, $escaper) { parent::__construct($baseFolderPath); $this->zipHelper = $zipHelper; + $this->escaper = $escaper; } /** @@ -298,14 +303,11 @@ EOD; EOD; - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $escaper = \Box\Spout\Common\Escaper\XLSX::getInstance(); - /** @var Worksheet $worksheet */ foreach ($worksheets as $worksheet) { $worksheetName = $worksheet->getExternalSheet()->getName(); $worksheetId = $worksheet->getId(); - $workbookXmlFileContents .= ''; + $workbookXmlFileContents .= ''; } $workbookXmlFileContents .= <<shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS); diff --git a/tests/Spout/Common/Escaper/ODSTest.php b/tests/Spout/Common/Helper/Escaper/ODSTest.php similarity index 83% rename from tests/Spout/Common/Escaper/ODSTest.php rename to tests/Spout/Common/Helper/Escaper/ODSTest.php index 77b4913..02d7ded 100644 --- a/tests/Spout/Common/Escaper/ODSTest.php +++ b/tests/Spout/Common/Helper/Escaper/ODSTest.php @@ -1,11 +1,13 @@ escape($stringToEscape); $this->assertEquals($expectedEscapedString, $escapedString, 'Incorrect escaped string'); diff --git a/tests/Spout/Common/Escaper/XLSXTest.php b/tests/Spout/Common/Helper/Escaper/XLSXTest.php similarity index 86% rename from tests/Spout/Common/Escaper/XLSXTest.php rename to tests/Spout/Common/Helper/Escaper/XLSXTest.php index 0f2098f..540cf04 100644 --- a/tests/Spout/Common/Escaper/XLSXTest.php +++ b/tests/Spout/Common/Helper/Escaper/XLSXTest.php @@ -1,11 +1,13 @@ escape($stringToEscape); $this->assertEquals($expectedEscapedString, $escapedString, 'Incorrect escaped string'); @@ -74,8 +75,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase */ public function testUnescape($stringToUnescape, $expectedUnescapedString) { - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $escaper = \Box\Spout\Common\Escaper\XLSX::getInstance(); + $escaper = new Escaper\XLSX(); $unescapedString = $escaper->unescape($stringToUnescape); $this->assertEquals($expectedUnescapedString, $unescapedString, 'Incorrect escaped string'); diff --git a/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php b/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php index 96c71a9..bef5512 100644 --- a/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php +++ b/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php @@ -2,6 +2,8 @@ namespace Box\Spout\Reader\XLSX\Helper; +use Box\Spout\Common\Helper\Escaper; + /** * Class CellValueFormatterTest * @@ -71,7 +73,7 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase ->with(123) ->will($this->returnValue(true)); - $formatter = new CellValueFormatter(null, $styleHelperMock, false); + $formatter = new CellValueFormatter(null, $styleHelperMock, false, new Escaper\XLSX()); $result = $formatter->extractAndFormatNodeValue($nodeMock); if ($expectedDateAsString === null) { @@ -124,7 +126,7 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase ->method('shouldFormatNumericValueAsDate') ->will($this->returnValue(false)); - $formatter = new CellValueFormatter(null, $styleHelperMock, false); + $formatter = new CellValueFormatter(null, $styleHelperMock, false, new Escaper\XLSX()); $formattedValue = \ReflectionHelper::callMethodOnObject($formatter, 'formatNumericCellValue', $value, 0); $this->assertEquals($expectedFormattedValue, $formattedValue); @@ -167,7 +169,7 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase ->with(CellValueFormatter::XML_NODE_INLINE_STRING_VALUE) ->will($this->returnValue($nodeListMock)); - $formatter = new CellValueFormatter(null, null, false); + $formatter = new CellValueFormatter(null, null, false, new Escaper\XLSX()); $formattedValue = \ReflectionHelper::callMethodOnObject($formatter, 'formatInlineStringCellValue', $nodeMock); $this->assertEquals($expectedFormattedValue, $formattedValue);