collections[$collection] = new $class(); } // Metadata $metadata = array('DocInfo', 'Settings', 'Compatibility'); foreach ($metadata as $meta) { $class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta; $this->metadata[$meta] = new $class(); } } /** * Dynamic function call to reduce static dependency * * @since 0.12.0 * * @param mixed $function * @param mixed $args * * @throws \BadMethodCallException * * @return mixed */ public function __call($function, $args) { $function = strtolower($function); $getCollection = array(); $addCollection = array(); $addStyle = array(); $collections = array('Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart', 'Comment'); foreach ($collections as $collection) { $getCollection[] = strtolower("get{$collection}s"); $addCollection[] = strtolower("add{$collection}"); } $styles = array('Paragraph', 'Font', 'Table', 'Numbering', 'Link', 'Title'); foreach ($styles as $style) { $addStyle[] = strtolower("add{$style}Style"); } // Run get collection method if (in_array($function, $getCollection)) { $key = ucfirst(str_replace('get', '', $function)); return $this->collections[$key]; } // Run add collection item method if (in_array($function, $addCollection)) { $key = ucfirst(str_replace('add', '', $function) . 's'); /** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collectionObject */ $collectionObject = $this->collections[$key]; return $collectionObject->addItem(isset($args[0]) ? $args[0] : null); } // Run add style method if (in_array($function, $addStyle)) { return forward_static_call_array(array('PhpOffice\\PhpWord\\Style', $function), $args); } // Exception throw new \BadMethodCallException("Method $function is not defined."); } /** * Get document properties object * * @return \PhpOffice\PhpWord\Metadata\DocInfo */ public function getDocInfo() { return $this->metadata['DocInfo']; } /** * Get protection * * @return \PhpOffice\PhpWord\Metadata\Protection * @since 0.12.0 * @deprecated Get the Document protection from PhpWord->getSettings()->getDocumentProtection(); * @codeCoverageIgnore */ public function getProtection() { return $this->getSettings()->getDocumentProtection(); } /** * Get compatibility * * @return \PhpOffice\PhpWord\Metadata\Compatibility * @since 0.12.0 */ public function getCompatibility() { return $this->metadata['Compatibility']; } /** * Get compatibility * * @return \PhpOffice\PhpWord\Metadata\Settings * @since 0.14.0 */ public function getSettings() { return $this->metadata['Settings']; } /** * Get all sections * * @return \PhpOffice\PhpWord\Element\Section[] */ public function getSections() { return $this->sections; } /** * Returns the section at the requested position * * @param int $index * @return \PhpOffice\PhpWord\Element\Section|null */ public function getSection($index) { if (array_key_exists($index, $this->sections)) { return $this->sections[$index]; } return null; } /** * Create new section * * @param array $style * @return \PhpOffice\PhpWord\Element\Section */ public function addSection($style = null) { $section = new Section(count($this->sections) + 1, $style); $section->setPhpWord($this); $this->sections[] = $section; return $section; } /** * Sorts the sections using the callable passed * * @see http://php.net/manual/en/function.usort.php for usage * @param callable $sorter */ public function sortSections($sorter) { usort($this->sections, $sorter); } /** * Get default font name * * @return string */ public function getDefaultFontName() { return Settings::getDefaultFontName(); } /** * Set default font name. * * @param string $fontName */ public function setDefaultFontName($fontName) { Settings::setDefaultFontName($fontName); } /** * Get default font size * * @return int */ public function getDefaultFontSize() { return Settings::getDefaultFontSize(); } /** * Set default font size. * * @param int $fontSize */ public function setDefaultFontSize($fontSize) { Settings::setDefaultFontSize($fontSize); } /** * Set default paragraph style definition to styles.xml * * @param array $styles Paragraph style definition * @return \PhpOffice\PhpWord\Style\Paragraph */ public function setDefaultParagraphStyle($styles) { return Style::setDefaultParagraphStyle($styles); } /** * Load template by filename * * @deprecated 0.12.0 Use `new TemplateProcessor($documentTemplate)` instead. * * @param string $filename Fully qualified filename * * @throws \PhpOffice\PhpWord\Exception\Exception * * @return TemplateProcessor * * @codeCoverageIgnore */ public function loadTemplate($filename) { if (file_exists($filename)) { return new TemplateProcessor($filename); } throw new Exception("Template file {$filename} not found."); } /** * Save to file or download * * All exceptions should already been handled by the writers * * @param string $filename * @param string $format * @param bool $download * @return bool */ public function save($filename, $format = 'Word2007', $download = false) { $mime = array( 'Word2007' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'ODText' => 'application/vnd.oasis.opendocument.text', 'RTF' => 'application/rtf', 'HTML' => 'text/html', 'PDF' => 'application/pdf', ); $writer = IOFactory::createWriter($this, $format); if ($download === true) { header('Content-Description: File Transfer'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Content-Type: ' . $mime[$format]); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Expires: 0'); $filename = 'php://output'; // Change filename to force download } $writer->save($filename); return true; } /** * Create new section * * @deprecated 0.10.0 * * @param array $settings * * @return \PhpOffice\PhpWord\Element\Section * * @codeCoverageIgnore */ public function createSection($settings = null) { return $this->addSection($settings); } /** * Get document properties object * * @deprecated 0.12.0 * * @return \PhpOffice\PhpWord\Metadata\DocInfo * * @codeCoverageIgnore */ public function getDocumentProperties() { return $this->getDocInfo(); } /** * Set document properties object * * @deprecated 0.12.0 * * @param \PhpOffice\PhpWord\Metadata\DocInfo $documentProperties * * @return self * * @codeCoverageIgnore */ public function setDocumentProperties($documentProperties) { $this->metadata['Document'] = $documentProperties; return $this; } }