Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.44% |
34 / 36 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ODText | |
94.44% |
34 / 36 |
|
50.00% |
1 / 2 |
11.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| save | |
90.48% |
19 / 21 |
|
0.00% |
0 / 1 |
8.06 | |||
| 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\Writer; |
| 20 | |
| 21 | use PhpOffice\Math\Writer\MathML; |
| 22 | use PhpOffice\PhpWord\Element\AbstractElement; |
| 23 | use PhpOffice\PhpWord\Element\Formula; |
| 24 | use PhpOffice\PhpWord\Media; |
| 25 | use PhpOffice\PhpWord\PhpWord; |
| 26 | use PhpOffice\PhpWord\Writer\ODText\Part\AbstractPart; |
| 27 | |
| 28 | /** |
| 29 | * ODText writer. |
| 30 | * |
| 31 | * @since 0.7.0 |
| 32 | */ |
| 33 | class ODText extends AbstractWriter implements WriterInterface |
| 34 | { |
| 35 | /** |
| 36 | * @var AbstractElement[] |
| 37 | */ |
| 38 | protected $objects = []; |
| 39 | |
| 40 | /** |
| 41 | * Create new ODText writer. |
| 42 | */ |
| 43 | public function __construct(?PhpWord $phpWord = null) |
| 44 | { |
| 45 | // Assign PhpWord |
| 46 | $this->setPhpWord($phpWord); |
| 47 | |
| 48 | // Create parts |
| 49 | $this->parts = [ |
| 50 | 'Mimetype' => 'mimetype', |
| 51 | 'Content' => 'content.xml', |
| 52 | 'Meta' => 'meta.xml', |
| 53 | 'Styles' => 'styles.xml', |
| 54 | 'Manifest' => 'META-INF/manifest.xml', |
| 55 | ]; |
| 56 | foreach (array_keys($this->parts) as $partName) { |
| 57 | $partClass = static::class . '\\Part\\' . $partName; |
| 58 | if (class_exists($partClass)) { |
| 59 | /** @var AbstractPart $partObject Type hint */ |
| 60 | $partObject = new $partClass(); |
| 61 | $partObject->setParentWriter($this); |
| 62 | $this->writerParts[strtolower($partName)] = $partObject; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Set package paths |
| 67 | $this->mediaPaths = ['image' => 'Pictures/']; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Save PhpWord to file. |
| 72 | */ |
| 73 | public function save(string $filename): void |
| 74 | { |
| 75 | $filename = $this->getTempFile($filename); |
| 76 | $zip = $this->getZipArchive($filename); |
| 77 | |
| 78 | // Add section media files |
| 79 | $sectionMedia = Media::getElements('section'); |
| 80 | if (!empty($sectionMedia)) { |
| 81 | $this->addFilesToPackage($zip, $sectionMedia); |
| 82 | } |
| 83 | |
| 84 | // Write parts |
| 85 | foreach ($this->parts as $partName => $fileName) { |
| 86 | if ($fileName === '') { |
| 87 | continue; |
| 88 | } |
| 89 | $part = $this->getWriterPart($partName); |
| 90 | if (!$part instanceof AbstractPart) { |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | $part->setObjects($this->objects); |
| 95 | |
| 96 | $zip->addFromString($fileName, $part->write()); |
| 97 | |
| 98 | $this->objects = $part->getObjects(); |
| 99 | } |
| 100 | |
| 101 | // Write objects charts |
| 102 | if (!empty($this->objects)) { |
| 103 | $writer = new MathML(); |
| 104 | foreach ($this->objects as $idxObject => $object) { |
| 105 | if ($object instanceof Formula) { |
| 106 | $zip->addFromString('Formula' . $idxObject . '/content.xml', $writer->write($object->getMath())); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Close zip archive and cleanup temp file |
| 112 | $zip->close(); |
| 113 | $this->cleanupTempFile(); |
| 114 | } |
| 115 | } |