| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Xlsx; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 6 |
use SimpleXMLElement; |
| 7 |
|
| 8 |
class WorkbookView |
| 9 |
{ |
| 10 |
private Spreadsheet $spreadsheet; |
| 11 |
|
| 12 |
public function __construct(Spreadsheet $spreadsheet) |
| 13 |
{ |
| 14 |
$this->spreadsheet = $spreadsheet; |
| 15 |
} |
| 16 |
|
| 17 |
/** @param array<int, ?int> $mapSheetId */ |
| 18 |
public function viewSettings(SimpleXMLElement $xmlWorkbook, string $mainNS, array $mapSheetId, bool $readDataOnly): void |
| 19 |
{ |
| 20 |
// Default active sheet index to the first loaded worksheet from the file |
| 21 |
$this->spreadsheet->setActiveSheetIndex(0); |
| 22 |
|
| 23 |
$workbookView = $xmlWorkbook->children($mainNS)->bookViews->workbookView; |
| 24 |
if ($readDataOnly !== true && !empty($workbookView)) { |
| 25 |
$workbookViewAttributes = self::testSimpleXml(self::getAttributes($workbookView)); |
| 26 |
// active sheet index |
| 27 |
$activeTab = (int) $workbookViewAttributes->activeTab; // refers to old sheet index |
| 28 |
// keep active sheet index if sheet is still loaded, else first sheet is set as the active worksheet |
| 29 |
if (isset($mapSheetId[$activeTab])) { |
| 30 |
$this->spreadsheet->setActiveSheetIndex($mapSheetId[$activeTab]); |
| 31 |
} |
| 32 |
|
| 33 |
$this->horizontalScroll($workbookViewAttributes); |
| 34 |
$this->verticalScroll($workbookViewAttributes); |
| 35 |
$this->sheetTabs($workbookViewAttributes); |
| 36 |
$this->minimized($workbookViewAttributes); |
| 37 |
$this->autoFilterDateGrouping($workbookViewAttributes); |
| 38 |
$this->firstSheet($workbookViewAttributes); |
| 39 |
$this->visibility($workbookViewAttributes); |
| 40 |
$this->tabRatio($workbookViewAttributes); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @param mixed $value |
| 46 |
*/ |
| 47 |
public static function testSimpleXml($value): SimpleXMLElement |
| 48 |
{ |
| 49 |
return ($value instanceof SimpleXMLElement) |
| 50 |
? $value |
| 51 |
: new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>'); |
| 52 |
} |
| 53 |
|
| 54 |
public static function getAttributes(?SimpleXMLElement $value, string $ns = ''): SimpleXMLElement |
| 55 |
{ |
| 56 |
return self::testSimpleXml($value === null ? $value : $value->attributes($ns)); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Convert an 'xsd:boolean' XML value to a PHP boolean value. |
| 61 |
* A valid 'xsd:boolean' XML value can be one of the following |
| 62 |
* four values: 'true', 'false', '1', '0'. It is case-sensitive. |
| 63 |
* |
| 64 |
* Note that just doing '(bool) $xsdBoolean' is not safe, |
| 65 |
* since '(bool) "false"' returns true. |
| 66 |
* |
| 67 |
* @see https://www.w3.org/TR/xmlschema11-2/#boolean |
| 68 |
* |
| 69 |
* @param string $xsdBoolean An XML string value of type 'xsd:boolean' |
| 70 |
* |
| 71 |
* @return bool Boolean value |
| 72 |
*/ |
| 73 |
private function castXsdBooleanToBool(string $xsdBoolean): bool |
| 74 |
{ |
| 75 |
if ($xsdBoolean === 'false') { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
return (bool) $xsdBoolean; |
| 80 |
} |
| 81 |
|
| 82 |
private function horizontalScroll(SimpleXMLElement $workbookViewAttributes): void |
| 83 |
{ |
| 84 |
if (isset($workbookViewAttributes->showHorizontalScroll)) { |
| 85 |
$showHorizontalScroll = (string) $workbookViewAttributes->showHorizontalScroll; |
| 86 |
$this->spreadsheet->setShowHorizontalScroll($this->castXsdBooleanToBool($showHorizontalScroll)); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private function verticalScroll(SimpleXMLElement $workbookViewAttributes): void |
| 91 |
{ |
| 92 |
if (isset($workbookViewAttributes->showVerticalScroll)) { |
| 93 |
$showVerticalScroll = (string) $workbookViewAttributes->showVerticalScroll; |
| 94 |
$this->spreadsheet->setShowVerticalScroll($this->castXsdBooleanToBool($showVerticalScroll)); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private function sheetTabs(SimpleXMLElement $workbookViewAttributes): void |
| 99 |
{ |
| 100 |
if (isset($workbookViewAttributes->showSheetTabs)) { |
| 101 |
$showSheetTabs = (string) $workbookViewAttributes->showSheetTabs; |
| 102 |
$this->spreadsheet->setShowSheetTabs($this->castXsdBooleanToBool($showSheetTabs)); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private function minimized(SimpleXMLElement $workbookViewAttributes): void |
| 107 |
{ |
| 108 |
if (isset($workbookViewAttributes->minimized)) { |
| 109 |
$minimized = (string) $workbookViewAttributes->minimized; |
| 110 |
$this->spreadsheet->setMinimized($this->castXsdBooleanToBool($minimized)); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
private function autoFilterDateGrouping(SimpleXMLElement $workbookViewAttributes): void |
| 115 |
{ |
| 116 |
if (isset($workbookViewAttributes->autoFilterDateGrouping)) { |
| 117 |
$autoFilterDateGrouping = (string) $workbookViewAttributes->autoFilterDateGrouping; |
| 118 |
$this->spreadsheet->setAutoFilterDateGrouping($this->castXsdBooleanToBool($autoFilterDateGrouping)); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
private function firstSheet(SimpleXMLElement $workbookViewAttributes): void |
| 123 |
{ |
| 124 |
if (isset($workbookViewAttributes->firstSheet)) { |
| 125 |
$firstSheet = (string) $workbookViewAttributes->firstSheet; |
| 126 |
$this->spreadsheet->setFirstSheetIndex((int) $firstSheet); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
private function visibility(SimpleXMLElement $workbookViewAttributes): void |
| 131 |
{ |
| 132 |
if (isset($workbookViewAttributes->visibility)) { |
| 133 |
$visibility = (string) $workbookViewAttributes->visibility; |
| 134 |
$this->spreadsheet->setVisibility($visibility); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
private function tabRatio(SimpleXMLElement $workbookViewAttributes): void |
| 139 |
{ |
| 140 |
if (isset($workbookViewAttributes->tabRatio)) { |
| 141 |
$tabRatio = (string) $workbookViewAttributes->tabRatio; |
| 142 |
$this->spreadsheet->setTabRatio((int) $tabRatio); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|