Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.50% covered (success)
97.50%
39 / 40
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Serialized
97.50% covered (success)
97.50%
39 / 40
66.67% covered (warning)
66.67%
2 / 3
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
6
 writeSerialized
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
5
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation 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/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Writer;
21
22use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter;
23use PhpOffice\Common\XMLWriter;
24use PhpOffice\PhpPresentation\Exception\DirectoryNotFoundException;
25use PhpOffice\PhpPresentation\Exception\InvalidParameterException;
26use PhpOffice\PhpPresentation\PhpPresentation;
27use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter;
28use PhpOffice\PhpPresentation\Shape\Drawing\File;
29
30/**
31 * \PhpOffice\PhpPresentation\Writer\Serialized.
32 */
33class Serialized extends AbstractWriter implements WriterInterface
34{
35    /**
36     * Create a new \PhpOffice\PhpPresentation\Writer\Serialized.
37     */
38    public function __construct(?PhpPresentation $pPhpPresentation = null)
39    {
40        // Set PhpPresentation
41        $this->setPhpPresentation($pPhpPresentation ?? new PhpPresentation());
42
43        // Set ZIP Adapter
44        $this->setZipAdapter(new ZipArchiveAdapter());
45    }
46
47    /**
48     * Save PhpPresentation to file.
49     */
50    public function save(string $pFilename): void
51    {
52        if (empty($pFilename)) {
53            throw new InvalidParameterException('pFilename', '');
54        }
55        if (!is_dir(dirname($pFilename))) {
56            throw new DirectoryNotFoundException(dirname($pFilename));
57        }
58        $oPresentation = $this->getPhpPresentation();
59
60        // Create new ZIP file and open it for writing
61        $objZip = $this->getZipAdapter();
62
63        // Try opening the ZIP file
64        $objZip->open($pFilename);
65
66        // Add media
67        $slideCount = $oPresentation->getSlideCount();
68        for ($i = 0; $i < $slideCount; ++$i) {
69            foreach ($oPresentation->getSlide($i)->getShapeCollection() as $shape) {
70                if ($shape instanceof AbstractDrawingAdapter) {
71                    $objZip->addFromString(
72                        'media/' . $shape->getImageIndex() . '/' . pathinfo($shape->getPath(), PATHINFO_BASENAME),
73                        file_get_contents($shape->getPath())
74                    );
75                }
76            }
77        }
78
79        // Add PhpPresentation.xml to the document, which represents a PHP serialized PhpPresentation object
80        $objZip->addFromString('PhpPresentation.xml', $this->writeSerialized($oPresentation, $pFilename));
81
82        // Close file
83        $objZip->close();
84    }
85
86    /**
87     * Serialize PhpPresentation object to XML.
88     *
89     * @param string $pFilename
90     *
91     * @return string XML Output
92     */
93    protected function writeSerialized(?PhpPresentation $pPhpPresentation = null, $pFilename = '')
94    {
95        // Clone $pPhpPresentation
96        $pPhpPresentation = clone $pPhpPresentation;
97
98        // Update media links
99        $slideCount = $pPhpPresentation->getSlideCount();
100        for ($i = 0; $i < $slideCount; ++$i) {
101            foreach ($pPhpPresentation->getSlide($i)->getShapeCollection() as $shape) {
102                if ($shape instanceof AbstractDrawingAdapter) {
103                    $imgPath = 'zip://' . $pFilename . '#media/' . $shape->getImageIndex() . '/' . pathinfo($shape->getPath(), PATHINFO_BASENAME);
104                    if ($shape instanceof File) {
105                        $shape->setPath($imgPath, false);
106                    } else {
107                        $shape->setPath($imgPath);
108                    }
109                }
110            }
111        }
112
113        // Create XML writer
114        $objWriter = new XMLWriter();
115        $objWriter->openMemory();
116        $objWriter->setIndent(true);
117
118        // XML header
119        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
120
121        // PhpPresentation
122        $objWriter->startElement('PhpPresentation');
123        $objWriter->writeAttribute('version', '##VERSION##');
124
125        // Comment
126        $objWriter->writeComment('This file has been generated using PhpPresentation v##VERSION## (http://github.com/PHPOffice/PhpPresentation). It contains a base64 encoded serialized version of the PhpPresentation internal object.');
127
128        // Data
129        $objWriter->startElement('data');
130        $objWriter->writeCData(base64_encode(serialize($pPhpPresentation)));
131        $objWriter->endElement();
132
133        $objWriter->endElement();
134
135        // Return
136        return $objWriter->getData();
137    }
138}