Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
    private Spreadsheet $spreadsheet;
12
 
13
    public function __construct(Spreadsheet $spreadsheet)
14
    {
15
        $this->spreadsheet = $spreadsheet;
16
    }
17
 
18
    public function load(SimpleXMLElement $xml, array $namespacesMeta): void
19
    {
20
        $docProps = $this->spreadsheet->getProperties();
21
        $officeProperty = $xml->children($namespacesMeta['office']);
22
        foreach ($officeProperty as $officePropertyData) {
23
            if (isset($namespacesMeta['dc'])) {
24
                $officePropertiesDC = $officePropertyData->children($namespacesMeta['dc']);
25
                $this->setCoreProperties($docProps, $officePropertiesDC);
26
            }
27
 
28
            $officePropertyMeta = null;
29
            if (isset($namespacesMeta['dc'])) {
30
                $officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
31
            }
32
            $officePropertyMeta = $officePropertyMeta ?? [];
33
            foreach ($officePropertyMeta as $propertyName => $propertyValue) {
34
                $this->setMetaProperties($namespacesMeta, $propertyValue, $propertyName, $docProps);
35
            }
36
        }
37
    }
38
 
39
    private function setCoreProperties(DocumentProperties $docProps, SimpleXMLElement $officePropertyDC): void
40
    {
41
        foreach ($officePropertyDC as $propertyName => $propertyValue) {
42
            $propertyValue = (string) $propertyValue;
43
            switch ($propertyName) {
44
                case 'title':
45
                    $docProps->setTitle($propertyValue);
46
 
47
                    break;
48
                case 'subject':
49
                    $docProps->setSubject($propertyValue);
50
 
51
                    break;
52
                case 'creator':
53
                    $docProps->setCreator($propertyValue);
54
                    $docProps->setLastModifiedBy($propertyValue);
55
 
56
                    break;
57
                case 'date':
58
                    $docProps->setModified($propertyValue);
59
 
60
                    break;
61
                case 'description':
62
                    $docProps->setDescription($propertyValue);
63
 
64
                    break;
65
            }
66
        }
67
    }
68
 
69
    private function setMetaProperties(
70
        array $namespacesMeta,
71
        SimpleXMLElement $propertyValue,
72
        string $propertyName,
73
        DocumentProperties $docProps
74
    ): void {
75
        $propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
76
        $propertyValue = (string) $propertyValue;
77
        switch ($propertyName) {
78
            case 'initial-creator':
79
                $docProps->setCreator($propertyValue);
80
 
81
                break;
82
            case 'keyword':
83
                $docProps->setKeywords($propertyValue);
84
 
85
                break;
86
            case 'creation-date':
87
                $docProps->setCreated($propertyValue);
88
 
89
                break;
90
            case 'user-defined':
91
                $name2 = (string) ($propertyValueAttributes['name'] ?? '');
92
                if ($name2 === 'Company') {
93
                    $docProps->setCompany($propertyValue);
94
                } elseif ($name2 === 'category') {
95
                    $docProps->setCategory($propertyValue);
96
                } else {
97
                    $this->setUserDefinedProperty($propertyValueAttributes, $propertyValue, $docProps);
98
                }
99
 
100
                break;
101
        }
102
    }
103
 
104
    private function setUserDefinedProperty(iterable $propertyValueAttributes, string $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
}