Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.67% |
29 / 30 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
EPub3 | |
96.67% |
29 / 30 |
|
50.00% |
1 / 2 |
9 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
save | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
4.01 |
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\PhpWord\PhpWord; |
22 | use PhpOffice\PhpWord\Writer\EPub3\Part\AbstractPart; |
23 | |
24 | /** |
25 | * EPub3 writer. |
26 | */ |
27 | class EPub3 extends AbstractWriter implements WriterInterface |
28 | { |
29 | /** |
30 | * Create new EPub3 writer. |
31 | */ |
32 | public function __construct(?PhpWord $phpWord = null) |
33 | { |
34 | // Assign PhpWord |
35 | $this->setPhpWord($phpWord); |
36 | |
37 | // Create parts |
38 | $this->parts = [ |
39 | 'Mimetype' => 'mimetype', |
40 | 'Content' => 'content.opf', |
41 | 'Toc' => 'toc.ncx', |
42 | 'Styles' => 'styles.css', |
43 | 'Manifest' => 'META-INF/container.xml', |
44 | 'Nav' => 'nav.xhtml', |
45 | 'ContentXhtml' => 'content.xhtml', |
46 | ]; |
47 | foreach (array_keys($this->parts) as $partName) { |
48 | $partClass = static::class . '\\Part\\' . $partName; |
49 | if (class_exists($partClass)) { |
50 | /** @var WriterPartInterface $part */ |
51 | $part = new $partClass($partName === 'Content' || $partName === 'ContentXhtml' ? $phpWord : null); |
52 | $part->setParentWriter($this); |
53 | $this->writerParts[strtolower($partName)] = $part; |
54 | } |
55 | } |
56 | |
57 | // Set package paths |
58 | $this->mediaPaths = ['image' => 'Images/', 'object' => 'Objects/']; |
59 | } |
60 | |
61 | /** |
62 | * Save PhpWord to file. |
63 | */ |
64 | public function save(string $filename): void |
65 | { |
66 | $filename = $this->getTempFile($filename); |
67 | $zip = $this->getZipArchive($filename); |
68 | |
69 | // Add mimetype first without compression |
70 | $zip->addFromString('mimetype', 'application/epub+zip'); |
71 | $zip->addEmptyDir('META-INF'); |
72 | |
73 | // Add other files |
74 | foreach ($this->parts as $partName => $fileName) { |
75 | if ($fileName === '') { |
76 | continue; |
77 | } |
78 | $part = $this->getWriterPart($partName); |
79 | if (!$part instanceof AbstractPart) { |
80 | continue; |
81 | } |
82 | $zip->addFromString($fileName, $part->write()); |
83 | } |
84 | |
85 | // Close zip archive |
86 | $zip->close(); |
87 | |
88 | // Cleanup temp file |
89 | $this->cleanupTempFile(); |
90 | } |
91 | } |