PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Reader / Xml / Properties.php

Properties.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Reader/Xml/Properties.php

159 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Xml;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Document\Properties as DocumentProperties;
6 use TablePress\PhpOffice\PhpSpreadsheet\Spreadsheet;
7 use SimpleXMLElement;
8
9 class Properties
10 {
11 protected Spreadsheet $spreadsheet;
12
13 public function __construct(Spreadsheet $spreadsheet)
14 {
15 $this->spreadsheet = $spreadsheet;
16 }
17
18 /** @param string[] $namespaces */
19 public function readProperties(SimpleXMLElement $xml, array $namespaces): void
20 {
21 $this->readStandardProperties($xml);
22 $this->readCustomProperties($xml, $namespaces);
23 }
24
25 protected function readStandardProperties(SimpleXMLElement $xml): void
26 {
27 if (isset($xml->DocumentProperties[0])) {
28 $docProps = $this->spreadsheet->getProperties();
29
30 foreach ($xml->DocumentProperties[0] as $propertyName => $propertyValue) {
31 $propertyValue = (string) $propertyValue;
32
33 $this->processStandardProperty($docProps, $propertyName, $propertyValue);
34 }
35 }
36 }
37
38 /** @param string[] $namespaces */
39 protected function readCustomProperties(SimpleXMLElement $xml, array $namespaces): void
40 {
41 if (isset($xml->CustomDocumentProperties) && is_iterable($xml->CustomDocumentProperties[0])) {
42 $docProps = $this->spreadsheet->getProperties();
43
44 foreach ($xml->CustomDocumentProperties[0] as $propertyName => $propertyValue) {
45 $propertyAttributes = self::getAttributes($propertyValue, $namespaces['dt']);
46 $propertyName = (string) preg_replace_callback('/_x([0-9a-f]{4})_/i', [$this, 'hex2str'], $propertyName);
47
48 $this->processCustomProperty($docProps, $propertyName, $propertyValue, $propertyAttributes);
49 }
50 }
51 }
52
53 protected function processStandardProperty(
54 DocumentProperties $docProps,
55 string $propertyName,
56 string $stringValue
57 ): void {
58 switch ($propertyName) {
59 case 'Title':
60 $docProps->setTitle($stringValue);
61
62 break;
63 case 'Subject':
64 $docProps->setSubject($stringValue);
65
66 break;
67 case 'Author':
68 $docProps->setCreator($stringValue);
69
70 break;
71 case 'Created':
72 $docProps->setCreated($stringValue);
73
74 break;
75 case 'LastAuthor':
76 $docProps->setLastModifiedBy($stringValue);
77
78 break;
79 case 'LastSaved':
80 $docProps->setModified($stringValue);
81
82 break;
83 case 'Company':
84 $docProps->setCompany($stringValue);
85
86 break;
87 case 'Category':
88 $docProps->setCategory($stringValue);
89
90 break;
91 case 'Manager':
92 $docProps->setManager($stringValue);
93
94 break;
95 case 'HyperlinkBase':
96 $docProps->setHyperlinkBase($stringValue);
97
98 break;
99 case 'Keywords':
100 $docProps->setKeywords($stringValue);
101
102 break;
103 case 'Description':
104 $docProps->setDescription($stringValue);
105
106 break;
107 }
108 }
109
110 protected function processCustomProperty(
111 DocumentProperties $docProps,
112 string $propertyName,
113 ?SimpleXMLElement $propertyValue,
114 SimpleXMLElement $propertyAttributes
115 ): void {
116 switch ((string) $propertyAttributes) {
117 case 'boolean':
118 $propertyType = DocumentProperties::PROPERTY_TYPE_BOOLEAN;
119 $propertyValue = (bool) (string) $propertyValue;
120
121 break;
122 case 'integer':
123 $propertyType = DocumentProperties::PROPERTY_TYPE_INTEGER;
124 $propertyValue = (int) $propertyValue;
125
126 break;
127 case 'float':
128 $propertyType = DocumentProperties::PROPERTY_TYPE_FLOAT;
129 $propertyValue = (float) $propertyValue;
130
131 break;
132 case 'dateTime.tz':
133 case 'dateTime.iso8601tz':
134 $propertyType = DocumentProperties::PROPERTY_TYPE_DATE;
135 $propertyValue = trim((string) $propertyValue);
136
137 break;
138 default:
139 $propertyType = DocumentProperties::PROPERTY_TYPE_STRING;
140 $propertyValue = trim((string) $propertyValue);
141
142 break;
143 }
144
145 $docProps->setCustomProperty($propertyName, $propertyValue, $propertyType);
146 }
147
148 /** @param string[] $hex */
149 protected function hex2str(array $hex): string
150 {
151 return mb_chr((int) hexdec($hex[1]), 'UTF-8');
152 }
153
154 private static function getAttributes(?SimpleXMLElement $simple, string $node): SimpleXMLElement
155 {
156 return ($simple === null) ? new SimpleXMLElement('<xml></xml>') : ($simple->attributes($node) ?? new SimpleXMLElement('<xml></xml>'));
157 }
158 }
159