Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ODText
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 load
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 readPart
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 readRelationships
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord 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/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PHPWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Reader;
19
20use PhpOffice\PhpWord\PhpWord;
21use PhpOffice\PhpWord\Shared\XMLReader;
22
23/**
24 * Reader for ODText.
25 *
26 * @since 0.10.0
27 */
28class ODText extends AbstractReader implements ReaderInterface
29{
30    /**
31     * Loads PhpWord from file.
32     *
33     * @param string $docFile
34     *
35     * @return \PhpOffice\PhpWord\PhpWord
36     */
37    public function load($docFile)
38    {
39        $phpWord = new PhpWord();
40        $relationships = $this->readRelationships($docFile);
41
42        $readerParts = [
43            'content.xml' => 'Content',
44            'meta.xml' => 'Meta',
45        ];
46
47        foreach ($readerParts as $xmlFile => $partName) {
48            $this->readPart($phpWord, $relationships, $partName, $docFile, $xmlFile);
49        }
50
51        return $phpWord;
52    }
53
54    /**
55     * Read document part.
56     */
57    private function readPart(PhpWord $phpWord, array $relationships, string $partName, string $docFile, string $xmlFile): void
58    {
59        $partClass = "PhpOffice\\PhpWord\\Reader\\ODText\\{$partName}";
60        if (class_exists($partClass)) {
61            /** @var \PhpOffice\PhpWord\Reader\ODText\AbstractPart $part Type hint */
62            $part = new $partClass($docFile, $xmlFile);
63            $part->setRels($relationships);
64            $part->read($phpWord);
65        }
66    }
67
68    /**
69     * Read all relationship files.
70     */
71    private function readRelationships(string $docFile): array
72    {
73        $rels = [];
74        $xmlFile = 'META-INF/manifest.xml';
75        $xmlReader = new XMLReader();
76        $xmlReader->getDomFromZip($docFile, $xmlFile);
77        $nodes = $xmlReader->getElements('manifest:file-entry');
78        foreach ($nodes as $node) {
79            $type = $xmlReader->getAttribute('manifest:media-type', $node);
80            $target = $xmlReader->getAttribute('manifest:full-path', $node);
81            $rels[] = ['type' => $type, 'target' => $target];
82        }
83
84        return $rels;
85    }
86}