Convert escapers to singletons
This commit is contained in:
parent
efebfb2bc2
commit
6175cbdb4f
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Box\Spout\Common\Escaper;
|
namespace Box\Spout\Common\Escaper;
|
||||||
|
|
||||||
|
use Box\Spout\Common\Singleton;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ODS
|
* Class ODS
|
||||||
* Provides functions to escape and unescape data for ODS files
|
* Provides functions to escape and unescape data for ODS files
|
||||||
@ -10,6 +12,8 @@ namespace Box\Spout\Common\Escaper;
|
|||||||
*/
|
*/
|
||||||
class ODS implements EscaperInterface
|
class ODS implements EscaperInterface
|
||||||
{
|
{
|
||||||
|
use Singleton;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escapes the given string to make it compatible with XLSX
|
* Escapes the given string to make it compatible with XLSX
|
||||||
*
|
*
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Box\Spout\Common\Escaper;
|
namespace Box\Spout\Common\Escaper;
|
||||||
|
|
||||||
|
use Box\Spout\Common\Singleton;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class XLSX
|
* Class XLSX
|
||||||
* Provides functions to escape and unescape data for XLSX files
|
* Provides functions to escape and unescape data for XLSX files
|
||||||
@ -10,13 +12,15 @@ namespace Box\Spout\Common\Escaper;
|
|||||||
*/
|
*/
|
||||||
class XLSX implements EscaperInterface
|
class XLSX implements EscaperInterface
|
||||||
{
|
{
|
||||||
|
use Singleton;
|
||||||
|
|
||||||
/** @var string[] Control characters to be escaped */
|
/** @var string[] Control characters to be escaped */
|
||||||
protected $controlCharactersEscapingMap;
|
protected $controlCharactersEscapingMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Initializes the singleton instance
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
protected function init()
|
||||||
{
|
{
|
||||||
$this->controlCharactersEscapingMap = $this->getControlCharactersEscapingMap();
|
$this->controlCharactersEscapingMap = $this->getControlCharactersEscapingMap();
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ class EncodingHelper
|
|||||||
{
|
{
|
||||||
$byteOffsetToSkipBom = 0;
|
$byteOffsetToSkipBom = 0;
|
||||||
|
|
||||||
if ($this->hasBom($filePointer, $encoding)) {
|
if ($this->hasBOM($filePointer, $encoding)) {
|
||||||
$bomUsed = $this->supportedEncodingsWithBom[$encoding];
|
$bomUsed = $this->supportedEncodingsWithBom[$encoding];
|
||||||
|
|
||||||
// we skip the N first bytes
|
// we skip the N first bytes
|
||||||
|
41
src/Spout/Common/Singleton.php
Normal file
41
src/Spout/Common/Singleton.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Box\Spout\Common;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Singleton
|
||||||
|
* Defines a class as a singleton.
|
||||||
|
*
|
||||||
|
* @package Box\Spout\Common
|
||||||
|
*/
|
||||||
|
trait Singleton
|
||||||
|
{
|
||||||
|
protected static $instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
final public static function getInstance()
|
||||||
|
{
|
||||||
|
return isset(static::$instance)
|
||||||
|
? static::$instance
|
||||||
|
: static::$instance = new static;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton constructor.
|
||||||
|
*/
|
||||||
|
final private function __construct()
|
||||||
|
{
|
||||||
|
$this->init();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the singleton
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function init() {}
|
||||||
|
|
||||||
|
final private function __wakeup() {}
|
||||||
|
final private function __clone() {}
|
||||||
|
}
|
@ -57,6 +57,7 @@ class RowIterator implements IteratorInterface
|
|||||||
* @param string $fieldDelimiter Character that delimits fields
|
* @param string $fieldDelimiter Character that delimits fields
|
||||||
* @param string $fieldEnclosure Character that enclose fields
|
* @param string $fieldEnclosure Character that enclose fields
|
||||||
* @param string $encoding Encoding of the CSV file to be read
|
* @param string $encoding Encoding of the CSV file to be read
|
||||||
|
* @param string $endOfLineDelimiter End of line delimiter
|
||||||
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
|
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
|
||||||
*/
|
*/
|
||||||
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineDelimiter, $globalFunctionsHelper)
|
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineDelimiter, $globalFunctionsHelper)
|
||||||
|
@ -48,7 +48,7 @@ class CellValueFormatter
|
|||||||
$this->shouldFormatDates = $shouldFormatDates;
|
$this->shouldFormatDates = $shouldFormatDates;
|
||||||
|
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$this->escaper = new \Box\Spout\Common\Escaper\ODS();
|
$this->escaper = \Box\Spout\Common\Escaper\ODS::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,7 +51,7 @@ class SheetIterator implements IteratorInterface
|
|||||||
$this->xmlReader = new XMLReader();
|
$this->xmlReader = new XMLReader();
|
||||||
|
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$this->escaper = new \Box\Spout\Common\Escaper\ODS();
|
$this->escaper = \Box\Spout\Common\Escaper\ODS::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -62,7 +62,7 @@ class CellValueFormatter
|
|||||||
$this->shouldFormatDates = $shouldFormatDates;
|
$this->shouldFormatDates = $shouldFormatDates;
|
||||||
|
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$this->escaper = new \Box\Spout\Common\Escaper\XLSX();
|
$this->escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,7 +80,7 @@ class SharedStringsHelper
|
|||||||
$xmlReader = new XMLReader();
|
$xmlReader = new XMLReader();
|
||||||
$sharedStringIndex = 0;
|
$sharedStringIndex = 0;
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$escaper = new \Box\Spout\Common\Escaper\XLSX();
|
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
|
||||||
|
|
||||||
$sharedStringsFilePath = $this->getSharedStringsFilePath();
|
$sharedStringsFilePath = $this->getSharedStringsFilePath();
|
||||||
if ($xmlReader->open($sharedStringsFilePath) === false) {
|
if ($xmlReader->open($sharedStringsFilePath) === false) {
|
||||||
|
@ -87,7 +87,7 @@ class SheetHelper
|
|||||||
$escapedSheetName = $xmlReaderOnSheetNode->getAttribute('name');
|
$escapedSheetName = $xmlReaderOnSheetNode->getAttribute('name');
|
||||||
|
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$escaper = new \Box\Spout\Common\Escaper\XLSX();
|
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
|
||||||
$sheetName = $escaper->unescape($escapedSheetName);
|
$sheetName = $escaper->unescape($escapedSheetName);
|
||||||
|
|
||||||
$sheetDataXMLFilePath = $this->getSheetDataXMLFilePathForSheetId($sheetId);
|
$sheetDataXMLFilePath = $this->getSheetDataXMLFilePathForSheetId($sheetId);
|
||||||
|
@ -47,7 +47,7 @@ class Worksheet implements WorksheetInterface
|
|||||||
{
|
{
|
||||||
$this->externalSheet = $externalSheet;
|
$this->externalSheet = $externalSheet;
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$this->stringsEscaper = new \Box\Spout\Common\Escaper\ODS();
|
$this->stringsEscaper = \Box\Spout\Common\Escaper\ODS::getInstance();
|
||||||
$this->worksheetFilePath = $worksheetFilesFolder . '/sheet' . $externalSheet->getIndex() . '.xml';
|
$this->worksheetFilePath = $worksheetFilesFolder . '/sheet' . $externalSheet->getIndex() . '.xml';
|
||||||
|
|
||||||
$this->stringHelper = new StringHelper();
|
$this->stringHelper = new StringHelper();
|
||||||
|
@ -284,7 +284,7 @@ EOD;
|
|||||||
EOD;
|
EOD;
|
||||||
|
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$escaper = new \Box\Spout\Common\Escaper\XLSX();
|
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
|
||||||
|
|
||||||
/** @var Worksheet $worksheet */
|
/** @var Worksheet $worksheet */
|
||||||
foreach ($worksheets as $worksheet) {
|
foreach ($worksheets as $worksheet) {
|
||||||
|
@ -49,7 +49,7 @@ EOD;
|
|||||||
fwrite($this->sharedStringsFilePointer, $header);
|
fwrite($this->sharedStringsFilePointer, $header);
|
||||||
|
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$this->stringsEscaper = new \Box\Spout\Common\Escaper\XLSX();
|
$this->stringsEscaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,7 +56,7 @@ EOD;
|
|||||||
$this->shouldUseInlineStrings = $shouldUseInlineStrings;
|
$this->shouldUseInlineStrings = $shouldUseInlineStrings;
|
||||||
|
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$this->stringsEscaper = new \Box\Spout\Common\Escaper\XLSX();
|
$this->stringsEscaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
|
||||||
|
|
||||||
$this->worksheetFilePath = $worksheetFilesFolder . '/' . strtolower($this->externalSheet->getName()) . '.xml';
|
$this->worksheetFilePath = $worksheetFilesFolder . '/' . strtolower($this->externalSheet->getName()) . '.xml';
|
||||||
$this->startSheet();
|
$this->startSheet();
|
||||||
|
@ -35,7 +35,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testEscape($stringToEscape, $expectedEscapedString)
|
public function testEscape($stringToEscape, $expectedEscapedString)
|
||||||
{
|
{
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$escaper = new \Box\Spout\Common\Escaper\XLSX();
|
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
|
||||||
$escapedString = $escaper->escape($stringToEscape);
|
$escapedString = $escaper->escape($stringToEscape);
|
||||||
|
|
||||||
$this->assertEquals($expectedEscapedString, $escapedString, 'Incorrect escaped string');
|
$this->assertEquals($expectedEscapedString, $escapedString, 'Incorrect escaped string');
|
||||||
@ -67,7 +67,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testUnescape($stringToUnescape, $expectedUnescapedString)
|
public function testUnescape($stringToUnescape, $expectedUnescapedString)
|
||||||
{
|
{
|
||||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||||
$escaper = new \Box\Spout\Common\Escaper\XLSX();
|
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
|
||||||
$unescapedString = $escaper->unescape($stringToUnescape);
|
$unescapedString = $escaper->unescape($stringToUnescape);
|
||||||
|
|
||||||
$this->assertEquals($expectedUnescapedString, $unescapedString, 'Incorrect escaped string');
|
$this->assertEquals($expectedUnescapedString, $unescapedString, 'Incorrect escaped string');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user