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