From 6cbb622a9e81b12a94ba0f494f478f1037a9a252 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 5 Jul 2020 18:25:39 +0200 Subject: [PATCH] Minor refactoring --- .../Reader/Xml/PageSettings.php | 24 +++++++++++++------ .../Reader/Ods/PageSetupTest.php | 2 +- .../Reader/Xls/PageSetupTest.php | 2 +- .../Reader/Xlsx/PageSetupTest.php | 2 +- .../Reader/Xml/PageSetupTest.php | 2 +- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/PhpSpreadsheet/Reader/Xml/PageSettings.php b/src/PhpSpreadsheet/Reader/Xml/PageSettings.php index 17883661..d07e55ae 100644 --- a/src/PhpSpreadsheet/Reader/Xml/PageSettings.php +++ b/src/PhpSpreadsheet/Reader/Xml/PageSettings.php @@ -64,9 +64,7 @@ class PageSettings $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; + $this->setLayout($printDefaults, $pageSetupAttributes); break; case 'Header': @@ -78,10 +76,7 @@ class PageSettings 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; + $this->setMargins($printDefaults, $pageSetupAttributes); break; } @@ -117,4 +112,19 @@ class PageSettings return $printDefaults; } + + private function setLayout(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void + { + $printDefaults->orientation = (string)strtolower($pageSetupAttributes->Orientation) ?: PageSetup::ORIENTATION_PORTRAIT; + $printDefaults->horizontalCentered = (bool)$pageSetupAttributes->CenterHorizontal ?: false; + $printDefaults->verticalCentered = (bool)$pageSetupAttributes->CenterVertical ?: false; + } + + private function setMargins(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void + { + $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; + } } diff --git a/tests/PhpSpreadsheetTests/Reader/Ods/PageSetupTest.php b/tests/PhpSpreadsheetTests/Reader/Ods/PageSetupTest.php index e17c5da4..a8e681d4 100644 --- a/tests/PhpSpreadsheetTests/Reader/Ods/PageSetupTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Ods/PageSetupTest.php @@ -11,7 +11,7 @@ class PageSetupTest extends TestCase { private const MARGIN_PRECISION = 0.00000001; - private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm + private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm /** * @var Spreadsheet diff --git a/tests/PhpSpreadsheetTests/Reader/Xls/PageSetupTest.php b/tests/PhpSpreadsheetTests/Reader/Xls/PageSetupTest.php index 5947d1b5..a5282405 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xls/PageSetupTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xls/PageSetupTest.php @@ -11,7 +11,7 @@ class PageSetupTest extends TestCase { private const MARGIN_PRECISION = 0.00000001; - private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm + private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm /** * @var Spreadsheet diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/PageSetupTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/PageSetupTest.php index cd1bd174..3efb581a 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xlsx/PageSetupTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/PageSetupTest.php @@ -11,7 +11,7 @@ class PageSetupTest extends TestCase { private const MARGIN_PRECISION = 0.00000001; - private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm + private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm /** * @var Spreadsheet diff --git a/tests/PhpSpreadsheetTests/Reader/Xml/PageSetupTest.php b/tests/PhpSpreadsheetTests/Reader/Xml/PageSetupTest.php index 92100b04..d6313281 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xml/PageSetupTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xml/PageSetupTest.php @@ -11,7 +11,7 @@ class PageSetupTest extends TestCase { private const MARGIN_PRECISION = 0.000001; - private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm + private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm /** * @var Spreadsheet