From 4dadf4a5c8d816e2f9ff04d9f803b5b1ad18f680 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Tue, 30 Jun 2020 22:23:33 +0200 Subject: [PATCH] Refactor reading Ods Page Settings into a separate class --- src/PhpSpreadsheet/Reader/Ods.php | 69 +----------- .../Reader/Ods/PageSettings.php | 104 ++++++++++++++++++ 2 files changed, 108 insertions(+), 65 deletions(-) create mode 100644 src/PhpSpreadsheet/Reader/Ods/PageSettings.php diff --git a/src/PhpSpreadsheet/Reader/Ods.php b/src/PhpSpreadsheet/Reader/Ods.php index 040bfffa..ad87192f 100644 --- a/src/PhpSpreadsheet/Reader/Ods.php +++ b/src/PhpSpreadsheet/Reader/Ods.php @@ -11,6 +11,7 @@ use DOMNode; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; +use PhpOffice\PhpSpreadsheet\Reader\Ods\PageSettings; use PhpOffice\PhpSpreadsheet\Reader\Ods\Properties as DocumentProperties; use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; use PhpOffice\PhpSpreadsheet\RichText\RichText; @@ -19,8 +20,6 @@ use PhpOffice\PhpSpreadsheet\Shared\Date; use PhpOffice\PhpSpreadsheet\Shared\File; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; -use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; -use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use XMLReader; use ZipArchive; @@ -291,36 +290,8 @@ class Ods extends BaseReader $this->securityScanner->scan($zip->getFromName('styles.xml')), Settings::getLibXmlLoaderOptions() ); - $officeNs = $dom->lookupNamespaceUri('office'); - $stylesNs = $dom->lookupNamespaceUri('style'); - $styles = $dom->getElementsByTagNameNS($officeNs, 'automatic-styles') - ->item(0) - ->getElementsByTagNameNS($stylesNs, 'page-layout'); - - foreach ($styles as $styleSet) { - $styleName = $styleSet->getAttributeNS($stylesNs, 'name'); - $pageLayoutProperties = $styleSet->getElementsByTagNameNS($stylesNs, 'page-layout-properties')[0]; - $styleOrientation = $pageLayoutProperties->getAttributeNS($stylesNs, 'print-orientation'); - $styleScale = $pageLayoutProperties->getAttributeNS($stylesNs, 'scale-to'); - $stylePrintOrder = $pageLayoutProperties->getAttributeNS($stylesNs, 'print-page-order'); - - $this->pageLayoutStyles[$styleName] = (object) [ - 'orientation' => $styleOrientation, - 'scale' => $styleScale, - 'printOrder' => $stylePrintOrder, - ]; - } - - $styleMasterLookup = $dom->getElementsByTagNameNS($officeNs, 'master-styles') - ->item(0) - ->getElementsByTagNameNS($stylesNs, 'master-page'); - - foreach ($styleMasterLookup as $styleMasterSet) { - $styleMasterName = $styleMasterSet->getAttributeNS($stylesNs, 'name'); - $pageLayoutName = $styleMasterSet->getAttributeNS($stylesNs, 'page-layout-name'); - $this->masterPrintStylesCrossReference[$styleMasterName] = $pageLayoutName; - } + $pageSettings = new PageSettings($dom); // Main Content @@ -335,17 +306,7 @@ class Ods extends BaseReader $textNs = $dom->lookupNamespaceUri('text'); $xlinkNs = $dom->lookupNamespaceUri('xlink'); - $styleXReferences = $dom->getElementsByTagNameNS($officeNs, 'automatic-styles') - ->item(0) - ->getElementsByTagNameNS($stylesNs, 'style'); - - foreach ($styleXReferences as $styleXreferenceSet) { - $styleXRefName = $styleXreferenceSet->getAttributeNS($stylesNs, 'name'); - $stylePageLayoutName = $styleXreferenceSet->getAttributeNS($stylesNs, 'master-page-name'); - if (!empty($stylePageLayoutName)) { - $this->masterStylesCrossReference[$styleXRefName] = $stylePageLayoutName; - } - } + $pageSettings->readStyleCrossReferences($dom); // Content @@ -703,7 +664,7 @@ class Ods extends BaseReader break; } } - $this->getPrintSettings($spreadsheet->getActiveSheet(), $worksheetStyleName); + $pageSettings->setPrintSettingsForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName); ++$worksheetID; } } @@ -712,28 +673,6 @@ class Ods extends BaseReader return $spreadsheet; } - private function getPrintSettings(Worksheet $worksheet, string $styleName): void - { - if (!array_key_exists($styleName, $this->masterStylesCrossReference)) { - return; - } - $masterStyleName = $this->masterStylesCrossReference[$styleName]; - - if (!array_key_exists($masterStyleName, $this->masterPrintStylesCrossReference)) { - return; - } - $printSettingsIndex = $this->masterPrintStylesCrossReference[$masterStyleName]; - - if (!array_key_exists($printSettingsIndex, $this->pageLayoutStyles)) { - return; - } - $printSettings = $this->pageLayoutStyles[$printSettingsIndex]; - - $worksheet->getPageSetup()->setOrientation($printSettings->orientation ?? PageSetup::ORIENTATION_DEFAULT); - $worksheet->getPageSetup()->setScale((int) trim($printSettings->scale, '%')); - $worksheet->getPageSetup()->setPageOrder($printSettings->printOrder === 'ltr' ? PageSetup::PAGEORDER_OVER_THEN_DOWN : PageSetup::PAGEORDER_DOWN_THEN_OVER); - } - /** * Recursively scan element. * diff --git a/src/PhpSpreadsheet/Reader/Ods/PageSettings.php b/src/PhpSpreadsheet/Reader/Ods/PageSettings.php new file mode 100644 index 00000000..5ff1d58b --- /dev/null +++ b/src/PhpSpreadsheet/Reader/Ods/PageSettings.php @@ -0,0 +1,104 @@ +setDomNameSpaces($styleDom); + $this->readPageSettingStyles($styleDom); + $this->readStyleMasterLookup($styleDom); + } + + private function setDomNameSpaces(DOMDocument $styleDom): void + { + $this->officeNs = $styleDom->lookupNamespaceUri('office'); + $this->stylesNs = $styleDom->lookupNamespaceUri('style'); + } + + private function readPageSettingStyles(DOMDocument $styleDom): void + { + $styles = $styleDom->getElementsByTagNameNS($this->officeNs, 'automatic-styles') + ->item(0) + ->getElementsByTagNameNS($this->stylesNs, 'page-layout'); + + foreach ($styles as $styleSet) { + $styleName = $styleSet->getAttributeNS($this->stylesNs, 'name'); + $pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')[0]; + $styleOrientation = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-orientation'); + $styleScale = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'scale-to'); + $stylePrintOrder = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-page-order'); + + $this->pageLayoutStyles[$styleName] = (object) [ + 'orientation' => $styleOrientation, + 'scale' => $styleScale, + 'printOrder' => $stylePrintOrder, + ]; + } + } + + private function readStyleMasterLookup(DOMDocument $styleDom): void + { + $styleMasterLookup = $styleDom->getElementsByTagNameNS($this->officeNs, 'master-styles') + ->item(0) + ->getElementsByTagNameNS($this->stylesNs, 'master-page'); + + foreach ($styleMasterLookup as $styleMasterSet) { + $styleMasterName = $styleMasterSet->getAttributeNS($this->stylesNs, 'name'); + $pageLayoutName = $styleMasterSet->getAttributeNS($this->stylesNs, 'page-layout-name'); + $this->masterPrintStylesCrossReference[$styleMasterName] = $pageLayoutName; + } + } + + public function readStyleCrossReferences(DOMDocument $contentDom): void + { + $styleXReferences = $contentDom->getElementsByTagNameNS($this->officeNs, 'automatic-styles') + ->item(0) + ->getElementsByTagNameNS($this->stylesNs, 'style'); + + foreach ($styleXReferences as $styleXreferenceSet) { + $styleXRefName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'name'); + $stylePageLayoutName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'master-page-name'); + if (!empty($stylePageLayoutName)) { + $this->masterStylesCrossReference[$styleXRefName] = $stylePageLayoutName; + } + } + } + + public function setPrintSettingsForWorksheet(Worksheet $worksheet, string $styleName): void + { + if (!array_key_exists($styleName, $this->masterStylesCrossReference)) { + return; + } + $masterStyleName = $this->masterStylesCrossReference[$styleName]; + + if (!array_key_exists($masterStyleName, $this->masterPrintStylesCrossReference)) { + return; + } + $printSettingsIndex = $this->masterPrintStylesCrossReference[$masterStyleName]; + + if (!array_key_exists($printSettingsIndex, $this->pageLayoutStyles)) { + return; + } + $printSettings = $this->pageLayoutStyles[$printSettingsIndex]; + + $worksheet->getPageSetup() + ->setOrientation($printSettings->orientation ?? PageSetup::ORIENTATION_DEFAULT) + ->setPageOrder($printSettings->printOrder === 'ltr' ? PageSetup::PAGEORDER_OVER_THEN_DOWN : PageSetup::PAGEORDER_DOWN_THEN_OVER) + ->setScale((int) trim($printSettings->scale, '%')); + } +}