ODPresentation::readList()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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