From d7c18fe4b8457c47bbb5c97ed2abf20b71977205 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 5 Apr 2014 00:08:00 +0700 Subject: [PATCH] Move OOXML specific feature from Media to Word2007\Base --- src/PhpWord/Container/Container.php | 98 ++++----- src/PhpWord/Container/Footer.php | 1 + src/PhpWord/Container/Header.php | 1 + src/PhpWord/Container/Section.php | 3 + src/PhpWord/Element/Cell.php | 3 +- src/PhpWord/Element/Element.php | 60 ++++++ src/PhpWord/Element/Object.php | 15 +- src/PhpWord/Element/Row.php | 20 +- src/PhpWord/Element/Table.php | 19 +- src/PhpWord/Element/TextRun.php | 4 +- src/PhpWord/Media.php | 23 +- src/PhpWord/Reader/Word2007.php | 4 +- src/PhpWord/Writer/Word2007.php | 5 +- src/PhpWord/Writer/Word2007/Base.php | 10 +- src/PhpWord/Writer/Word2007/ContentTypes.php | 215 +++++++------------ tests/PhpWord/Tests/Element/ObjectTest.php | 13 -- 16 files changed, 200 insertions(+), 294 deletions(-) diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index fd08785e..ec66a5a0 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -58,24 +58,6 @@ abstract class Container extends Element */ protected $elements = array(); - /** - * Document part type: section|header|footer - * - * Used by textrun and cell to determine where the element is located - * because it will affect the availability of other element, e.g. footnote - * will not be available when $docPart is header or footer. - * - * @var string - */ - protected $docPart = null; - - /** - * Document part Id - * - * @var int - */ - protected $docPartId; - /** * Relation Id * @@ -101,10 +83,11 @@ abstract class Container extends Element } $text = String::toUTF8($text); - $element = new Text($text, $fontStyle, $paragraphStyle); - $this->elements[] = $element; + $textObject = new Text($text, $fontStyle, $paragraphStyle); + $textObject->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->elements[] = $textObject; - return $element; + return $textObject; } /** @@ -117,15 +100,8 @@ abstract class Container extends Element { $this->checkValidity('textrun'); - if ($this->container == 'cell') { - $docPart = $this->docPart; - $docPartId = $this->docPartId; - } else { - $docPart = $this->container; - $docPartId = $this->containerId; - } - - $textRun = new TextRun($paragraphStyle, $docPart, $docPartId); + $textRun = new TextRun($paragraphStyle); + $textRun->setDocPart($this->getDocPart(), $this->getDocPartId()); $this->elements[] = $textRun; return $textRun; @@ -145,9 +121,8 @@ abstract class Container extends Element $this->checkValidity('link'); $elementDocPart = $this->checkElementDocPart(); - $linkSrc = String::toUTF8($linkSrc); - $linkName = String::toUTF8($linkName); - $link = new Link($linkSrc, $linkName, $fontStyle, $paragraphStyle); + $link = new Link(String::toUTF8($linkSrc), String::toUTF8($linkName), $fontStyle, $paragraphStyle); + $link->setDocPart($this->getDocPart(), $this->getDocPartId()); $rID = Media::addMediaElement($elementDocPart, 'link', $linkSrc); $link->setRelationId($rID); $this->elements[] = $link; @@ -167,14 +142,15 @@ abstract class Container extends Element { $this->checkValidity('title'); - $text = String::toUTF8($text); $styles = Style::getStyles(); if (array_key_exists('Heading_' . $depth, $styles)) { $style = 'Heading' . $depth; } else { $style = null; } + $text = String::toUTF8($text); $title = new Title($text, $depth, $style); + $title->setDocPart($this->getDocPart(), $this->getDocPartId()); $data = TOC::addTitle($text, $depth); $anchor = $data[0]; $bookmarkId = $data[1]; @@ -197,11 +173,11 @@ abstract class Container extends Element { $this->checkValidity('preservetext'); - $text = String::toUTF8($text); - $ptext = new PreserveText($text, $fontStyle, $paragraphStyle); - $this->elements[] = $ptext; + $preserveText = new PreserveText(String::toUTF8($text), $fontStyle, $paragraphStyle); + $preserveText->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->elements[] = $preserveText; - return $ptext; + return $preserveText; } /** @@ -216,7 +192,9 @@ abstract class Container extends Element $this->checkValidity('textbreak'); for ($i = 1; $i <= $count; $i++) { - $this->elements[] = new TextBreak($fontStyle, $paragraphStyle); + $textBreak = new TextBreak($fontStyle, $paragraphStyle); + $textBreak->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->elements[] = $textBreak; } } @@ -234,8 +212,8 @@ abstract class Container extends Element { $this->checkValidity('listitem'); - $text = String::toUTF8($text); - $listItem = new ListItem($text, $depth, $fontStyle, $styleList, $paragraphStyle); + $listItem = new ListItem(String::toUTF8($text), $depth, $fontStyle, $styleList, $paragraphStyle); + $listItem->setDocPart($this->getDocPart(), $this->getDocPartId()); $this->elements[] = $listItem; return $listItem; @@ -251,7 +229,7 @@ abstract class Container extends Element { $this->checkValidity('table'); - $table = new Table($this->container, $this->containerId, $style); + $table = new Table($this->getDocPart(), $this->getDocPartId(), $style); $this->elements[] = $table; return $table; @@ -271,11 +249,10 @@ abstract class Container extends Element $elementDocPart = $this->checkElementDocPart(); $image = new Image($src, $style, $isWatermark); + $image->setDocPart($this->getDocPart(), $this->getDocPartId()); if (!is_null($image->getSource())) { $rID = Media::addMediaElement($elementDocPart, 'image', $src, $image); - if (is_int($rID)) { - $image->setRelationId($rID); - } + $image->setRelationId($rID); $this->elements[] = $image; return $image; } else { @@ -299,6 +276,7 @@ abstract class Container extends Element $elementDocPart = $this->checkElementDocPart(); $object = new Object($src, $style); + $object->setDocPart($this->getDocPart(), $this->getDocPartId()); if (!is_null($object->getSource())) { $inf = pathinfo($src); $ext = $inf['extension']; @@ -306,15 +284,10 @@ abstract class Container extends Element $ext = substr($ext, 0, -1); } $icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png"); - $rIDimg = Media::addMediaElement($elementDocPart, 'image', $icon, new Image($icon)); - $data = Media::addMediaElement($elementDocPart, 'object', $src); - $rID = $data[0]; - $objectId = $data[1]; + $rID = Media::addMediaElement($elementDocPart, 'object', $src); $object->setRelationId($rID); - $object->setObjectId($objectId); - if (is_int($rIDimg)) { - $object->setImageRelationId($rIDimg); - } + $rIDimg = Media::addMediaElement($elementDocPart, 'image', $icon, new Image($icon)); + $object->setImageRelationId($rIDimg); $this->elements[] = $object; return $object; } else { @@ -334,6 +307,7 @@ abstract class Container extends Element $footnote = new FootnoteElement($paragraphStyle); $refID = FootnoteCollection::addFootnoteElement($footnote); + $footnote->setDocPart($this->getDocPart(), $this->getDocPartId()); $footnote->setRelationId($refID); $this->elements[] = $footnote; @@ -353,17 +327,17 @@ abstract class Container extends Element { $this->checkValidity('checkbox'); - $name = String::toUTF8($name); - $text = String::toUTF8($text); - $element = new CheckBox($name, $text, $fontStyle, $paragraphStyle); - $this->elements[] = $element; + $checkBox = new CheckBox(String::toUTF8($name), String::toUTF8($text), $fontStyle, $paragraphStyle); + $checkBox->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->elements[] = $checkBox; - return $element; + return $checkBox; } /** * Get section number - * getFooterCount + * + * @return array */ public function getSectionId() { @@ -484,7 +458,7 @@ abstract class Container extends Element $containers = $rules[0]; $allowedDocParts = $rules[1]; foreach ($containers as $container) { - if ($this->container == $container && !in_array($this->docPart, $allowedDocParts)) { + if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) { throw new \BadMethodCallException(); } } @@ -499,8 +473,8 @@ abstract class Container extends Element private function checkElementDocPart() { $isCellTextrun = in_array($this->container, array('cell', 'textrun')); - $docPart = $isCellTextrun ? $this->docPart : $this->container; - $docPartId = $isCellTextrun ? $this->docPartId : $this->containerId; + $docPart = $isCellTextrun ? $this->getDocPart() : $this->container; + $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->containerId; $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); return $inHeaderFooter ? $docPart . $docPartId : $docPart; diff --git a/src/PhpWord/Container/Footer.php b/src/PhpWord/Container/Footer.php index 56f06383..440b3b4e 100755 --- a/src/PhpWord/Container/Footer.php +++ b/src/PhpWord/Container/Footer.php @@ -23,5 +23,6 @@ class Footer extends Container { $this->container = 'footer'; $this->containerId = $sectionId; + $this->setDocPart($this->container, $this->containerId); } } diff --git a/src/PhpWord/Container/Header.php b/src/PhpWord/Container/Header.php index ba31994d..39c6ca98 100755 --- a/src/PhpWord/Container/Header.php +++ b/src/PhpWord/Container/Header.php @@ -42,6 +42,7 @@ class Header extends Container { $this->container = 'header'; $this->containerId = $sectionId; + $this->setDocPart($this->container, $this->containerId); } /** diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index 5e39ee06..69edec41 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -52,6 +52,7 @@ class Section extends Container { $this->container = 'section'; $this->containerId = $sectionCount; + $this->setDocPart($this->container, $this->containerId); $this->settings = new Settings(); $this->setSettings($settings); } @@ -168,6 +169,7 @@ class Section extends Container /** * Create header * + * @return Header * @deprecated 0.9.2 * @codeCoverageIgnore */ @@ -179,6 +181,7 @@ class Section extends Container /** * Create footer * + * @return Footer * @deprecated 0.9.2 * @codeCoverageIgnore */ diff --git a/src/PhpWord/Element/Cell.php b/src/PhpWord/Element/Cell.php index 61e2fa4c..5e035fb7 100755 --- a/src/PhpWord/Element/Cell.php +++ b/src/PhpWord/Element/Cell.php @@ -42,8 +42,7 @@ class Cell extends Container public function __construct($docPart, $docPartId, $width = null, $style = null) { $this->container = 'cell'; - $this->docPart = $docPart; - $this->docPartId = $docPartId; + $this->setDocPart($docPart, $docPartId); $this->width = $width; $this->cellStyle = $this->setStyle(new CellStyle(), $style, true); } diff --git a/src/PhpWord/Element/Element.php b/src/PhpWord/Element/Element.php index b94a62c3..b3565775 100644 --- a/src/PhpWord/Element/Element.php +++ b/src/PhpWord/Element/Element.php @@ -16,6 +16,24 @@ namespace PhpOffice\PhpWord\Element; */ abstract class Element { + /** + * Document part type: section|header|footer + * + * Used by textrun and cell container to determine where the element is + * located because it will affect the availability of other element, + * e.g. footnote will not be available when $docPart is header or footer. + * + * @var string + */ + private $docPart = 'section'; + + /** + * Document part Id + * + * @var integer + */ + private $docPartId = 1; + /** * Set style value * @@ -39,4 +57,46 @@ abstract class Element return $style; } + + /** + * Set doc part + * + * @param string $docPart + * @param integer $docPartId + */ + public function setDocPart($docPart, $docPartId = 1) + { + $this->docPart = $docPart; + $this->docPartId = $docPartId; + } + + /** + * Get doc part + * + * @return string + */ + public function getDocPart() + { + return $this->docPart; + } + + /** + * Get doc part Id + * + * @return integer + */ + public function getDocPartId() + { + return $this->docPartId; + } + + /** + * Check if element is located in section doc part (as opposed to header/footer) + * + * @return boolean + */ + public function isInSection() + { + return ($this->docPart == 'section'); + } } diff --git a/src/PhpWord/Element/Object.php b/src/PhpWord/Element/Object.php index d885bfed..5716b7e6 100644 --- a/src/PhpWord/Element/Object.php +++ b/src/PhpWord/Element/Object.php @@ -44,13 +44,6 @@ class Object extends Element */ private $imageRelationId; - /** - * Object ID - * - * @var int - */ - private $objectId; - /** * Create a new Ole-Object Element * @@ -135,19 +128,23 @@ class Object extends Element * Get Object ID * * @return int + * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function getObjectId() { - return $this->objectId; + return $this->relationId + 1325353440; } /** * Set Object ID * * @param int $objId + * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function setObjectId($objId) { - $this->objectId = $objId; + $this->relationId = $objId; } } diff --git a/src/PhpWord/Element/Row.php b/src/PhpWord/Element/Row.php index 25357062..53990850 100644 --- a/src/PhpWord/Element/Row.php +++ b/src/PhpWord/Element/Row.php @@ -37,21 +37,6 @@ class Row extends Element */ private $cells = array(); - /** - * Table holder - * - * @var string - */ - private $docPart; - - /** - * Section/Header/Footer count - * - * @var int - */ - private $docPartId; - - /** * Create a new table row * @@ -62,8 +47,7 @@ class Row extends Element */ public function __construct($docPart, $docPartId, $height = null, $style = null) { - $this->docPart = $docPart; - $this->docPartId = $docPartId; + $this->setDocPart($docPart, $docPartId); $this->height = $height; $this->style = $this->setStyle(new RowStyle(), $style, true); } @@ -76,7 +60,7 @@ class Row extends Element */ public function addCell($width = null, $style = null) { - $cell = new Cell($this->docPart, $this->docPartId, $width, $style); + $cell = new Cell($this->getDocPart(), $this->getDocPartId(), $width, $style); $this->cells[] = $cell; return $cell; } diff --git a/src/PhpWord/Element/Table.php b/src/PhpWord/Element/Table.php index fb50caf2..5808a065 100644 --- a/src/PhpWord/Element/Table.php +++ b/src/PhpWord/Element/Table.php @@ -31,20 +31,6 @@ class Table extends Element */ private $rows = array(); - /** - * Table holder - * - * @var string - */ - private $docPart = null; - - /** - * Table holder count - * - * @var int - */ - private $docPartId; - /** * Table width * @@ -62,8 +48,7 @@ class Table extends Element */ public function __construct($docPart, $docPartId, $style = null) { - $this->docPart = $docPart; - $this->docPartId = $docPartId; + $this->setDocPart($docPart, $docPartId); $this->style = $this->setStyle(new TableStyle(), $style); } @@ -75,7 +60,7 @@ class Table extends Element */ public function addRow($height = null, $style = null) { - $row = new Row($this->docPart, $this->docPartId, $height, $style); + $row = new Row($this->getDocPart(), $this->getDocPartId(), $height, $style); $this->rows[] = $row; return $row; } diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 4a980424..794ad230 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -31,11 +31,9 @@ class TextRun extends Container * @param string $docPart section|header|footer * @param int $docPartId */ - public function __construct($paragraphStyle = null, $docPart = 'section', $docPartId = 1) + public function __construct($paragraphStyle = null) { $this->container = 'textrun'; - $this->docPart = $docPart; - $this->docPartId = $docPartId; $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); } diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index 0a24d12a..d354e399 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -23,13 +23,6 @@ class Media */ private static $media = array(); - /** - * ObjectID Counter - * - * @var integer - */ - private static $objectId = 1325353440; - /** * Add new media element * @@ -37,7 +30,7 @@ class Media * @param string $mediaType image|object|link * @param string $source * @param Image $image - * @return integer|array + * @return integer */ public static function addMediaElement($container, $mediaType, $source, Image $image = null) { @@ -84,18 +77,9 @@ class Media $mediaData['type'] = $mediaType; $mediaData['rID'] = $relId; self::$media[$container][$mediaId] = $mediaData; - if ($mediaType === 'object') { - return array($relId, ++self::$objectId); - } else { - return $relId; - } + return $relId; } else { - if ($mediaType === 'object') { - $relId = self::$media[$container][$mediaId]['rID']; - return array($relId, ++self::$objectId); - } else { - return self::$media[$container][$mediaId]['rID']; - } + return self::$media[$container][$mediaId]['rID']; } } @@ -246,7 +230,6 @@ class Media /** * Get Header Media Elements * - * @param string $prefix header|footer * @return array * @deprecated 0.9.2 * @codeCoverageIgnore diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 175d7fd3..f8add95a 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -428,8 +428,8 @@ class Word2007 extends Reader implements IReader * Return item of array * * @param array $array - * @param mixed $key - * @return mixed|null + * @param integer $key + * @return string */ private static function arrayItem($array, $key = 0) { diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 352bb7ca..084deb8d 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -250,7 +250,7 @@ class Word2007 extends Writer implements IWriter * Check content types * * @param mixed $objZip - * @param mixed $element + * @param mixed $elements */ private function addFilesToPackage($objZip, $elements) { @@ -277,6 +277,9 @@ class Word2007 extends Writer implements IWriter /** * Add header/footer media elements + * + * @param mixed $objZip + * @param string $docPart */ private function addHeaderFooterMedia($objZip, $docPart) { diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 10cabe49..762bc7ea 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -93,7 +93,7 @@ class Base extends WriterPart */ protected function writeLink(XMLWriter $xmlWriter, Link $link, $withoutP = false) { - $rID = $link->getRelationId(); + $rID = $link->getRelationId() + ($link->isInSection() ? 6 : 0); $linkName = $link->getLinkName(); if (is_null($linkName)) { $linkName = $link->getLinkSrc(); @@ -417,7 +417,7 @@ class Base extends WriterPart */ protected function writeImage(XMLWriter $xmlWriter, Image $image, $withoutP = false) { - $rId = $image->getRelationId(); + $rId = $image->getRelationId() + ($image->isInSection() ? 6 : 0); $style = $image->getStyle(); $width = $style->getWidth(); @@ -550,10 +550,10 @@ class Base extends WriterPart */ protected function writeObject(XMLWriter $xmlWriter, Object $object, $withoutP = false) { - $rIdObject = $object->getRelationId(); - $rIdImage = $object->getImageRelationId(); + $rIdObject = $object->getRelationId() + ($object->isInSection() ? 6 : 0); + $rIdImage = $object->getImageRelationId() + ($object->isInSection() ? 6 : 0); $shapeId = md5($rIdObject . '_' . $rIdImage); - $objectId = $object->getObjectId(); + $objectId = $object->getRelationId() + 1325353440; $style = $object->getStyle(); $align = $style->getAlign(); diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 98bf4e08..9e6853d0 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -26,173 +26,76 @@ class ContentTypes extends WriterPart */ public function writeContentTypes($imageTypes, $objectTypes, $cHdrs, $footers) { - // Create XML writer - $xmlWriter = $this->getXmlWriter(); - // XML header - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); + $OpenXMLPrefix = 'application/vnd.openxmlformats-'; + $WordMLPrefix = $OpenXMLPrefix . 'officedocument.wordprocessingml.'; - // Types - $xmlWriter->startElement('Types'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); + $defaults = array( + 'rels' => $OpenXMLPrefix . 'package.relationships+xml', + 'xml' => 'application/xml', - // Rels - $this->writeDefaultContentType( - $xmlWriter, - 'rels', - 'application/vnd.openxmlformats-package.relationships+xml' ); - - // XML - $this->writeDefaultContentType( - $xmlWriter, - 'xml', - 'application/xml' - ); - - // Add media content-types - foreach ($imageTypes as $key => $value) { - $this->writeDefaultContentType($xmlWriter, $key, $value); + if (is_array($imageTypes)) { + $defaults = array_merge($defaults, $imageTypes); } - - // Add embedding content-types if (count($objectTypes) > 0) { - $this->writeDefaultContentType( - $xmlWriter, - 'bin', - 'application/vnd.openxmlformats-officedocument.oleObject' - ); + $defaults['bin'] = $OpenXMLPrefix . 'officedocument.oleObject'; } - - // DocProps - $this->writeOverrideContentType( - $xmlWriter, - '/docProps/app.xml', - 'application/vnd.openxmlformats-officedocument.extended-properties+xml' + $overrides = array( + '/docProps/core.xml' => $OpenXMLPrefix . 'package.core-properties+xml', + '/docProps/app.xml' => $OpenXMLPrefix . 'officedocument.extended-properties+xml', + '/word/document.xml' => $WordMLPrefix . 'document.main+xml', + '/word/styles.xml' => $WordMLPrefix . 'styles+xml', + '/word/numbering.xml' => $WordMLPrefix . 'numbering+xml', + '/word/settings.xml' => $WordMLPrefix . 'settings+xml', + '/word/theme/theme1.xml' => $OpenXMLPrefix . 'officedocument.theme+xml', + '/word/webSettings.xml' => $WordMLPrefix . 'webSettings+xml', + '/word/fontTable.xml' => $WordMLPrefix . 'fontTable+xml', + '/word/footnotes.xml' => $WordMLPrefix . 'footnotes+xml', ); - - $this->writeOverrideContentType( - $xmlWriter, - '/docProps/core.xml', - 'application/vnd.openxmlformats-package.core-properties+xml' - ); - - // Document - $this->writeOverrideContentType( - $xmlWriter, - '/word/document.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' - ); - - // Styles - $this->writeOverrideContentType( - $xmlWriter, - '/word/styles.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml' - ); - - // Numbering - $this->writeOverrideContentType( - $xmlWriter, - '/word/numbering.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml' - ); - - // Settings - $this->writeOverrideContentType( - $xmlWriter, - '/word/settings.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml' - ); - - // Theme1 - $this->writeOverrideContentType( - $xmlWriter, - '/word/theme/theme1.xml', - 'application/vnd.openxmlformats-officedocument.theme+xml' - ); - - // WebSettings - $this->writeOverrideContentType( - $xmlWriter, - '/word/webSettings.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml' - ); - - // Font Table - $this->writeOverrideContentType( - $xmlWriter, - '/word/fontTable.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml' - ); - - // Footnotes - $this->writeOverrideContentType( - $xmlWriter, - '/word/footnotes.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml' - ); - for ($i = 1; $i <= $cHdrs; $i++) { - $this->writeOverrideContentType( - $xmlWriter, - '/word/header' . $i . '.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml' - ); + $overrides["/word/header{$i}.xml"] = $WordMLPrefix . 'header+xml'; } - for ($i = 1; $i <= count($footers); $i++) { if (!is_null($footers[$i])) { - $this->writeOverrideContentType( - $xmlWriter, - '/word/footer' . $i . '.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml' - ); + $overrides["/word/footer{$i}.xml"] = $WordMLPrefix . 'footer+xml'; } } + $xmlWriter = $this->getXmlWriter(); + $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); + $xmlWriter->startElement('Types'); + $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); + foreach ($defaults as $key => $value) + { + $this->writeContentType($xmlWriter, true, $key, $value); + } + foreach ($overrides as $key => $value) + { + $this->writeContentType($xmlWriter, false, $key, $value); + } $xmlWriter->endElement(); - // Return return $xmlWriter->getData(); } /** - * Write Default XML element + * Write content types element * - * @param XMLWriter $xmlWriter XML Writer - * @param string $pPartname Part name - * @param string $pContentType Content type + * @param XMLWriter $xmlWriter XML Writer + * @param boolean $isDefault + * @param string $partName Part name + * @param string $contentType Content type * @throws Exception */ - private function writeDefaultContentType(XMLWriter $xmlWriter, $pPartname = '', $pContentType = '') + private function writeContentType(XMLWriter $xmlWriter, $isDefault, $partName = '', $contentType = '') { - if ($pPartname != '' && $pContentType != '') { - // Write content type - $xmlWriter->startElement('Default'); - $xmlWriter->writeAttribute('Extension', $pPartname); - $xmlWriter->writeAttribute('ContentType', $pContentType); - $xmlWriter->endElement(); - } else { - throw new Exception("Invalid parameters passed."); - } - } - - /** - * Write Override XML element - * - * @param XMLWriter $xmlWriter - * @param string $pPartname Part name - * @param string $pContentType Content type - * @throws Exception - */ - private function writeOverrideContentType(XMLWriter $xmlWriter, $pPartname = '', $pContentType = '') - { - if ($pPartname != '' && $pContentType != '') { - // Write content type - $xmlWriter->startElement('Override'); - $xmlWriter->writeAttribute('PartName', $pPartname); - $xmlWriter->writeAttribute('ContentType', $pContentType); + if ($partName != '' && $contentType != '') { + $element = $isDefault ? 'Default' : 'Override'; + $partAttribute = $isDefault ? 'Extension' : 'PartName'; + $xmlWriter->startElement($element); + $xmlWriter->writeAttribute($partAttribute, $partName); + $xmlWriter->writeAttribute('ContentType', $contentType); $xmlWriter->endElement(); } else { throw new Exception("Invalid parameters passed."); @@ -215,4 +118,32 @@ class ContentTypes extends WriterPart throw new Exception("File $pFile does not exist"); } } + + /** + * Write Default XML element + * + * @param XMLWriter $xmlWriter + * @param string $partName Part name + * @param string $contentType Content type + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + private function writeDefaultContentType(XMLWriter $xmlWriter, $partName = '', $contentType = '') + { + $this->writeContentType($xmlWriter, true, $partName, $contentType); + } + + /** + * Write Override XML element + * + * @param XMLWriter $xmlWriter + * @param string $partName Part name + * @param string $contentType Content type + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + private function writeOverrideContentType(XMLWriter $xmlWriter, $partName = '', $contentType = '') + { + $this->writeContentType($xmlWriter, false, $partName, $contentType); + } } diff --git a/tests/PhpWord/Tests/Element/ObjectTest.php b/tests/PhpWord/Tests/Element/ObjectTest.php index 22b4787f..0f5f191a 100644 --- a/tests/PhpWord/Tests/Element/ObjectTest.php +++ b/tests/PhpWord/Tests/Element/ObjectTest.php @@ -83,17 +83,4 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $oObject->setImageRelationId($iVal); $this->assertEquals($oObject->getImageRelationId(), $iVal); } - - /** - * Set/get object relation Id - */ - public function testObjectId() - { - $src = __DIR__ . "/../_files/documents/sheet.xls"; - $oObject = new Object($src); - - $iVal = rand(1, 1000); - $oObject->setObjectId($iVal); - $this->assertEquals($oObject->getObjectId(), $iVal); - } }