1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Reader\Ods;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Document\Properties as DocumentProperties;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
7 |
use SimpleXMLElement;
|
|
|
8 |
|
|
|
9 |
class Properties
|
|
|
10 |
{
|
|
|
11 |
/** @var Spreadsheet */
|
|
|
12 |
private $spreadsheet;
|
|
|
13 |
|
|
|
14 |
public function __construct(Spreadsheet $spreadsheet)
|
|
|
15 |
{
|
|
|
16 |
$this->spreadsheet = $spreadsheet;
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
public function load(SimpleXMLElement $xml, array $namespacesMeta): void
|
|
|
20 |
{
|
|
|
21 |
$docProps = $this->spreadsheet->getProperties();
|
|
|
22 |
$officeProperty = $xml->children($namespacesMeta['office']);
|
|
|
23 |
foreach ($officeProperty as $officePropertyData) {
|
|
|
24 |
if (isset($namespacesMeta['dc'])) {
|
|
|
25 |
/** @scrutinizer ignore-call */
|
|
|
26 |
$officePropertiesDC = $officePropertyData->children($namespacesMeta['dc']);
|
|
|
27 |
$this->setCoreProperties($docProps, $officePropertiesDC);
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
$officePropertyMeta = null;
|
|
|
31 |
if (isset($namespacesMeta['dc'])) {
|
|
|
32 |
/** @scrutinizer ignore-call */
|
|
|
33 |
$officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
|
|
|
34 |
}
|
|
|
35 |
$officePropertyMeta = $officePropertyMeta ?? [];
|
|
|
36 |
foreach ($officePropertyMeta as $propertyName => $propertyValue) {
|
|
|
37 |
$this->setMetaProperties($namespacesMeta, $propertyValue, $propertyName, $docProps);
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
private function setCoreProperties(DocumentProperties $docProps, SimpleXMLElement $officePropertyDC): void
|
|
|
43 |
{
|
|
|
44 |
foreach ($officePropertyDC as $propertyName => $propertyValue) {
|
|
|
45 |
$propertyValue = (string) $propertyValue;
|
|
|
46 |
switch ($propertyName) {
|
|
|
47 |
case 'title':
|
|
|
48 |
$docProps->setTitle($propertyValue);
|
|
|
49 |
|
|
|
50 |
break;
|
|
|
51 |
case 'subject':
|
|
|
52 |
$docProps->setSubject($propertyValue);
|
|
|
53 |
|
|
|
54 |
break;
|
|
|
55 |
case 'creator':
|
|
|
56 |
$docProps->setCreator($propertyValue);
|
|
|
57 |
$docProps->setLastModifiedBy($propertyValue);
|
|
|
58 |
|
|
|
59 |
break;
|
|
|
60 |
case 'date':
|
|
|
61 |
$docProps->setModified($propertyValue);
|
|
|
62 |
|
|
|
63 |
break;
|
|
|
64 |
case 'description':
|
|
|
65 |
$docProps->setDescription($propertyValue);
|
|
|
66 |
|
|
|
67 |
break;
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function setMetaProperties(
|
|
|
73 |
array $namespacesMeta,
|
|
|
74 |
SimpleXMLElement $propertyValue,
|
|
|
75 |
string $propertyName,
|
|
|
76 |
DocumentProperties $docProps
|
|
|
77 |
): void {
|
|
|
78 |
$propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
|
|
|
79 |
$propertyValue = (string) $propertyValue;
|
|
|
80 |
switch ($propertyName) {
|
|
|
81 |
case 'initial-creator':
|
|
|
82 |
$docProps->setCreator($propertyValue);
|
|
|
83 |
|
|
|
84 |
break;
|
|
|
85 |
case 'keyword':
|
|
|
86 |
$docProps->setKeywords($propertyValue);
|
|
|
87 |
|
|
|
88 |
break;
|
|
|
89 |
case 'creation-date':
|
|
|
90 |
$docProps->setCreated($propertyValue);
|
|
|
91 |
|
|
|
92 |
break;
|
|
|
93 |
case 'user-defined':
|
|
|
94 |
$this->setUserDefinedProperty($propertyValueAttributes, $propertyValue, $docProps);
|
|
|
95 |
|
|
|
96 |
break;
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* @param mixed $propertyValueAttributes
|
|
|
102 |
* @param mixed $propertyValue
|
|
|
103 |
*/
|
|
|
104 |
private function setUserDefinedProperty($propertyValueAttributes, $propertyValue, DocumentProperties $docProps): void
|
|
|
105 |
{
|
|
|
106 |
$propertyValueName = '';
|
|
|
107 |
$propertyValueType = DocumentProperties::PROPERTY_TYPE_STRING;
|
|
|
108 |
foreach ($propertyValueAttributes as $key => $value) {
|
|
|
109 |
if ($key == 'name') {
|
|
|
110 |
$propertyValueName = (string) $value;
|
|
|
111 |
} elseif ($key == 'value-type') {
|
|
|
112 |
switch ($value) {
|
|
|
113 |
case 'date':
|
|
|
114 |
$propertyValue = DocumentProperties::convertProperty($propertyValue, 'date');
|
|
|
115 |
$propertyValueType = DocumentProperties::PROPERTY_TYPE_DATE;
|
|
|
116 |
|
|
|
117 |
break;
|
|
|
118 |
case 'boolean':
|
|
|
119 |
$propertyValue = DocumentProperties::convertProperty($propertyValue, 'bool');
|
|
|
120 |
$propertyValueType = DocumentProperties::PROPERTY_TYPE_BOOLEAN;
|
|
|
121 |
|
|
|
122 |
break;
|
|
|
123 |
case 'float':
|
|
|
124 |
$propertyValue = DocumentProperties::convertProperty($propertyValue, 'r4');
|
|
|
125 |
$propertyValueType = DocumentProperties::PROPERTY_TYPE_FLOAT;
|
|
|
126 |
|
|
|
127 |
break;
|
|
|
128 |
default:
|
|
|
129 |
$propertyValueType = DocumentProperties::PROPERTY_TYPE_STRING;
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
$docProps->setCustomProperty($propertyValueName, $propertyValue, $propertyValueType);
|
|
|
135 |
}
|
|
|
136 |
}
|