From 586b36b44014f34ac515003cba0ce2b58f6f999d Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 5 Jul 2020 18:11:14 +0200 Subject: [PATCH] Refactor Xml PageSettings into a separate loader class --- src/PhpSpreadsheet/Reader/Xml.php | 105 +-------------- .../Reader/Xml/PageSettings.php | 120 ++++++++++++++++++ 2 files changed, 124 insertions(+), 101 deletions(-) create mode 100644 src/PhpSpreadsheet/Reader/Xml/PageSettings.php diff --git a/src/PhpSpreadsheet/Reader/Xml.php b/src/PhpSpreadsheet/Reader/Xml.php index 768498c2..fc2f5108 100644 --- a/src/PhpSpreadsheet/Reader/Xml.php +++ b/src/PhpSpreadsheet/Reader/Xml.php @@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Document\Properties; use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; +use PhpOffice\PhpSpreadsheet\Reader\Xml\PageSettings; use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\Settings; use PhpOffice\PhpSpreadsheet\Shared\Date; @@ -15,9 +16,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Font; -use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; use SimpleXMLElement; -use stdClass; /** * Reader for SpreadsheetML, the XML schema for Microsoft Office Excel 2003. @@ -629,25 +628,9 @@ class Xml extends BaseReader ++$rowID; } - $xml_x = $worksheet->children($namespaces['x']); - if (isset($xml_x->WorksheetOptions)) { - $printSettings = $this->pageSetup($xml_x, $namespaces, $this->getPrintDefaults()); - $printSettings = $this->printSetup($xml_x, $printSettings); - - $spreadsheet->getActiveSheet()->getPageSetup() - ->setPaperSize($printSettings->paperSize) - ->setOrientation($printSettings->orientation) - ->setScale($printSettings->scale) - ->setVerticalCentered($printSettings->verticalCentered) - ->setHorizontalCentered($printSettings->horizontalCentered) - ->setPageOrder($printSettings->printOrder); - $spreadsheet->getActiveSheet()->getPageMargins() - ->setTop($printSettings->topMargin) - ->setHeader($printSettings->headerMargin) - ->setLeft($printSettings->leftMargin) - ->setRight($printSettings->rightMargin) - ->setBottom($printSettings->bottomMargin) - ->setFooter($printSettings->footerMargin); + $xmlX = $worksheet->children($namespaces['x']); + if (isset($xmlX->WorksheetOptions)) { + (new PageSettings($xmlX, $namespaces))->loadPageSettings($spreadsheet); } } ++$worksheetID; @@ -878,84 +861,4 @@ class Xml extends BaseReader } } } - - private function getPrintDefaults() - { - return (object) [ - 'paperSize' => 9, - 'orientation' => PageSetup::ORIENTATION_DEFAULT, - 'scale' => 100, - 'horizontalCentered' => false, - 'verticalCentered' => false, - 'printOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, - 'topMargin' => 0.75, - 'headerMargin' => 0.3, - 'leftMargin' => 0.7, - 'rightMargin' => 0.7, - 'bottomMargin' => 0.75, - 'footerMargin' => 0.3, - ]; - } - - private function pageSetup(SimpleXMLElement $xmlX, array $namespaces, stdClass $printDefaults): stdClass - { - if (isset($xmlX->WorksheetOptions->PageSetup)) { - foreach ($xmlX->WorksheetOptions->PageSetup as $pageSetupData) { - foreach ($pageSetupData as $pageSetupKey => $pageSetupValue) { - $pageSetupAttributes = $pageSetupValue->attributes($namespaces['x']); - switch ($pageSetupKey) { - case 'Layout': - $printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation) ?: PageSetup::ORIENTATION_PORTRAIT; - $printDefaults->horizontalCentered = (bool) $pageSetupAttributes->CenterHorizontal ?: false; - $printDefaults->verticalCentered = (bool) $pageSetupAttributes->CenterVertical ?: false; - - break; - case 'Header': - $printDefaults->headerMargin = (float) $pageSetupAttributes->Margin ?: 1.0; - - break; - case 'Footer': - $printDefaults->footerMargin = (float) $pageSetupAttributes->Margin ?: 1.0; - - break; - case 'PageMargins': - $printDefaults->leftMargin = (float) $pageSetupAttributes->Left ?: 1.0; - $printDefaults->rightMargin = (float) $pageSetupAttributes->Right ?: 1.0; - $printDefaults->topMargin = (float) $pageSetupAttributes->Top ?: 1.0; - $printDefaults->bottomMargin = (float) $pageSetupAttributes->Bottom ?: 1.0; - - break; - } - } - } - } - - return $printDefaults; - } - - private function printSetup(SimpleXMLElement $xmlX, stdClass $printDefaults): stdClass - { - if (isset($xmlX->WorksheetOptions->Print)) { - foreach ($xmlX->WorksheetOptions->Print as $printData) { - foreach ($printData as $printKey => $printValue) { - switch ($printKey) { - case 'LeftToRight': - $printDefaults->printOrder = PageSetup::PAGEORDER_OVER_THEN_DOWN; - - break; - case 'PaperSizeIndex': - $printDefaults->paperSize = (int) $printValue ?: 9; - - break; - case 'Scale': - $printDefaults->scale = (int) $printValue ?: 100; - - break; - } - } - } - } - - return $printDefaults; - } } diff --git a/src/PhpSpreadsheet/Reader/Xml/PageSettings.php b/src/PhpSpreadsheet/Reader/Xml/PageSettings.php new file mode 100644 index 00000000..17883661 --- /dev/null +++ b/src/PhpSpreadsheet/Reader/Xml/PageSettings.php @@ -0,0 +1,120 @@ +pageSetup($xmlX, $namespaces, $this->getPrintDefaults()); + $this->printSettings = $this->printSetup($xmlX, $printSettings); + } + + public function loadPageSettings(Spreadsheet $spreadsheet): void + { + $spreadsheet->getActiveSheet()->getPageSetup() + ->setPaperSize($this->printSettings->paperSize) + ->setOrientation($this->printSettings->orientation) + ->setScale($this->printSettings->scale) + ->setVerticalCentered($this->printSettings->verticalCentered) + ->setHorizontalCentered($this->printSettings->horizontalCentered) + ->setPageOrder($this->printSettings->printOrder); + $spreadsheet->getActiveSheet()->getPageMargins() + ->setTop($this->printSettings->topMargin) + ->setHeader($this->printSettings->headerMargin) + ->setLeft($this->printSettings->leftMargin) + ->setRight($this->printSettings->rightMargin) + ->setBottom($this->printSettings->bottomMargin) + ->setFooter($this->printSettings->footerMargin); + } + + private function getPrintDefaults() + { + return (object) [ + 'paperSize' => 9, + 'orientation' => PageSetup::ORIENTATION_DEFAULT, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => false, + 'printOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + 'topMargin' => 0.75, + 'headerMargin' => 0.3, + 'leftMargin' => 0.7, + 'rightMargin' => 0.7, + 'bottomMargin' => 0.75, + 'footerMargin' => 0.3, + ]; + } + + private function pageSetup(SimpleXMLElement $xmlX, array $namespaces, stdClass $printDefaults): stdClass + { + if (isset($xmlX->WorksheetOptions->PageSetup)) { + foreach ($xmlX->WorksheetOptions->PageSetup as $pageSetupData) { + foreach ($pageSetupData as $pageSetupKey => $pageSetupValue) { + $pageSetupAttributes = $pageSetupValue->attributes($namespaces['x']); + switch ($pageSetupKey) { + case 'Layout': + $printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation) ?: PageSetup::ORIENTATION_PORTRAIT; + $printDefaults->horizontalCentered = (bool) $pageSetupAttributes->CenterHorizontal ?: false; + $printDefaults->verticalCentered = (bool) $pageSetupAttributes->CenterVertical ?: false; + + break; + case 'Header': + $printDefaults->headerMargin = (float) $pageSetupAttributes->Margin ?: 1.0; + + break; + case 'Footer': + $printDefaults->footerMargin = (float) $pageSetupAttributes->Margin ?: 1.0; + + break; + case 'PageMargins': + $printDefaults->leftMargin = (float) $pageSetupAttributes->Left ?: 1.0; + $printDefaults->rightMargin = (float) $pageSetupAttributes->Right ?: 1.0; + $printDefaults->topMargin = (float) $pageSetupAttributes->Top ?: 1.0; + $printDefaults->bottomMargin = (float) $pageSetupAttributes->Bottom ?: 1.0; + + break; + } + } + } + } + + return $printDefaults; + } + + private function printSetup(SimpleXMLElement $xmlX, stdClass $printDefaults): stdClass + { + if (isset($xmlX->WorksheetOptions->Print)) { + foreach ($xmlX->WorksheetOptions->Print as $printData) { + foreach ($printData as $printKey => $printValue) { + switch ($printKey) { + case 'LeftToRight': + $printDefaults->printOrder = PageSetup::PAGEORDER_OVER_THEN_DOWN; + + break; + case 'PaperSizeIndex': + $printDefaults->paperSize = (int) $printValue ?: 9; + + break; + case 'Scale': + $printDefaults->scale = (int) $printValue ?: 100; + + break; + } + } + } + } + + return $printDefaults; + } +}