Convert escapers to singletons (#239)

This commit is contained in:
Adrien Loison 2016-05-30 13:55:21 -07:00
parent efebfb2bc2
commit 1d3a9f939c
15 changed files with 64 additions and 14 deletions

View File

@ -2,6 +2,8 @@
namespace Box\Spout\Common\Escaper;
use Box\Spout\Common\Singleton;
/**
* Class ODS
* Provides functions to escape and unescape data for ODS files
@ -10,6 +12,8 @@ namespace Box\Spout\Common\Escaper;
*/
class ODS implements EscaperInterface
{
use Singleton;
/**
* Escapes the given string to make it compatible with XLSX
*

View File

@ -2,6 +2,8 @@
namespace Box\Spout\Common\Escaper;
use Box\Spout\Common\Singleton;
/**
* Class XLSX
* Provides functions to escape and unescape data for XLSX files
@ -10,13 +12,15 @@ namespace Box\Spout\Common\Escaper;
*/
class XLSX implements EscaperInterface
{
use Singleton;
/** @var string[] Control characters to be escaped */
protected $controlCharactersEscapingMap;
/**
*
* Initializes the singleton instance
*/
public function __construct()
protected function init()
{
$this->controlCharactersEscapingMap = $this->getControlCharactersEscapingMap();
}

View File

@ -59,7 +59,7 @@ class EncodingHelper
{
$byteOffsetToSkipBom = 0;
if ($this->hasBom($filePointer, $encoding)) {
if ($this->hasBOM($filePointer, $encoding)) {
$bomUsed = $this->supportedEncodingsWithBom[$encoding];
// we skip the N first bytes

View 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() {}
}

View File

@ -57,6 +57,7 @@ class RowIterator implements IteratorInterface
* @param string $fieldDelimiter Character that delimits fields
* @param string $fieldEnclosure Character that enclose fields
* @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
*/
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineDelimiter, $globalFunctionsHelper)

View File

@ -48,7 +48,7 @@ class CellValueFormatter
$this->shouldFormatDates = $shouldFormatDates;
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$this->escaper = new \Box\Spout\Common\Escaper\ODS();
$this->escaper = \Box\Spout\Common\Escaper\ODS::getInstance();
}
/**

View File

@ -51,7 +51,7 @@ class SheetIterator implements IteratorInterface
$this->xmlReader = new XMLReader();
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$this->escaper = new \Box\Spout\Common\Escaper\ODS();
$this->escaper = \Box\Spout\Common\Escaper\ODS::getInstance();
}
/**

View File

@ -62,7 +62,7 @@ class CellValueFormatter
$this->shouldFormatDates = $shouldFormatDates;
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$this->escaper = new \Box\Spout\Common\Escaper\XLSX();
$this->escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
}
/**

View File

@ -80,7 +80,7 @@ class SharedStringsHelper
$xmlReader = new XMLReader();
$sharedStringIndex = 0;
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$escaper = new \Box\Spout\Common\Escaper\XLSX();
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
$sharedStringsFilePath = $this->getSharedStringsFilePath();
if ($xmlReader->open($sharedStringsFilePath) === false) {

View File

@ -87,7 +87,7 @@ class SheetHelper
$escapedSheetName = $xmlReaderOnSheetNode->getAttribute('name');
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$escaper = new \Box\Spout\Common\Escaper\XLSX();
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
$sheetName = $escaper->unescape($escapedSheetName);
$sheetDataXMLFilePath = $this->getSheetDataXMLFilePathForSheetId($sheetId);

View File

@ -47,7 +47,7 @@ class Worksheet implements WorksheetInterface
{
$this->externalSheet = $externalSheet;
/** @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->stringHelper = new StringHelper();

View File

@ -284,7 +284,7 @@ EOD;
EOD;
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$escaper = new \Box\Spout\Common\Escaper\XLSX();
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
/** @var Worksheet $worksheet */
foreach ($worksheets as $worksheet) {

View File

@ -49,7 +49,7 @@ EOD;
fwrite($this->sharedStringsFilePointer, $header);
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$this->stringsEscaper = new \Box\Spout\Common\Escaper\XLSX();
$this->stringsEscaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
}
/**

View File

@ -56,7 +56,7 @@ EOD;
$this->shouldUseInlineStrings = $shouldUseInlineStrings;
/** @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->startSheet();

View File

@ -35,7 +35,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
public function testEscape($stringToEscape, $expectedEscapedString)
{
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$escaper = new \Box\Spout\Common\Escaper\XLSX();
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
$escapedString = $escaper->escape($stringToEscape);
$this->assertEquals($expectedEscapedString, $escapedString, 'Incorrect escaped string');
@ -67,7 +67,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
public function testUnescape($stringToUnescape, $expectedUnescapedString)
{
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$escaper = new \Box\Spout\Common\Escaper\XLSX();
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
$unescapedString = $escaper->unescape($stringToUnescape);
$this->assertEquals($expectedUnescapedString, $unescapedString, 'Incorrect escaped string');