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