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

ODPresentation::loadStylesFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

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