1 | <?php |
||
27 | class Serialized implements WriterInterface |
||
28 | { |
||
29 | /** |
||
30 | * Private PhpPresentation |
||
31 | * |
||
32 | * @var \PhpOffice\PhpPresentation\PhpPresentation |
||
33 | */ |
||
34 | private $presentation; |
||
35 | |||
36 | /** |
||
37 | * Create a new \PhpOffice\PhpPresentation\Writer\Serialized |
||
38 | * |
||
39 | * @param \PhpOffice\PhpPresentation\PhpPresentation $pPhpPresentation |
||
40 | */ |
||
41 | 7 | public function __construct(PhpPresentation $pPhpPresentation = null) |
|
46 | |||
47 | /** |
||
48 | * Save PhpPresentation to file |
||
49 | * |
||
50 | * @param string $pFilename |
||
51 | * @throws \Exception |
||
52 | */ |
||
53 | 5 | public function save($pFilename) |
|
54 | { |
||
55 | 5 | if (empty($pFilename)) { |
|
56 | 1 | throw new \Exception("Filename is empty."); |
|
57 | } |
||
58 | 4 | if (!is_null($this->presentation)) { |
|
59 | // Create new ZIP file and open it for writing |
||
60 | 3 | $objZip = new \ZipArchive(); |
|
61 | |||
62 | // Try opening the ZIP file |
||
63 | 3 | if ($objZip->open($pFilename, \ZipArchive::CREATE) !== true) { |
|
64 | 2 | if ($objZip->open($pFilename, \ZipArchive::OVERWRITE) !== true) { |
|
65 | 1 | throw new \Exception("Could not open " . $pFilename . " for writing."); |
|
66 | } |
||
67 | 1 | } |
|
68 | |||
69 | // Add media |
||
70 | 2 | $slideCount = $this->presentation->getSlideCount(); |
|
71 | 2 | for ($i = 0; $i < $slideCount; ++$i) { |
|
72 | 2 | for ($j = 0; $j < $this->presentation->getSlide($i)->getShapeCollection()->count(); ++$j) { |
|
73 | 2 | if ($this->presentation->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawing) { |
|
74 | 2 | $imgTemp = $this->presentation->getSlide($i)->getShapeCollection()->offsetGet($j); |
|
75 | 2 | $objZip->addFromString('media/' . $imgTemp->getFilename(), file_get_contents($imgTemp->getPath())); |
|
76 | 2 | } |
|
77 | 2 | } |
|
78 | 2 | } |
|
79 | |||
80 | // Add PhpPresentation.xml to the document, which represents a PHP serialized PhpPresentation object |
||
81 | 2 | $objZip->addFromString('PhpPresentation.xml', $this->writeSerialized($this->presentation, $pFilename)); |
|
82 | |||
83 | // Close file |
||
84 | 2 | if ($objZip->close() === false) { |
|
85 | throw new \Exception("Could not close zip file $pFilename."); |
||
86 | } |
||
87 | 2 | } else { |
|
88 | 1 | throw new \Exception("PhpPresentation object unassigned."); |
|
89 | } |
||
90 | 2 | } |
|
91 | |||
92 | /** |
||
93 | * Get PhpPresentation object |
||
94 | * |
||
95 | * @return PhpPresentation |
||
96 | * @throws \Exception |
||
97 | */ |
||
98 | 2 | public function getPhpPresentation() |
|
106 | |||
107 | /** |
||
108 | * Get PhpPresentation object |
||
109 | * |
||
110 | * @param PhpPresentation $pPhpPresentation PhpPresentation object |
||
111 | * @throws \Exception |
||
112 | * @return \PhpOffice\PhpPresentation\Writer\Serialized |
||
113 | */ |
||
114 | 7 | public function setPhpPresentation(PhpPresentation $pPhpPresentation = null) |
|
120 | |||
121 | /** |
||
122 | * Serialize PhpPresentation object to XML |
||
123 | * |
||
124 | * @param PhpPresentation $pPhpPresentation |
||
125 | * @param string $pFilename |
||
126 | * @return string XML Output |
||
127 | * @throws \Exception |
||
128 | */ |
||
129 | 2 | private function writeSerialized(PhpPresentation $pPhpPresentation = null, $pFilename = '') |
|
169 | } |
||
170 |