diff options
author | h-hishida <h-hishida@quartetcom.co.jp> | 2015-04-28 09:45:00 +0900 |
---|---|---|
committer | h-hishida <h-hishida@quartetcom.co.jp> | 2015-04-28 09:45:00 +0900 |
commit | 373ae12ca5db9837f84fef44f3f7077e766aca08 (patch) | |
tree | c515f2cccceaa886282ac40f89785da072a3627e /src/StyleFixer.php | |
download | PHPExcelFixer.StyleFixer-373ae12ca5db9837f84fef44f3f7077e766aca08.zip PHPExcelFixer.StyleFixer-373ae12ca5db9837f84fef44f3f7077e766aca08.tar.gz PHPExcelFixer.StyleFixer-373ae12ca5db9837f84fef44f3f7077e766aca08.tar.bz2 |
initial commit
Diffstat (limited to 'src/StyleFixer.php')
-rw-r--r-- | src/StyleFixer.php | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/StyleFixer.php b/src/StyleFixer.php new file mode 100644 index 0000000..2e4457a --- /dev/null +++ b/src/StyleFixer.php @@ -0,0 +1,77 @@ +<?php + +namespace PHPExcel\StyleFixer; + +use PHPExcel\StyleFixer\Plugin\Plugin; + +/** + * Class StyleFixer + * PHPExcelが壊した書式設定を修復(テンプレートからコピー)する + */ +class StyleFixer +{ + const STYLES_XML_PATH = 'xl/styles.xml'; + + /** + * @var Plugin[] + */ + private $plugins; + + public function __construct(array $plugins = null) + { + $this->plugins = $plugins; + } + + /** + * @param string $outputPath + * @param string $templatePath + */ + public function execute($outputPath, $templatePath) + { + $output = $this->openFile($outputPath); + $template = $this->openFile($templatePath); + + // スタイル定義を修復 + $this->fixStyles($output, $template); + $output->close(); + + // 個別のシートを修復 + if (null !== $this->plugins) { + foreach ($this->plugins as $fixer) { + + $output = $this->openFile($outputPath); + + $fixer->execute($output, $template); + + $output->close(); + } + } + + $template->close(); + } + + /** + * @param string $path + * @return \ZipArchive + */ + protected function openFile($path) + { + $zip = new \ZipArchive; + $zip->open($path); + + return $zip; + } + + /** + * xl/styles.xml全体を修復(テンプレートからコピー)する + * + * @param \ZipArchive $output + * @param \ZipArchive $template + */ + private function fixStyles(\ZipArchive $output, \ZipArchive $template) + { + $srcStylesXml = $template->getFromName(self::STYLES_XML_PATH); + + $output->addFromString(self::STYLES_XML_PATH, $srcStylesXml); + } +} |