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