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