Merge 1c22a132437a9e3721de4ed21355dab9dbe903bf into 606103f7fc7af596924c2ddabc8b92d4ca1a8a59

This commit is contained in:
Ilya Vertakov 2017-05-19 09:58:07 +00:00 committed by GitHub
commit dc79964ffa
2 changed files with 24 additions and 10 deletions

View File

@ -60,15 +60,28 @@ class SharedStringsHelper
*/
public function hasSharedStrings()
{
$hasSharedStrings = false;
return $this->getSharedStringsPath() !== null;
}
/**
* Returns shared strings XML file path
*
* @return string|null
*/
public function getSharedStringsPath()
{
$sharedStringsPath = null;
$zip = new \ZipArchive();
if ($zip->open($this->filePath) === true) {
$hasSharedStrings = ($zip->locateName(self::SHARED_STRINGS_XML_FILE_PATH) !== false);
$index = $zip->locateName(self::SHARED_STRINGS_XML_FILE_PATH, \ZipArchive::FL_NOCASE);
if ($index !== false) {
$sharedStringsPath = $zip->getNameIndex($index);
}
$zip->close();
}
return $hasSharedStrings;
return $sharedStringsPath;
}
/**
@ -83,17 +96,18 @@ class SharedStringsHelper
* Please note that SimpleXML does not provide such a functionality but since it is faster
* and more handy to parse few XML nodes, it is used in combination with XMLReader for that purpose.
*
* @param string $stringsPath Path of the XLSX's shared strings
* @return void
* @throws \Box\Spout\Common\Exception\IOException If sharedStrings.xml can't be read
*/
public function extractSharedStrings()
public function extractSharedStrings($stringsPath = self::SHARED_STRINGS_XML_FILE_PATH)
{
$xmlReader = new XMLReader();
$sharedStringIndex = 0;
$sharedStringsFilePath = $this->getSharedStringsFilePath();
$sharedStringsFilePath = $this->getSharedStringsFilePath($stringsPath);
if ($xmlReader->open($sharedStringsFilePath) === false) {
throw new IOException('Could not open "' . self::SHARED_STRINGS_XML_FILE_PATH . '".');
throw new IOException('Could not open "' . $stringsPath . '".');
}
try {
@ -122,9 +136,9 @@ class SharedStringsHelper
/**
* @return string The path to the shared strings XML file
*/
protected function getSharedStringsFilePath()
protected function getSharedStringsFilePath($path = self::SHARED_STRINGS_XML_FILE_PATH)
{
return 'zip://' . $this->filePath . '#' . self::SHARED_STRINGS_XML_FILE_PATH;
return 'zip://' . $this->filePath . '#' . $path;
}
/**

View File

@ -74,9 +74,9 @@ class Reader extends AbstractReader
if ($this->zip->open($filePath) === true) {
$this->sharedStringsHelper = new SharedStringsHelper($filePath, $this->getOptions()->getTempFolder());
if ($this->sharedStringsHelper->hasSharedStrings()) {
if ($sharedStringsPath = $this->sharedStringsHelper->getSharedStringsPath()) {
// Extracts all the strings from the sheets for easy access in the future
$this->sharedStringsHelper->extractSharedStrings();
$this->sharedStringsHelper->extractSharedStrings($sharedStringsPath);
}
$this->sheetIterator = new SheetIterator($filePath, $this->getOptions(), $this->sharedStringsHelper, $this->globalFunctionsHelper);