From d7751afbecdfea989495bfdc86017a1366b5f5a5 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sat, 12 May 2018 22:00:29 +0900 Subject: [PATCH] Fix tests on PHP 5.6 --- src/PhpSpreadsheet/Writer/Xlsx.php | 21 ++++++++++--------- .../Writer/Xlsx/ContentTypes.php | 11 +++++----- src/PhpSpreadsheet/Writer/Xlsx/Drawing.php | 5 +++-- src/PhpSpreadsheet/Writer/Xlsx/Rels.php | 10 +++++---- src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 12 ++++++----- 5 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Xlsx.php b/src/PhpSpreadsheet/Writer/Xlsx.php index 2995bd73..dd19021e 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx.php +++ b/src/PhpSpreadsheet/Writer/Xlsx.php @@ -137,9 +137,9 @@ class Xlsx extends BaseWriter } $hashTablesArray = ['stylesConditionalHashTable', 'fillHashTable', 'fontHashTable', - 'bordersHashTable', 'numFmtHashTable', 'drawingHashTable', - 'styleHashTable', - ]; + 'bordersHashTable', 'numFmtHashTable', 'drawingHashTable', + 'styleHashTable', + ]; // Set HashTable variables foreach ($hashTablesArray as $tableName) { @@ -298,13 +298,14 @@ class Xlsx extends BaseWriter // Add unparsedLoadedData $sheetCodeName = $this->spreadSheet->getSheet($i)->getCodeName(); - if (isset($this->spreadSheet->getUnparsedLoadedData()['sheets'][$sheetCodeName]['ctrlProps'])) { - foreach ($this->spreadSheet->getUnparsedLoadedData()['sheets'][$sheetCodeName]['ctrlProps'] as $ctrlProp) { + $unparsedLoadedData = $this->spreadSheet->getUnparsedLoadedData(); + if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'])) { + foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'] as $ctrlProp) { $zip->addFromString($ctrlProp['filePath'], $ctrlProp['content']); } } - if (isset($this->spreadSheet->getUnparsedLoadedData()['sheets'][$sheetCodeName]['printerSettings'])) { - foreach ($this->spreadSheet->getUnparsedLoadedData()['sheets'][$sheetCodeName]['printerSettings'] as $ctrlProp) { + if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'])) { + foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'] as $ctrlProp) { $zip->addFromString($ctrlProp['filePath'], $ctrlProp['content']); } } @@ -322,7 +323,7 @@ class Xlsx extends BaseWriter // Drawings $zip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts)); - } elseif (isset($this->spreadSheet->getUnparsedLoadedData()['sheets'][$sheetCodeName]['drawingAlternateContents'])) { + } elseif (isset($unparsedLoadedData['sheets'][$sheetCodeName]['drawingAlternateContents'])) { // Drawings $zip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts)); } @@ -337,8 +338,8 @@ class Xlsx extends BaseWriter } // Add unparsed relationship parts - if (isset($this->spreadSheet->getUnparsedLoadedData()['sheets'][$this->spreadSheet->getSheet($i)->getCodeName()]['vmlDrawings'])) { - foreach ($this->spreadSheet->getUnparsedLoadedData()['sheets'][$this->spreadSheet->getSheet($i)->getCodeName()]['vmlDrawings'] as $vmlDrawing) { + if (isset($unparsedLoadedData['sheets'][$this->spreadSheet->getSheet($i)->getCodeName()]['vmlDrawings'])) { + foreach ($unparsedLoadedData['sheets'][$this->spreadSheet->getSheet($i)->getCodeName()]['vmlDrawings'] as $vmlDrawing) { $zip->addFromString($vmlDrawing['filePath'], $vmlDrawing['content']); } } diff --git a/src/PhpSpreadsheet/Writer/Xlsx/ContentTypes.php b/src/PhpSpreadsheet/Writer/Xlsx/ContentTypes.php index defff4c9..6b22d713 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/ContentTypes.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/ContentTypes.php @@ -89,12 +89,13 @@ class ContentTypes extends WriterPart $this->writeOverrideContentType($objWriter, '/xl/sharedStrings.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml'); // Add worksheet relationship content types + $unparsedLoadedData = $spreadsheet->getUnparsedLoadedData(); $chart = 1; for ($i = 0; $i < $sheetCount; ++$i) { $drawings = $spreadsheet->getSheet($i)->getDrawingCollection(); $drawingCount = count($drawings); $chartCount = ($includeCharts) ? $spreadsheet->getSheet($i)->getChartCount() : 0; - $hasUnparsedDrawing = isset($spreadsheet->getUnparsedLoadedData()['sheets'][$spreadsheet->getSheet($i)->getCodeName()]['drawingOriginalIds']); + $hasUnparsedDrawing = isset($unparsedLoadedData['sheets'][$spreadsheet->getSheet($i)->getCodeName()]['drawingOriginalIds']); // We need a drawing relationship for the worksheet if we have either drawings or charts if (($drawingCount > 0) || ($chartCount > 0) || $hasUnparsedDrawing) { @@ -163,15 +164,15 @@ class ContentTypes extends WriterPart } // unparsed defaults - if (isset($spreadsheet->getUnparsedLoadedData()['default_content_types'])) { - foreach ($spreadsheet->getUnparsedLoadedData()['default_content_types'] as $extName => $contentType) { + if (isset($unparsedLoadedData['default_content_types'])) { + foreach ($unparsedLoadedData['default_content_types'] as $extName => $contentType) { $this->writeDefaultContentType($objWriter, $extName, $contentType); } } // unparsed overrides - if (isset($spreadsheet->getUnparsedLoadedData()['override_content_types'])) { - foreach ($spreadsheet->getUnparsedLoadedData()['override_content_types'] as $partName => $overrideType) { + if (isset($unparsedLoadedData['override_content_types'])) { + foreach ($unparsedLoadedData['override_content_types'] as $partName => $overrideType) { $this->writeOverrideContentType($objWriter, $partName, $overrideType); } } diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Drawing.php b/src/PhpSpreadsheet/Writer/Xlsx/Drawing.php index ce5da3ea..533c7b3b 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Drawing.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Drawing.php @@ -60,8 +60,9 @@ class Drawing extends WriterPart } // unparsed AlternateContent - if (isset($pWorksheet->getParent()->getUnparsedLoadedData()['sheets'][$pWorksheet->getCodeName()]['drawingAlternateContents'])) { - foreach ($pWorksheet->getParent()->getUnparsedLoadedData()['sheets'][$pWorksheet->getCodeName()]['drawingAlternateContents'] as $drawingAlternateContent) { + $unparsedLoadedData = $pWorksheet->getParent()->getUnparsedLoadedData(); + if (isset($unparsedLoadedData['sheets'][$pWorksheet->getCodeName()]['drawingAlternateContents'])) { + foreach ($unparsedLoadedData['sheets'][$pWorksheet->getCodeName()]['drawingAlternateContents'] as $drawingAlternateContent) { $objWriter->writeRaw($drawingAlternateContent); } } diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Rels.php b/src/PhpSpreadsheet/Writer/Xlsx/Rels.php index 629f070e..e60ce5e7 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Rels.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Rels.php @@ -196,8 +196,9 @@ class Rels extends WriterPart // Write drawing relationships? $d = 0; $drawingOriginalIds = []; - if (isset($pWorksheet->getParent()->getUnparsedLoadedData()['sheets'][$pWorksheet->getCodeName()]['drawingOriginalIds'])) { - $drawingOriginalIds = $pWorksheet->getParent()->getUnparsedLoadedData()['sheets'][$pWorksheet->getCodeName()]['drawingOriginalIds']; + $unparsedLoadedData = $pWorksheet->getParent()->getUnparsedLoadedData(); + if (isset($unparsedLoadedData['sheets'][$pWorksheet->getCodeName()]['drawingOriginalIds'])) { + $drawingOriginalIds = $unparsedLoadedData['sheets'][$pWorksheet->getCodeName()]['drawingOriginalIds']; } if ($includeCharts) { @@ -278,11 +279,12 @@ class Rels extends WriterPart private function writeUnparsedRelationship(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet, XMLWriter $objWriter, $relationship, $type) { - if (!isset($pWorksheet->getParent()->getUnparsedLoadedData()['sheets'][$pWorksheet->getCodeName()][$relationship])) { + $unparsedLoadedData = $pWorksheet->getParent()->getUnparsedLoadedData(); + if (!isset($unparsedLoadedData['sheets'][$pWorksheet->getCodeName()][$relationship])) { return; } - foreach ($pWorksheet->getParent()->getUnparsedLoadedData()['sheets'][$pWorksheet->getCodeName()][$relationship] as $rId => $value) { + foreach ($unparsedLoadedData['sheets'][$pWorksheet->getCodeName()][$relationship] as $rId => $value) { $this->writeRelationship( $objWriter, $rId, diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index 7e2b4d62..9b8bb544 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -852,8 +852,9 @@ class Worksheet extends WriterPart $objWriter->writeAttribute('useFirstPageNumber', '1'); } - if (isset($pSheet->getParent()->getUnparsedLoadedData()['sheets'][$pSheet->getCodeName()]['pageSetupRelId'])) { - $objWriter->writeAttribute('r:id', $pSheet->getParent()->getUnparsedLoadedData()['sheets'][$pSheet->getCodeName()]['pageSetupRelId']); + $getUnparsedLoadedData = $pSheet->getParent()->getUnparsedLoadedData(); + if (isset($getUnparsedLoadedData['sheets'][$pSheet->getCodeName()]['pageSetupRelId'])) { + $objWriter->writeAttribute('r:id', $getUnparsedLoadedData['sheets'][$pSheet->getCodeName()]['pageSetupRelId']); } $objWriter->endElement(); @@ -1157,7 +1158,8 @@ class Worksheet extends WriterPart */ private function writeDrawings(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet, $includeCharts = false) { - $hasUnparsedDrawing = isset($pSheet->getParent()->getUnparsedLoadedData()['sheets'][$pSheet->getCodeName()]['drawingOriginalIds']); + $unparsedLoadedData = $pSheet->getParent()->getUnparsedLoadedData(); + $hasUnparsedDrawing = isset($unparsedLoadedData['sheets'][$pSheet->getCodeName()]['drawingOriginalIds']); $chartCount = ($includeCharts) ? $pSheet->getChartCollection()->count() : 0; if ($chartCount == 0 && $pSheet->getDrawingCollection()->count() == 0 && !$hasUnparsedDrawing) { return; @@ -1167,8 +1169,8 @@ class Worksheet extends WriterPart $objWriter->startElement('drawing'); $rId = 'rId1'; - if (isset($pSheet->getParent()->getUnparsedLoadedData()['sheets'][$pSheet->getCodeName()]['drawingOriginalIds'])) { - $drawingOriginalIds = $pSheet->getParent()->getUnparsedLoadedData()['sheets'][$pSheet->getCodeName()]['drawingOriginalIds']; + if (isset($unparsedLoadedData['sheets'][$pSheet->getCodeName()]['drawingOriginalIds'])) { + $drawingOriginalIds = $unparsedLoadedData['sheets'][$pSheet->getCodeName()]['drawingOriginalIds']; // take first. In future can be overriten $rId = reset($drawingOriginalIds); }