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\Writer\Xlsx;
4
 
5
use PhpOffice\PhpSpreadsheet\Cell\Cell;
6
use PhpOffice\PhpSpreadsheet\Cell\DataType;
7
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
8
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces;
9
use PhpOffice\PhpSpreadsheet\RichText\RichText;
10
use PhpOffice\PhpSpreadsheet\RichText\Run;
11
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
12
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
13
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet as ActualWorksheet;
14
 
15
class StringTable extends WriterPart
16
{
17
    /**
18
     * Create worksheet stringtable.
19
     *
20
     * @param string[] $existingTable Existing table to eventually merge with
21
     *
22
     * @return string[] String table for worksheet
23
     */
24
    public function createStringTable(ActualWorksheet $worksheet, ?array $existingTable = null): array
25
    {
26
        // Create string lookup table
27
        $aStringTable = $existingTable ?? [];
28
 
29
        // Fill index array
30
        $aFlippedStringTable = $this->flipStringTable($aStringTable);
31
 
32
        // Loop through cells
33
        foreach ($worksheet->getCellCollection()->getCoordinates() as $coordinate) {
34
            /** @var Cell $cell */
35
            $cell = $worksheet->getCellCollection()->get($coordinate);
36
            $cellValue = $cell->getValue();
37
            if (
38
                !is_object($cellValue)
39
                && ($cellValue !== null)
40
                && $cellValue !== ''
41
                && ($cell->getDataType() == DataType::TYPE_STRING || $cell->getDataType() == DataType::TYPE_STRING2 || $cell->getDataType() == DataType::TYPE_NULL)
42
                && !isset($aFlippedStringTable[$cellValue])
43
            ) {
44
                $aStringTable[] = $cellValue;
45
                $aFlippedStringTable[$cellValue] = true;
46
            } elseif (
47
                $cellValue instanceof RichText
48
                && !isset($aFlippedStringTable[$cellValue->getHashCode()])
49
            ) {
50
                $aStringTable[] = $cellValue;
51
                $aFlippedStringTable[$cellValue->getHashCode()] = true;
52
            }
53
        }
54
 
55
        return $aStringTable;
56
    }
57
 
58
    /**
59
     * Write string table to XML format.
60
     *
61
     * @param (RichText|string)[] $stringTable
62
     *
63
     * @return string XML Output
64
     */
65
    public function writeStringTable(array $stringTable): string
66
    {
67
        // Create XML writer
68
        $objWriter = null;
69
        if ($this->getParentWriter()->getUseDiskCaching()) {
70
            $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
71
        } else {
72
            $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
73
        }
74
 
75
        // XML header
76
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
77
 
78
        // String table
79
        $objWriter->startElement('sst');
80
        $objWriter->writeAttribute('xmlns', Namespaces::MAIN);
81
        $objWriter->writeAttribute('uniqueCount', (string) count($stringTable));
82
 
83
        // Loop through string table
84
        foreach ($stringTable as $textElement) {
85
            $objWriter->startElement('si');
86
 
87
            if (!($textElement instanceof RichText)) {
88
                $textToWrite = StringHelper::controlCharacterPHP2OOXML($textElement);
89
                $objWriter->startElement('t');
90
                if ($textToWrite !== trim($textToWrite)) {
91
                    $objWriter->writeAttribute('xml:space', 'preserve');
92
                }
93
                $objWriter->writeRawData($textToWrite);
94
                $objWriter->endElement();
95
            } else {
96
                $this->writeRichText($objWriter, $textElement);
97
            }
98
 
99
            $objWriter->endElement();
100
        }
101
 
102
        $objWriter->endElement();
103
 
104
        return $objWriter->getData();
105
    }
106
 
107
    /**
108
     * Write Rich Text.
109
     *
110
     * @param ?string $prefix Optional Namespace prefix
111
     */
112
    public function writeRichText(XMLWriter $objWriter, RichText $richText, ?string $prefix = null): void
113
    {
114
        if ($prefix !== null) {
115
            $prefix .= ':';
116
        }
117
 
118
        // Loop through rich text elements
119
        $elements = $richText->getRichTextElements();
120
        foreach ($elements as $element) {
121
            // r
122
            $objWriter->startElement($prefix . 'r');
123
 
124
            // rPr
125
            if ($element instanceof Run && $element->getFont() !== null) {
126
                // rPr
127
                $objWriter->startElement($prefix . 'rPr');
128
 
129
                // rFont
130
                if ($element->getFont()->getName() !== null) {
131
                    $objWriter->startElement($prefix . 'rFont');
132
                    $objWriter->writeAttribute('val', $element->getFont()->getName());
133
                    $objWriter->endElement();
134
                }
135
 
136
                // Bold
137
                $objWriter->startElement($prefix . 'b');
138
                $objWriter->writeAttribute('val', ($element->getFont()->getBold() ? 'true' : 'false'));
139
                $objWriter->endElement();
140
 
141
                // Italic
142
                $objWriter->startElement($prefix . 'i');
143
                $objWriter->writeAttribute('val', ($element->getFont()->getItalic() ? 'true' : 'false'));
144
                $objWriter->endElement();
145
 
146
                // Superscript / subscript
147
                if ($element->getFont()->getSuperscript() || $element->getFont()->getSubscript()) {
148
                    $objWriter->startElement($prefix . 'vertAlign');
149
                    if ($element->getFont()->getSuperscript()) {
150
                        $objWriter->writeAttribute('val', 'superscript');
151
                    } elseif ($element->getFont()->getSubscript()) {
152
                        $objWriter->writeAttribute('val', 'subscript');
153
                    }
154
                    $objWriter->endElement();
155
                }
156
 
157
                // Strikethrough
158
                $objWriter->startElement($prefix . 'strike');
159
                $objWriter->writeAttribute('val', ($element->getFont()->getStrikethrough() ? 'true' : 'false'));
160
                $objWriter->endElement();
161
 
162
                // Color
163
                if ($element->getFont()->getColor()->getARGB() !== null) {
164
                    $objWriter->startElement($prefix . 'color');
165
                    $objWriter->writeAttribute('rgb', $element->getFont()->getColor()->getARGB());
166
                    $objWriter->endElement();
167
                }
168
 
169
                // Size
170
                if ($element->getFont()->getSize() !== null) {
171
                    $objWriter->startElement($prefix . 'sz');
172
                    $objWriter->writeAttribute('val', (string) $element->getFont()->getSize());
173
                    $objWriter->endElement();
174
                }
175
 
176
                // Underline
177
                if ($element->getFont()->getUnderline() !== null) {
178
                    $objWriter->startElement($prefix . 'u');
179
                    $objWriter->writeAttribute('val', $element->getFont()->getUnderline());
180
                    $objWriter->endElement();
181
                }
182
 
183
                $objWriter->endElement();
184
            }
185
 
186
            // t
187
            $objWriter->startElement($prefix . 't');
188
            $objWriter->writeAttribute('xml:space', 'preserve');
189
            $objWriter->writeRawData(StringHelper::controlCharacterPHP2OOXML($element->getText()));
190
            $objWriter->endElement();
191
 
192
            $objWriter->endElement();
193
        }
194
    }
195
 
196
    /**
197
     * Write Rich Text.
198
     *
199
     * @param RichText|string $richText text string or Rich text
200
     * @param string $prefix Optional Namespace prefix
201
     */
202
    public function writeRichTextForCharts(XMLWriter $objWriter, $richText = null, string $prefix = ''): void
203
    {
204
        if (!($richText instanceof RichText)) {
205
            $textRun = $richText;
206
            $richText = new RichText();
207
            $run = $richText->createTextRun($textRun ?? '');
208
            $run->setFont(null);
209
        }
210
 
211
        if ($prefix !== '') {
212
            $prefix .= ':';
213
        }
214
 
215
        // Loop through rich text elements
216
        $elements = $richText->getRichTextElements();
217
        foreach ($elements as $element) {
218
            // r
219
            $objWriter->startElement($prefix . 'r');
220
            if ($element->getFont() !== null) {
221
                // rPr
222
                $objWriter->startElement($prefix . 'rPr');
223
                $fontSize = $element->getFont()->getSize();
224
                if (is_numeric($fontSize)) {
225
                    $fontSize *= (($fontSize < 100) ? 100 : 1);
226
                    $objWriter->writeAttribute('sz', (string) $fontSize);
227
                }
228
 
229
                // Bold
230
                $objWriter->writeAttribute('b', ($element->getFont()->getBold() ? '1' : '0'));
231
                // Italic
232
                $objWriter->writeAttribute('i', ($element->getFont()->getItalic() ? '1' : '0'));
233
                // Underline
234
                $underlineType = $element->getFont()->getUnderline();
235
                switch ($underlineType) {
236
                    case 'single':
237
                        $underlineType = 'sng';
238
 
239
                        break;
240
                    case 'double':
241
                        $underlineType = 'dbl';
242
 
243
                        break;
244
                }
245
                if ($underlineType !== null) {
246
                    $objWriter->writeAttribute('u', $underlineType);
247
                }
248
                // Strikethrough
249
                $objWriter->writeAttribute('strike', ($element->getFont()->getStriketype() ?: 'noStrike'));
250
                // Superscript/subscript
251
                if ($element->getFont()->getBaseLine()) {
252
                    $objWriter->writeAttribute('baseline', (string) $element->getFont()->getBaseLine());
253
                }
254
 
255
                // Color
256
                $this->writeChartTextColor($objWriter, $element->getFont()->getChartColor(), $prefix);
257
 
258
                // Underscore Color
259
                $this->writeChartTextColor($objWriter, $element->getFont()->getUnderlineColor(), $prefix, 'uFill');
260
 
261
                // fontName
262
                if ($element->getFont()->getLatin()) {
263
                    $objWriter->startElement($prefix . 'latin');
264
                    $objWriter->writeAttribute('typeface', $element->getFont()->getLatin());
265
                    $objWriter->endElement();
266
                }
267
                if ($element->getFont()->getEastAsian()) {
268
                    $objWriter->startElement($prefix . 'ea');
269
                    $objWriter->writeAttribute('typeface', $element->getFont()->getEastAsian());
270
                    $objWriter->endElement();
271
                }
272
                if ($element->getFont()->getComplexScript()) {
273
                    $objWriter->startElement($prefix . 'cs');
274
                    $objWriter->writeAttribute('typeface', $element->getFont()->getComplexScript());
275
                    $objWriter->endElement();
276
                }
277
 
278
                $objWriter->endElement();
279
            }
280
 
281
            // t
282
            $objWriter->startElement($prefix . 't');
283
            $objWriter->writeRawData(StringHelper::controlCharacterPHP2OOXML($element->getText()));
284
            $objWriter->endElement();
285
 
286
            $objWriter->endElement();
287
        }
288
    }
289
 
290
    private function writeChartTextColor(XMLWriter $objWriter, ?ChartColor $underlineColor, string $prefix, ?string $openTag = ''): void
291
    {
292
        if ($underlineColor !== null) {
293
            $type = $underlineColor->getType();
294
            $value = $underlineColor->getValue();
295
            if (!empty($type) && !empty($value)) {
296
                if ($openTag !== '') {
297
                    $objWriter->startElement($prefix . $openTag);
298
                }
299
                $objWriter->startElement($prefix . 'solidFill');
300
                $objWriter->startElement($prefix . $type);
301
                $objWriter->writeAttribute('val', $value);
302
                $alpha = $underlineColor->getAlpha();
303
                if (is_numeric($alpha)) {
304
                    $objWriter->startElement('a:alpha');
305
                    $objWriter->writeAttribute('val', ChartColor::alphaToXml((int) $alpha));
306
                    $objWriter->endElement();
307
                }
308
                $objWriter->endElement(); // srgbClr/schemeClr/prstClr
309
                $objWriter->endElement(); // solidFill
310
                if ($openTag !== '') {
311
                    $objWriter->endElement(); // uFill
312
                }
313
            }
314
        }
315
    }
316
 
317
    /**
318
     * Flip string table (for index searching).
319
     *
320
     * @param array $stringTable Stringtable
321
     */
322
    public function flipStringTable(array $stringTable): array
323
    {
324
        // Return value
325
        $returnValue = [];
326
 
327
        // Loop through stringtable and add flipped items to $returnValue
328
        foreach ($stringTable as $key => $value) {
329
            if (!$value instanceof RichText) {
330
                $returnValue[$value] = $key;
331
            } elseif ($value instanceof RichText) {
332
                $returnValue[$value->getHashCode()] = $key;
333
            }
334
        }
335
 
336
        return $returnValue;
337
    }
338
}