Completed
Pull Request — develop (#565)
by
unknown
06:14
created

ODPresentation::readList()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.8333
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 20
1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Reader;
19
20
use PhpOffice\Common\Drawing as CommonDrawing;
21
use PhpOffice\Common\XMLReader;
22
use PhpOffice\PhpPresentation\PhpPresentation;
23
use PhpOffice\PhpPresentation\Shape\Drawing\Gd;
24
use PhpOffice\PhpPresentation\Shape\RichText;
25
use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
26
use PhpOffice\PhpPresentation\Slide\Background\Image;
27
use PhpOffice\PhpPresentation\Style\Alignment;
28
use PhpOffice\PhpPresentation\Style\Bullet;
29
use PhpOffice\PhpPresentation\Style\Color;
30
use PhpOffice\PhpPresentation\Style\Fill;
31
use PhpOffice\PhpPresentation\Style\Font;
32
use PhpOffice\PhpPresentation\Style\Shadow;
33
use ZipArchive;
34
35
/**
36
 * Serialized format reader
37
 */
38
class ODPresentation implements ReaderInterface
39
{
40
    /**
41
     * Output Object
42
     * @var PhpPresentation
43
     */
44
    protected $oPhpPresentation;
45
    /**
46
     * Output Object
47
     * @var \ZipArchive
48
     */
49
    protected $oZip;
50
    /**
51
     * @var array[]
52
     */
53
    protected $arrayStyles = array();
54
    /**
55
     * @var array[]
56
     */
57
    protected $arrayCommonStyles = array();
58
    /**
59
     * @var \PhpOffice\Common\XMLReader
60
     */
61
    protected $oXMLReader;
62
63
    /**
64
     * Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
65
     *
66
     * @param  string $pFilename
67
     * @throws \Exception
68
     * @return boolean
69
     */
70 2
    public function canRead($pFilename)
71
    {
72 2
        return $this->fileSupportsUnserializePhpPresentation($pFilename);
73
    }
74
75
    /**
76
     * Does a file support UnserializePhpPresentation ?
77
     *
78
     * @param  string    $pFilename
79
     * @throws \Exception
80
     * @return boolean
81
     */
82 5
    public function fileSupportsUnserializePhpPresentation($pFilename = '')
83
    {
84
        // Check if file exists
85 5
        if (!file_exists($pFilename)) {
86 2
            throw new \Exception('Could not open ' . $pFilename . ' for reading! File does not exist.');
87
        }
88
        
89 3
        $oZip = new ZipArchive();
90
        // Is it a zip ?
91 3
        if ($oZip->open($pFilename) === true) {
92
            // Is it an OpenXML Document ?
93
            // Is it a Presentation ?
94 1
            if (is_array($oZip->statName('META-INF/manifest.xml')) && is_array($oZip->statName('mimetype')) && $oZip->getFromName('mimetype') == 'application/vnd.oasis.opendocument.presentation') {
95 1
                return true;
96
            }
97
        }
98 3
        return false;
99
    }
100
101
    /**
102
     * Loads PhpPresentation Serialized file
103
     *
104
     * @param  string        $pFilename
105
     * @return \PhpOffice\PhpPresentation\PhpPresentation
106
     * @throws \Exception
107
     */
108 2
    public function load($pFilename)
109
    {
110
        // Unserialize... First make sure the file supports it!
111 2
        if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) {
112 1
            throw new \Exception("Invalid file format for PhpOffice\PhpPresentation\Reader\ODPresentation: " . $pFilename . '.');
113
        }
114
115
        return $this->loadFile($pFilename);
116
    }
117
118
    /**
119
     * Load PhpPresentation Serialized file
120
     *
121
     * @param  string $pFilename
122
     * @return \PhpOffice\PhpPresentation\PhpPresentation
123
     * @throws \Exception
124
     */
125
    protected function loadFile($pFilename)
126
    {
127
        $this->oPhpPresentation = new PhpPresentation();
128
        $this->oPhpPresentation->removeSlideByIndex();
129
        
130
        $this->oZip = new ZipArchive();
131
        $this->oZip->open($pFilename);
132
        
133
        $this->oXMLReader = new XMLReader();
134
        if ($this->oXMLReader->getDomFromZip($pFilename, 'meta.xml') !== false) {
135
            $this->loadDocumentProperties();
136
        }
137
        $this->oXMLReader = new XMLReader();
138
        if ($this->oXMLReader->getDomFromZip($pFilename, 'styles.xml') !== false) {
139
            $this->loadStylesFile();
140
        }
141
        $this->oXMLReader = new XMLReader();
142
        if ($this->oXMLReader->getDomFromZip($pFilename, 'content.xml') !== false) {
143
            $this->loadSlides();
144
        }
145
146
        return $this->oPhpPresentation;
147
    }
148
    
149
    /**
150
     * Read Document Properties
151
     */
152
    protected function loadDocumentProperties()
153
    {
154
        $arrayProperties = array(
155
            '/office:document-meta/office:meta/meta:initial-creator' => 'setCreator',
156
            '/office:document-meta/office:meta/dc:creator' => 'setLastModifiedBy',
157
            '/office:document-meta/office:meta/dc:title' => 'setTitle',
158
            '/office:document-meta/office:meta/dc:description' => 'setDescription',
159
            '/office:document-meta/office:meta/dc:subject' => 'setSubject',
160
            '/office:document-meta/office:meta/meta:keyword' => 'setKeywords',
161
            '/office:document-meta/office:meta/meta:creation-date' => 'setCreated',
162
            '/office:document-meta/office:meta/dc:date' => 'setModified',
163
        );
164
        $oProperties = $this->oPhpPresentation->getDocumentProperties();
165
        foreach ($arrayProperties as $path => $property) {
166
            $oElement = $this->oXMLReader->getElement($path);
167
            if ($oElement instanceof \DOMElement) {
168
                if (in_array($property, array('setCreated', 'setModified'))) {
169
                    $oDateTime = \DateTime::createFromFormat(\DateTime::W3C, $oElement->nodeValue);
170
                    $oProperties->{$property}($oDateTime->getTimestamp());
171
                } else {
172
                    $oProperties->{$property}($oElement->nodeValue);
173
                }
174
            }
175
        }
176
    }
177
    
178
    /**
179
     * Extract all slides
180
     */
181
    protected function loadSlides()
182
    {
183
        foreach ($this->oXMLReader->getElements('/office:document-content/office:automatic-styles/*') as $oElement) {
184
            if ($oElement->hasAttribute('style:name')) {
185
                $this->loadStyle($oElement);
186
            }
187
        }
188
        foreach ($this->oXMLReader->getElements('/office:document-content/office:body/office:presentation/draw:page') as $oElement) {
189
            if ($oElement->nodeName == 'draw:page') {
190
                $this->loadSlide($oElement);
191
            }
192
        }
193
    }
194
195
    /**
196
     * Extract style
197
     * @param \DOMElement $nodeStyle
198
     * @return bool
199
     * @throws \Exception
200
     */
201
    protected function loadStyle(\DOMElement $nodeStyle)
202
    {
203
        $keyStyle = $nodeStyle->getAttribute('style:name');
204
205
        $nodeDrawingPageProps = $this->oXMLReader->getElement('style:drawing-page-properties', $nodeStyle);
206
        if ($nodeDrawingPageProps instanceof \DOMElement) {
207
            // Read Background Color
208
            if ($nodeDrawingPageProps->hasAttribute('draw:fill-color') && $nodeDrawingPageProps->getAttribute('draw:fill') == 'solid') {
209
                $oBackground = new \PhpOffice\PhpPresentation\Slide\Background\Color();
210
                $oColor = new Color();
211
                $oColor->setRGB(substr($nodeDrawingPageProps->getAttribute('draw:fill-color'), -6));
212
                $oBackground->setColor($oColor);
213
            }
214
            // Read Background Image
215
            if ($nodeDrawingPageProps->getAttribute('draw:fill') == 'bitmap' && $nodeDrawingPageProps->hasAttribute('draw:fill-image-name')) {
216
                $nameStyle = $nodeDrawingPageProps->getAttribute('draw:fill-image-name');
217
                if (!empty($this->arrayCommonStyles[$nameStyle]) && $this->arrayCommonStyles[$nameStyle]['type'] == 'image' && !empty($this->arrayCommonStyles[$nameStyle]['path'])) {
218
                    $tmpBkgImg = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderODPBkg');
219
                    $contentImg = $this->oZip->getFromName($this->arrayCommonStyles[$nameStyle]['path']);
220
                    file_put_contents($tmpBkgImg, $contentImg);
221
222
                    $oBackground = new Image();
223
                    $oBackground->setPath($tmpBkgImg);
224
                }
225
            }
226
        }
227
228
        $nodeGraphicProps = $this->oXMLReader->getElement('style:graphic-properties', $nodeStyle);
229
        if ($nodeGraphicProps instanceof \DOMElement) {
230
            // Read Shadow
231
            if ($nodeGraphicProps->hasAttribute('draw:shadow') && $nodeGraphicProps->getAttribute('draw:shadow') == 'visible') {
232
                $oShadow = new Shadow();
233
                $oShadow->setVisible(true);
234
                if ($nodeGraphicProps->hasAttribute('draw:shadow-color')) {
235
                    $oShadow->getColor()->setRGB(substr($nodeGraphicProps->getAttribute('draw:shadow-color'), -6));
236
                }
237
                if ($nodeGraphicProps->hasAttribute('draw:shadow-opacity')) {
238
                    $oShadow->setAlpha(100 - (int)substr($nodeGraphicProps->getAttribute('draw:shadow-opacity'), 0, -1));
239
                }
240
                if ($nodeGraphicProps->hasAttribute('draw:shadow-offset-x') && $nodeGraphicProps->hasAttribute('draw:shadow-offset-y')) {
241
                    $offsetX = substr($nodeGraphicProps->getAttribute('draw:shadow-offset-x'), 0, -2);
242
                    $offsetY = substr($nodeGraphicProps->getAttribute('draw:shadow-offset-y'), 0, -2);
243
                    $distance = 0;
244
                    if ($offsetX != 0) {
245
                        $distance = ($offsetX < 0 ? $offsetX * -1 : $offsetX);
246
                    } elseif ($offsetY != 0) {
247
                        $distance = ($offsetY < 0 ? $offsetY * -1 : $offsetY);
248
                    }
249
                    $oShadow->setDirection(rad2deg(atan2($offsetY, $offsetX)));
250
                    $oShadow->setDistance(CommonDrawing::centimetersToPixels($distance));
251
                }
252
            }
253
            // Read Fill
254
            if ($nodeGraphicProps->hasAttribute('draw:fill')) {
255
                $value = $nodeGraphicProps->getAttribute('draw:fill');
256
257
                switch ($value) {
258
                    case 'none':
259
                        $oFill = new Fill();
260
                        $oFill->setFillType(Fill::FILL_NONE);
261
                        break;
262
                    case 'solid':
263
                        $oFill = new Fill();
264
                        $oFill->setFillType(Fill::FILL_SOLID);
265
                        if ($nodeGraphicProps->hasAttribute('draw:fill-color')) {
266
                            $oColor = new Color();
267
                            $oColor->setRGB(substr($nodeGraphicProps->getAttribute('draw:fill-color'), 1));
268
                            $oFill->setStartColor($oColor);
269
                        }
270
                        break;
271
                }
272
            }
273
        }
274
        
275
        $nodeTextProperties = $this->oXMLReader->getElement('style:text-properties', $nodeStyle);
276
        if ($nodeTextProperties instanceof \DOMElement) {
277
            $oFont = new Font();
278
            if ($nodeTextProperties->hasAttribute('fo:color')) {
279
                $oFont->getColor()->setRGB(substr($nodeTextProperties->getAttribute('fo:color'), -6));
280
            }
281
            if ($nodeTextProperties->hasAttribute('fo:font-family')) {
282
                $oFont->setName($nodeTextProperties->getAttribute('fo:font-family'));
283
            }
284
            if ($nodeTextProperties->hasAttribute('fo:font-weight') && $nodeTextProperties->getAttribute('fo:font-weight') == 'bold') {
285
                $oFont->setBold(true);
286
            }
287
            if ($nodeTextProperties->hasAttribute('fo:font-size')) {
288
                $oFont->setSize(substr($nodeTextProperties->getAttribute('fo:font-size'), 0, -2));
289
            }
290
        }
291
292
        $nodeParagraphProps = $this->oXMLReader->getElement('style:paragraph-properties', $nodeStyle);
293
        if ($nodeParagraphProps instanceof \DOMElement) {
294
            $oAlignment = new Alignment();
295
            if ($nodeParagraphProps->hasAttribute('fo:text-align')) {
296
                $oAlignment->setHorizontal($nodeParagraphProps->getAttribute('fo:text-align'));
297
            }
298
        }
299
        
300
        if ($nodeStyle->nodeName == 'text:list-style') {
301
            $arrayListStyle = array();
302
            foreach ($this->oXMLReader->getElements('text:list-level-style-bullet', $nodeStyle) as $oNodeListLevel) {
303
                $oAlignment = new Alignment();
304
                $oBullet = new Bullet();
305
                $oBullet->setBulletType(Bullet::TYPE_NONE);
306
                if ($oNodeListLevel->hasAttribute('text:level')) {
307
                    $oAlignment->setLevel((int) $oNodeListLevel->getAttribute('text:level') - 1);
308
                }
309
                if ($oNodeListLevel->hasAttribute('text:bullet-char')) {
310
                    $oBullet->setBulletChar($oNodeListLevel->getAttribute('text:bullet-char'));
311
                    $oBullet->setBulletType(Bullet::TYPE_BULLET);
312
                }
313
                
314
                $oNodeListProperties = $this->oXMLReader->getElement('style:list-level-properties', $oNodeListLevel);
315
                if ($oNodeListProperties instanceof \DOMElement) {
316
                    if ($oNodeListProperties->hasAttribute('text:min-label-width')) {
317
                        $oAlignment->setIndent((int)round(CommonDrawing::centimetersToPixels(substr($oNodeListProperties->getAttribute('text:min-label-width'), 0, -2))));
318
                    }
319
                    if ($oNodeListProperties->hasAttribute('text:space-before')) {
320
                        $iSpaceBefore = CommonDrawing::centimetersToPixels(substr($oNodeListProperties->getAttribute('text:space-before'), 0, -2));
321
                        $iMarginLeft = $iSpaceBefore + $oAlignment->getIndent();
322
                        $oAlignment->setMarginLeft($iMarginLeft);
323
                    }
324
                }
325
                $oNodeTextProperties = $this->oXMLReader->getElement('style:text-properties', $oNodeListLevel);
326
                if ($oNodeTextProperties instanceof \DOMElement) {
327
                    if ($oNodeTextProperties->hasAttribute('fo:font-family')) {
328
                        $oBullet->setBulletFont($oNodeTextProperties->getAttribute('fo:font-family'));
329
                    }
330
                }
331
                
332
                $arrayListStyle[$oAlignment->getLevel()] = array(
333
                    'alignment' => $oAlignment,
334
                    'bullet' => $oBullet,
335
                );
336
            }
337
        }
338
        
339
        $this->arrayStyles[$keyStyle] = array(
340
            'alignment' => isset($oAlignment) ? $oAlignment : null,
341
            'background' => isset($oBackground) ? $oBackground : null,
342
            'fill' => isset($oFill) ? $oFill : null,
343
            'font' => isset($oFont) ? $oFont : null,
344
            'shadow' => isset($oShadow) ? $oShadow : null,
345
            'listStyle' => isset($arrayListStyle) ? $arrayListStyle : null,
346
        );
347
        
348
        return true;
349
    }
350
351
    /**
352
     * Read Slide
353
     *
354
     * @param \DOMElement $nodeSlide
355
     * @return bool
356
     * @throws \Exception
357
     */
358
    protected function loadSlide(\DOMElement $nodeSlide)
359
    {
360
        // Core
361
        $this->oPhpPresentation->createSlide();
362
        $this->oPhpPresentation->setActiveSlideIndex($this->oPhpPresentation->getSlideCount() - 1);
363
        if ($nodeSlide->hasAttribute('draw:name')) {
364
            $this->oPhpPresentation->getActiveSlide()->setName($nodeSlide->getAttribute('draw:name'));
365
        }
366
        if ($nodeSlide->hasAttribute('draw:style-name')) {
367
            $keyStyle = $nodeSlide->getAttribute('draw:style-name');
368
            if (isset($this->arrayStyles[$keyStyle])) {
369
                $this->oPhpPresentation->getActiveSlide()->setBackground($this->arrayStyles[$keyStyle]['background']);
370
            }
371
        }
372
        foreach ($this->oXMLReader->getElements('draw:frame', $nodeSlide) as $oNodeFrame) {
373
            if ($this->oXMLReader->getElement('draw:image', $oNodeFrame)) {
374
                $this->loadShapeDrawing($oNodeFrame);
375
                continue;
376
            }
377
            if ($this->oXMLReader->getElement('draw:text-box', $oNodeFrame)) {
378
                $this->loadShapeRichText($oNodeFrame);
379
                continue;
380
            }
381
        }
382
        return true;
383
    }
384
385
    /**
386
     * Read Shape Drawing
387
     *
388
     * @param \DOMElement $oNodeFrame
389
     * @throws \Exception
390
     */
391
    protected function loadShapeDrawing(\DOMElement $oNodeFrame)
392
    {
393
        // Core
394
        $oShape = new Gd();
395
        $oShape->getShadow()->setVisible(false);
396
397
        $oNodeImage = $this->oXMLReader->getElement('draw:image', $oNodeFrame);
398
        if ($oNodeImage instanceof \DOMElement) {
399
            if ($oNodeImage->hasAttribute('xlink:href')) {
400
                $sFilename = $oNodeImage->getAttribute('xlink:href');
401
                // svm = StarView Metafile
402
                if (pathinfo($sFilename, PATHINFO_EXTENSION) == 'svm') {
403
                    return;
404
                }
405
                $imageFile = $this->oZip->getFromName($sFilename);
406
                if (!empty($imageFile)) {
407
                    $oShape->setImageResource(imagecreatefromstring($imageFile));
408
                }
409
            }
410
        }
411
        
412
        $oShape->setName($oNodeFrame->hasAttribute('draw:name') ? $oNodeFrame->getAttribute('draw:name') : '');
413
        $oShape->setDescription($oNodeFrame->hasAttribute('draw:name') ? $oNodeFrame->getAttribute('draw:name') : '');
414
        $oShape->setResizeProportional(false);
415
        $oShape->setWidth($oNodeFrame->hasAttribute('svg:width') ? (int)round(CommonDrawing::centimetersToPixels(substr($oNodeFrame->getAttribute('svg:width'), 0, -2))) : '');
416
        $oShape->setHeight($oNodeFrame->hasAttribute('svg:height') ? (int)round(CommonDrawing::centimetersToPixels(substr($oNodeFrame->getAttribute('svg:height'), 0, -2))) : '');
417
        $oShape->setResizeProportional(true);
418
        $oShape->setOffsetX($oNodeFrame->hasAttribute('svg:x') ? (int)round(CommonDrawing::centimetersToPixels(substr($oNodeFrame->getAttribute('svg:x'), 0, -2))) : '');
419
        $oShape->setOffsetY($oNodeFrame->hasAttribute('svg:y') ? (int)round(CommonDrawing::centimetersToPixels(substr($oNodeFrame->getAttribute('svg:y'), 0, -2))) : '');
420
        
421
        if ($oNodeFrame->hasAttribute('draw:style-name')) {
422
            $keyStyle = $oNodeFrame->getAttribute('draw:style-name');
423
            if (isset($this->arrayStyles[$keyStyle])) {
424
                $oShape->setShadow($this->arrayStyles[$keyStyle]['shadow']);
425
                $oShape->setFill($this->arrayStyles[$keyStyle]['fill']);
426
            }
427
        }
428
        
429
        $this->oPhpPresentation->getActiveSlide()->addShape($oShape);
430
    }
431
432
    /**
433
     * Read Shape RichText
434
     *
435
     * @param \DOMElement $oNodeFrame
436
     * @throws \Exception
437
     */
438
    protected function loadShapeRichText(\DOMElement $oNodeFrame)
439
    {
440
        // Core
441
        $oShape = $this->oPhpPresentation->getActiveSlide()->createRichTextShape();
442
        $oShape->setParagraphs(array());
443
        
444
        $oShape->setWidth($oNodeFrame->hasAttribute('svg:width') ? (int)round(CommonDrawing::centimetersToPixels(substr($oNodeFrame->getAttribute('svg:width'), 0, -2))) : '');
445
        $oShape->setHeight($oNodeFrame->hasAttribute('svg:height') ? (int)round(CommonDrawing::centimetersToPixels(substr($oNodeFrame->getAttribute('svg:height'), 0, -2))) : '');
446
        $oShape->setOffsetX($oNodeFrame->hasAttribute('svg:x') ? (int)round(CommonDrawing::centimetersToPixels(substr($oNodeFrame->getAttribute('svg:x'), 0, -2))) : '');
447
        $oShape->setOffsetY($oNodeFrame->hasAttribute('svg:y') ? (int)round(CommonDrawing::centimetersToPixels(substr($oNodeFrame->getAttribute('svg:y'), 0, -2))) : '');
448
        
449
        foreach ($this->oXMLReader->getElements('draw:text-box/*', $oNodeFrame) as $oNodeParagraph) {
450
            $this->levelParagraph = 0;
451
            if ($oNodeParagraph->nodeName == 'text:p') {
452
                $this->readParagraph($oShape, $oNodeParagraph);
453
            }
454
            if ($oNodeParagraph->nodeName == 'text:list') {
455
                $this->readList($oShape, $oNodeParagraph);
456
            }
457
        }
458
        
459
        if (count($oShape->getParagraphs()) > 0) {
460
            $oShape->setActiveParagraph(0);
461
        }
462
    }
463
    
464
    protected $levelParagraph = 0;
465
466
    /**
467
     * Read Paragraph
468
     * @param RichText $oShape
469
     * @param \DOMElement $oNodeParent
470
     * @throws \Exception
471
     */
472
    protected function readParagraph(RichText $oShape, \DOMElement $oNodeParent)
473
    {
474
        $oParagraph = $oShape->createParagraph();
475
        $oDomList = $this->oXMLReader->getElements('text:span', $oNodeParent);
476
        $oDomTextNodes = $this->oXMLReader->getElements('text()', $oNodeParent);
477
        foreach ($oDomTextNodes as $oDomTextNode) {
478
            if (trim($oDomTextNode->nodeValue) != '') {
479
                $oTextRun = $oParagraph->createTextRun();
480
                $oTextRun->setText(trim($oDomTextNode->nodeValue));
481
            }
482
        }
483
        foreach ($oDomList as $oNodeRichTextElement) {
484
            $this->readParagraphItem($oParagraph, $oNodeRichTextElement);
485
        }
486
    }
487
488
    /**
489
     * Read Paragraph Item
490
     * @param Paragraph $oParagraph
491
     * @param \DOMElement $oNodeParent
492
     * @throws \Exception
493
     */
494
    protected function readParagraphItem(Paragraph $oParagraph, \DOMElement $oNodeParent)
495
    {
496
        if ($this->oXMLReader->elementExists('text:line-break', $oNodeParent)) {
497
            $oParagraph->createBreak();
498
        } else {
499
            $oTextRun = $oParagraph->createTextRun();
500
            if ($oNodeParent->hasAttribute('text:style-name')) {
501
                $keyStyle = $oNodeParent->getAttribute('text:style-name');
502
                if (isset($this->arrayStyles[$keyStyle])) {
503
                    $oTextRun->setFont($this->arrayStyles[$keyStyle]['font']);
504
                }
505
            }
506
            $oTextRunLink = $this->oXMLReader->getElement('text:a', $oNodeParent);
507
            if ($oTextRunLink instanceof \DOMElement) {
508
                $oTextRun->setText($oTextRunLink->nodeValue);
509
                if ($oTextRunLink->hasAttribute('xlink:href')) {
510
                    $oTextRun->getHyperlink()->setUrl($oTextRunLink->getAttribute('xlink:href'));
511
                }
512
            } else {
513
                $oTextRun->setText($oNodeParent->nodeValue);
514
            }
515
        }
516
    }
517
518
    /**
519
     * Read List
520
     *
521
     * @param RichText $oShape
522
     * @param \DOMElement $oNodeParent
523
     * @throws \Exception
524
     */
525
    protected function readList(RichText $oShape, \DOMElement $oNodeParent)
526
    {
527
        foreach ($this->oXMLReader->getElements('text:list-item/*', $oNodeParent) as $oNodeListItem) {
528
            if ($oNodeListItem->nodeName == 'text:p') {
529
                $this->readListItem($oShape, $oNodeListItem, $oNodeParent);
530
            }
531
            if ($oNodeListItem->nodeName == 'text:list') {
532
                $this->levelParagraph++;
533
                $this->readList($oShape, $oNodeListItem);
534
                $this->levelParagraph--;
535
            }
536
        }
537
    }
538
539
    /**
540
     * Read List Item
541
     * @param RichText $oShape
542
     * @param \DOMElement $oNodeParent
543
     * @param \DOMElement $oNodeParagraph
544
     * @throws \Exception
545
     */
546
    protected function readListItem(RichText $oShape, \DOMElement $oNodeParent, \DOMElement $oNodeParagraph)
547
    {
548
        $oParagraph = $oShape->createParagraph();
549
        if ($oNodeParagraph->hasAttribute('text:style-name')) {
550
            $keyStyle = $oNodeParagraph->getAttribute('text:style-name');
551
            if (isset($this->arrayStyles[$keyStyle]) && !empty($this->arrayStyles[$keyStyle]['listStyle'])) {
552
                $oParagraph->setAlignment($this->arrayStyles[$keyStyle]['listStyle'][$this->levelParagraph]['alignment']);
553
                $oParagraph->setBulletStyle($this->arrayStyles[$keyStyle]['listStyle'][$this->levelParagraph]['bullet']);
554
            }
555
        }
556
        foreach ($this->oXMLReader->getElements('text:span', $oNodeParent) as $oNodeRichTextElement) {
557
            $this->readParagraphItem($oParagraph, $oNodeRichTextElement);
558
        }
559
    }
560
561
    /**
562
     * Load file 'styles.xml'
563
     */
564
    protected function loadStylesFile()
565
    {
566
        foreach ($this->oXMLReader->getElements('/office:document-styles/office:styles/*') as $oElement) {
567
            if ($oElement->nodeName == 'draw:fill-image') {
568
                $this->arrayCommonStyles[$oElement->getAttribute('draw:name')] = array(
569
                    'type' => 'image',
570
                    'path' => $oElement->hasAttribute('xlink:href') ? $oElement->getAttribute('xlink:href') : null
571
                );
572
            }
573
        }
574
    }
575
}
576