| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Writer; |
| 4 |
|
| 5 |
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 6 |
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException; |
| 7 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content; |
| 8 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Meta; |
| 9 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\MetaInf; |
| 10 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Mimetype; |
| 11 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Settings; |
| 12 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Styles; |
| 13 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails; |
| 14 |
use ZipStream\Exception\OverflowException; |
| 15 |
use ZipStream\ZipStream; |
| 16 |
|
| 17 |
class Ods extends BaseWriter |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Private PhpSpreadsheet. |
| 21 |
* |
| 22 |
* @var Spreadsheet |
| 23 |
*/ |
| 24 |
private $spreadSheet; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var Content |
| 28 |
*/ |
| 29 |
private $writerPartContent; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var Meta |
| 33 |
*/ |
| 34 |
private $writerPartMeta; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var MetaInf |
| 38 |
*/ |
| 39 |
private $writerPartMetaInf; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var Mimetype |
| 43 |
*/ |
| 44 |
private $writerPartMimetype; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var Settings |
| 48 |
*/ |
| 49 |
private $writerPartSettings; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var Styles |
| 53 |
*/ |
| 54 |
private $writerPartStyles; |
| 55 |
|
| 56 |
/** |
| 57 |
* @var Thumbnails |
| 58 |
*/ |
| 59 |
private $writerPartThumbnails; |
| 60 |
|
| 61 |
/** |
| 62 |
* Create a new Ods. |
| 63 |
*/ |
| 64 |
public function __construct(Spreadsheet $spreadsheet) |
| 65 |
{ |
| 66 |
$this->setSpreadsheet($spreadsheet); |
| 67 |
|
| 68 |
$this->writerPartContent = new Content($this); |
| 69 |
$this->writerPartMeta = new Meta($this); |
| 70 |
$this->writerPartMetaInf = new MetaInf($this); |
| 71 |
$this->writerPartMimetype = new Mimetype($this); |
| 72 |
$this->writerPartSettings = new Settings($this); |
| 73 |
$this->writerPartStyles = new Styles($this); |
| 74 |
$this->writerPartThumbnails = new Thumbnails($this); |
| 75 |
} |
| 76 |
|
| 77 |
public function getWriterPartContent(): Content |
| 78 |
{ |
| 79 |
return $this->writerPartContent; |
| 80 |
} |
| 81 |
|
| 82 |
public function getWriterPartMeta(): Meta |
| 83 |
{ |
| 84 |
return $this->writerPartMeta; |
| 85 |
} |
| 86 |
|
| 87 |
public function getWriterPartMetaInf(): MetaInf |
| 88 |
{ |
| 89 |
return $this->writerPartMetaInf; |
| 90 |
} |
| 91 |
|
| 92 |
public function getWriterPartMimetype(): Mimetype |
| 93 |
{ |
| 94 |
return $this->writerPartMimetype; |
| 95 |
} |
| 96 |
|
| 97 |
public function getWriterPartSettings(): Settings |
| 98 |
{ |
| 99 |
return $this->writerPartSettings; |
| 100 |
} |
| 101 |
|
| 102 |
public function getWriterPartStyles(): Styles |
| 103 |
{ |
| 104 |
return $this->writerPartStyles; |
| 105 |
} |
| 106 |
|
| 107 |
public function getWriterPartThumbnails(): Thumbnails |
| 108 |
{ |
| 109 |
return $this->writerPartThumbnails; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Save PhpSpreadsheet to file. |
| 114 |
* |
| 115 |
* @param resource|string $filename |
| 116 |
*/ |
| 117 |
public function save($filename, int $flags = 0): void |
| 118 |
{ |
| 119 |
$this->processFlags($flags); |
| 120 |
|
| 121 |
// garbage collect |
| 122 |
$this->spreadSheet->garbageCollect(); |
| 123 |
|
| 124 |
$this->openFileHandle($filename); |
| 125 |
|
| 126 |
$zip = $this->createZip(); |
| 127 |
|
| 128 |
$zip->addFile('META-INF/manifest.xml', $this->getWriterPartMetaInf()->write()); |
| 129 |
$zip->addFile('Thumbnails/thumbnail.png', $this->getWriterPartthumbnails()->write()); |
| 130 |
// Settings always need to be written before Content; Styles after Content |
| 131 |
$zip->addFile('settings.xml', $this->getWriterPartsettings()->write()); |
| 132 |
$zip->addFile('content.xml', $this->getWriterPartcontent()->write()); |
| 133 |
$zip->addFile('meta.xml', $this->getWriterPartmeta()->write()); |
| 134 |
$zip->addFile('mimetype', $this->getWriterPartmimetype()->write()); |
| 135 |
$zip->addFile('styles.xml', $this->getWriterPartstyles()->write()); |
| 136 |
|
| 137 |
// Close file |
| 138 |
try { |
| 139 |
$zip->finish(); |
| 140 |
} catch (OverflowException $e) { |
| 141 |
throw new WriterException('Could not close resource.'); |
| 142 |
} |
| 143 |
|
| 144 |
$this->maybeCloseFileHandle(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Create zip object. |
| 149 |
* |
| 150 |
* @return ZipStream |
| 151 |
*/ |
| 152 |
private function createZip() |
| 153 |
{ |
| 154 |
// Try opening the ZIP file |
| 155 |
if (!is_resource($this->fileHandle)) { |
| 156 |
throw new WriterException('Could not open resource for writing.'); |
| 157 |
} |
| 158 |
|
| 159 |
// Create new ZIP stream |
| 160 |
return ZipStream0::newZipStream($this->fileHandle); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get Spreadsheet object. |
| 165 |
* |
| 166 |
* @return Spreadsheet |
| 167 |
*/ |
| 168 |
public function getSpreadsheet() |
| 169 |
{ |
| 170 |
return $this->spreadSheet; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Set Spreadsheet object. |
| 175 |
* |
| 176 |
* @param Spreadsheet $spreadsheet PhpSpreadsheet object |
| 177 |
* |
| 178 |
* @return $this |
| 179 |
*/ |
| 180 |
public function setSpreadsheet(Spreadsheet $spreadsheet) |
| 181 |
{ |
| 182 |
$this->spreadSheet = $spreadsheet; |
| 183 |
|
| 184 |
return $this; |
| 185 |
} |
| 186 |
} |
| 187 |
|