Rename SharedStringsHelper to SharedStringsManager (#435)
This commit is contained in:
1 parent
cc9a0b526b
commit
7ec0f565fd
4 files changed
+30
-29
No files matched your search
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\XLSX\Manager;
|
||||
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Escaper;
|
||||
|
||||
/**
|
||||
* Class SharedStringsManager
|
||||
* This class provides functions to write shared strings
|
||||
*
|
||||
* @package Box\Spout\Writer\XLSX\Manager
|
||||
*/
|
||||
class SharedStringsManager
|
||||
{
|
||||
const SHARED_STRINGS_FILE_NAME = 'sharedStrings.xml';
|
||||
|
||||
const SHARED_STRINGS_XML_FILE_FIRST_PART_HEADER = <<<EOD
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
|
||||
EOD;
|
||||
|
||||
/**
|
||||
* This number must be really big so that the no generated file will have more strings than that.
|
||||
* If the strings number goes above, characters will be overwritten in an unwanted way and will corrupt the file.
|
||||
*/
|
||||
const DEFAULT_STRINGS_COUNT_PART = 'count="9999999999999" uniqueCount="9999999999999"';
|
||||
|
||||
/** @var resource Pointer to the sharedStrings.xml file */
|
||||
protected $sharedStringsFilePointer;
|
||||
|
||||
/** @var int Number of shared strings already written */
|
||||
protected $numSharedStrings = 0;
|
||||
|
||||
/** @var Escaper\XLSX Strings escaper */
|
||||
protected $stringsEscaper;
|
||||
|
||||
/**
|
||||
* @param string $xlFolder Path to the "xl" folder
|
||||
* @param Escaper\XLSX $stringsEscaper Strings escaper
|
||||
*/
|
||||
public function __construct($xlFolder, $stringsEscaper)
|
||||
{
|
||||
$sharedStringsFilePath = $xlFolder . '/' . self::SHARED_STRINGS_FILE_NAME;
|
||||
$this->sharedStringsFilePointer = fopen($sharedStringsFilePath, 'w');
|
||||
|
||||
$this->throwIfSharedStringsFilePointerIsNotAvailable();
|
||||
|
||||
// the headers is split into different parts so that we can fseek and put in the correct count and uniqueCount later
|
||||
$header = self::SHARED_STRINGS_XML_FILE_FIRST_PART_HEADER . ' ' . self::DEFAULT_STRINGS_COUNT_PART . '>';
|
||||
fwrite($this->sharedStringsFilePointer, $header);
|
||||
|
||||
$this->stringsEscaper = $stringsEscaper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the book has been created. Throws an exception if not created yet.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing
|
||||
*/
|
||||
protected function throwIfSharedStringsFilePointerIsNotAvailable()
|
||||
{
|
||||
if (!$this->sharedStringsFilePointer) {
|
||||
throw new IOException('Unable to open shared strings file for writing.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the given string into the sharedStrings.xml file.
|
||||
* Starting and ending whitespaces are preserved.
|
||||
*
|
||||
* @param string $string
|
||||
* @return int ID of the written shared string
|
||||
*/
|
||||
public function writeString($string)
|
||||
{
|
||||
fwrite($this->sharedStringsFilePointer, '<si><t xml:space="preserve">' . $this->stringsEscaper->escape($string) . '</t></si>');
|
||||
$this->numSharedStrings++;
|
||||
|
||||
// Shared string ID is zero-based
|
||||
return ($this->numSharedStrings - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finishes writing the data in the sharedStrings.xml file and closes the file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if (!is_resource($this->sharedStringsFilePointer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
fwrite($this->sharedStringsFilePointer, '</sst>');
|
||||
|
||||
// Replace the default strings count with the actual number of shared strings in the file header
|
||||
$firstPartHeaderLength = strlen(self::SHARED_STRINGS_XML_FILE_FIRST_PART_HEADER);
|
||||
$defaultStringsCountPartLength = strlen(self::DEFAULT_STRINGS_COUNT_PART);
|
||||
|
||||
// Adding 1 to take into account the space between the last xml attribute and "count"
|
||||
fseek($this->sharedStringsFilePointer, $firstPartHeaderLength + 1);
|
||||
fwrite($this->sharedStringsFilePointer, sprintf("%-{$defaultStringsCountPartLength}s", 'count="' . $this->numSharedStrings . '" uniqueCount="' . $this->numSharedStrings . '"'));
|
||||
|
||||
fclose($this->sharedStringsFilePointer);
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ class WorkbookManager extends WorkbookManagerAbstract
|
||||
*/
|
||||
protected function closeRemainingObjects()
|
||||
{
|
||||
$this->worksheetManager->getSharedStringsHelper()->close();
|
||||
$this->worksheetManager->getSharedStringsManager()->close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,6 @@ use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\XLSX\Helper\SharedStringsHelper;
|
||||
use Box\Spout\Writer\XLSX\Manager\Style\StyleManager;
|
||||
|
||||
/**
|
||||
@@ -42,8 +41,8 @@ EOD;
|
||||
/** @var StyleManager Manages styles */
|
||||
private $styleManager;
|
||||
|
||||
/** @var SharedStringsHelper Helper to write shared strings */
|
||||
private $sharedStringsHelper;
|
||||
/** @var SharedStringsManager Helper to write shared strings */
|
||||
private $sharedStringsManager;
|
||||
|
||||
/** @var \Box\Spout\Common\Escaper\XLSX Strings escaper */
|
||||
private $stringsEscaper;
|
||||
@@ -56,30 +55,30 @@ EOD;
|
||||
*
|
||||
* @param OptionsManagerInterface $optionsManager
|
||||
* @param StyleManager $styleManager
|
||||
* @param SharedStringsHelper $sharedStringsHelper
|
||||
* @param SharedStringsManager $sharedStringsManager
|
||||
* @param \Box\Spout\Common\Escaper\XLSX $stringsEscaper
|
||||
* @param StringHelper $stringHelper
|
||||
*/
|
||||
public function __construct(
|
||||
OptionsManagerInterface $optionsManager,
|
||||
StyleManager $styleManager,
|
||||
SharedStringsHelper $sharedStringsHelper,
|
||||
SharedStringsManager $sharedStringsManager,
|
||||
\Box\Spout\Common\Escaper\XLSX $stringsEscaper,
|
||||
StringHelper $stringHelper)
|
||||
{
|
||||
$this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS);
|
||||
$this->styleManager = $styleManager;
|
||||
$this->sharedStringsHelper = $sharedStringsHelper;
|
||||
$this->sharedStringsManager = $sharedStringsManager;
|
||||
$this->stringsEscaper = $stringsEscaper;
|
||||
$this->stringHelper = $stringHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SharedStringsHelper
|
||||
* @return SharedStringsManager
|
||||
*/
|
||||
public function getSharedStringsHelper()
|
||||
public function getSharedStringsManager()
|
||||
{
|
||||
return $this->sharedStringsHelper;
|
||||
return $this->sharedStringsManager;
|
||||
}
|
||||
|
||||
|
||||
@@ -241,7 +240,7 @@ EOD;
|
||||
if ($this->shouldUseInlineStrings) {
|
||||
$cellXMLFragment = ' t="inlineStr"><is><t>' . $this->stringsEscaper->escape($cellValue) . '</t></is></c>';
|
||||
} else {
|
||||
$sharedStringId = $this->sharedStringsHelper->writeString($cellValue);
|
||||
$sharedStringId = $this->sharedStringsManager->writeString($cellValue);
|
||||
$cellXMLFragment = ' t="s"><v>' . $sharedStringId . '</v></c>';
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user