From 297eeaadf0f7fd6f36db34c81cfc1b4e23fec5b1 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 11 Apr 2014 17:59:48 +0700 Subject: [PATCH] Documentation for new ListItem feature --- docs/elements.rst | 38 ++++++++++++++++++++++++++ src/PhpWord/Reader/Word2007.php | 20 ++++++++++---- src/PhpWord/Style.php | 20 +++++++------- src/PhpWord/Style/ListItem.php | 6 ++-- src/PhpWord/Style/NumberingLevel.php | 2 +- src/PhpWord/Template.php | 13 --------- src/PhpWord/Writer/Word2007/Styles.php | 2 -- 7 files changed, 66 insertions(+), 35 deletions(-) diff --git a/docs/elements.rst b/docs/elements.rst index 62ee007f..b6842b65 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -207,10 +207,14 @@ Lists To add a list item use the function ``addListItem``. +Basic usage: + .. code-block:: php $section->addListItem($text, [$depth], [$fontStyle], [$listStyle], [$paragraphStyle]); +Parameters: + - ``$text`` Text that appears in the document. - ``$depth`` Depth of list item. - ``$fontStyle`` See "Font style" section. @@ -219,6 +223,40 @@ To add a list item use the function ``addListItem``. PHPWord\_Style\_ListItem. - ``$paragraphStyle`` See "Paragraph style" section. +Advanced usage: + +You can also create your own numbering style by changing the ``$listStyle`` parameter +with the name of your numbering style. + +.. code-block:: php + + $phpWord->addNumberingStyle( + 'multilevel', + array('type' => 'multilevel', 'levels' => array( + array('format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360), + array('format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720), + ) + ) + ); + $section->addListItem('List Item I', 0, null, 'multilevel'); + $section->addListItem('List Item I.a', 1, null, 'multilevel'); + $section->addListItem('List Item I.b', 1, null, 'multilevel'); + $section->addListItem('List Item II', 0, null, 'multilevel'); + +Level styles: + +- ``start`` Starting value +- ``format`` Numbering format bullet|decimal|upperRoman|lowerRoman|upperLetter|lowerLetter +- ``restart`` Restart numbering level symbol +- ``suffix`` Content between numbering symbol and paragraph text tab|space|nothing +- ``text`` Numbering level text e.g. %1 for nonbullet or bullet character +- ``align`` Numbering symbol align left|center|right|both +- ``left`` See paragraph style +- ``hanging`` See paragraph style +- ``tabPos`` See paragraph style +- ``font`` Font name +- ``hint`` See font style + Tables ------ diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index e03d28a0..4ff84c3f 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -221,9 +221,11 @@ class Word2007 extends AbstractReader implements ReaderInterface // Section properties if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) { $settingsNode = $xmlReader->getElement('w:pPr/w:sectPr', $node); - $settings = $this->readSectionStyle($xmlReader, $settingsNode); - $section->setSettings($settings); - $this->readHeaderFooter($filename, $settings, $section); + if (!is_null($settingsNode)) { + $settings = $this->readSectionStyle($xmlReader, $settingsNode); + $section->setSettings($settings); + $this->readHeaderFooter($filename, $settings, $section); + } $section = $this->phpWord->addSection(); } break; @@ -268,7 +270,9 @@ class Word2007 extends AbstractReader implements ReaderInterface $pStyle = $this->readParagraphStyle($xmlReader, $node); $fStyle = $this->readFontStyle($xmlReader, $node); if (empty($fStyle)) { - $this->phpWord->addParagraphStyle($name, $pStyle); + if (is_array($pStyle)) { + $this->phpWord->addParagraphStyle($name, $pStyle); + } } else { $this->phpWord->addFontStyle($name, $fStyle, $pStyle); } @@ -634,8 +638,9 @@ class Word2007 extends AbstractReader implements ReaderInterface } elseif ($rowNode->nodeName == 'w:tc') { // Cell $cellWidth = $xmlReader->getAttribute('w:w', $rowNode, 'w:tcPr/w:tcW'); $cellStyle = null; - if ($xmlReader->elementExists('w:tcPr', $rowNode)) { - $cellStyle = $this->readCellStyle($xmlReader, $xmlReader->getElement('w:tcPr', $rowNode)); + $cellStyleNode = $xmlReader->getElement('w:tcPr', $rowNode); + if (!is_null($cellStyleNode)) { + $cellStyle = $this->readCellStyle($xmlReader, $cellStyleNode); } $cell = $row->addCell($cellWidth, $cellStyle); @@ -786,6 +791,9 @@ class Word2007 extends AbstractReader implements ReaderInterface if ($domNode->nodeName == 'w:hyperlink') { $domNode = $xmlReader->getElement('w:r', $domNode); } + if (is_null($domNode)) { + return $style; + } if ($xmlReader->elementExists('w:rPr', $domNode)) { if ($xmlReader->elementExists('w:rPr/w:rStyle', $domNode)) { $style = $xmlReader->getAttribute('w:val', $domNode, 'w:rPr/w:rStyle'); diff --git a/src/PhpWord/Style.php b/src/PhpWord/Style.php index eb01786c..084d7131 100755 --- a/src/PhpWord/Style.php +++ b/src/PhpWord/Style.php @@ -34,7 +34,7 @@ class Style */ public static function addParagraphStyle($styleName, $styles) { - self::setStyleValues($styleName, $styles, new Paragraph()); + self::setStyleValues($styleName, new Paragraph(), $styles); } /** @@ -46,7 +46,7 @@ class Style */ public static function addFontStyle($styleName, $styleFont, $styleParagraph = null) { - self::setStyleValues($styleName, $styleFont, new Font('text', $styleParagraph)); + self::setStyleValues($styleName, new Font('text', $styleParagraph), $styleFont); } /** @@ -57,7 +57,7 @@ class Style */ public static function addLinkStyle($styleName, $styles) { - self::setStyleValues($styleName, $styles, new Font('link')); + self::setStyleValues($styleName, new Font('link'), $styles); } /** @@ -65,11 +65,11 @@ class Style * * @param string $styleName * @param array $styleTable - * @param array $styleFirstRow + * @param array|null $styleFirstRow */ public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null) { - self::setStyleValues($styleName, null, new Table($styleTable, $styleFirstRow)); + self::setStyleValues($styleName, new Table($styleTable, $styleFirstRow), null); } /** @@ -81,7 +81,7 @@ class Style */ public static function addTitleStyle($titleCount, $styleFont, $styleParagraph = null) { - self::setStyleValues("Heading_{$titleCount}", $styleFont, new Font('title', $styleParagraph)); + self::setStyleValues("Heading_{$titleCount}", new Font('title', $styleParagraph), $styleFont); } /** @@ -94,7 +94,7 @@ class Style */ public static function addNumberingStyle($styleName, $styleValues) { - self::setStyleValues($styleName, $styleValues, new Numbering()); + self::setStyleValues($styleName, new Numbering(), $styleValues); } /** @@ -156,13 +156,13 @@ class Style * Set style values and put it to static style collection * * @param string $styleName - * @param array $styleValues * @param Paragraph|Font|Table|Numbering $styleObject + * @param array|null $styleValues */ - private static function setStyleValues($styleName, $styleValues, $styleObject) + private static function setStyleValues($styleName, $styleObject, $styleValues = null) { if (!array_key_exists($styleName, self::$styles)) { - if (is_array($styleValues)) { + if (!is_null($styleValues) && is_array($styleValues)) { foreach ($styleValues as $key => $value) { $styleObject->setStyleValue($key, $value); } diff --git a/src/PhpWord/Style/ListItem.php b/src/PhpWord/Style/ListItem.php index 93a3bb07..f9e5e947 100644 --- a/src/PhpWord/Style/ListItem.php +++ b/src/PhpWord/Style/ListItem.php @@ -10,7 +10,6 @@ namespace PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style; -use PhpOffice\PhpWord\Style\Numbering; /** * List item style @@ -92,7 +91,7 @@ class ListItem extends AbstractStyle /** * Get numbering style name * - * @return integer + * @return string */ public function getNumStyle() { @@ -108,7 +107,7 @@ class ListItem extends AbstractStyle { $this->numStyle = $value; $numStyleObject = Style::getStyle($this->numStyle); - if (!is_null($numStyleObject)) { + if ($numStyleObject instanceof Numbering) { $this->numId = $numStyleObject->getIndex(); $numStyleObject->setNumId($this->numId); } @@ -234,6 +233,7 @@ class ListItem extends AbstractStyle // Populate style and register to global Style register $style = $listTypeStyles[$this->listType]; foreach ($style['levels'] as $key => $value) { + $level = array(); $levelProperties = explode(', ', $value); $level['level'] = $key; for ($i = 0; $i < count($properties); $i++) { diff --git a/src/PhpWord/Style/NumberingLevel.php b/src/PhpWord/Style/NumberingLevel.php index 77b63b40..2979f0c2 100644 --- a/src/PhpWord/Style/NumberingLevel.php +++ b/src/PhpWord/Style/NumberingLevel.php @@ -115,7 +115,7 @@ class NumberingLevel extends AbstractStyle */ public function getLevel() { - return $level->level; + return $this->level; } /** diff --git a/src/PhpWord/Template.php b/src/PhpWord/Template.php index 9f71d306..65ffd75f 100644 --- a/src/PhpWord/Template.php +++ b/src/PhpWord/Template.php @@ -421,17 +421,4 @@ class Template } return substr($this->documentXML, $startPosition, ($endPosition - $startPosition)); } - - /** - * Delete a block of text - * - * @param string $blockname - * @param string $replacement - * @deprecated - * @codeCoverageIgnore - */ - public function deleteTemplateBlock($blockname, $replacement = '') - { - $this->deleteBlock($blockname); - } } diff --git a/src/PhpWord/Writer/Word2007/Styles.php b/src/PhpWord/Writer/Word2007/Styles.php index 14a54b2d..ae784982 100644 --- a/src/PhpWord/Writer/Word2007/Styles.php +++ b/src/PhpWord/Writer/Word2007/Styles.php @@ -15,7 +15,6 @@ use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style\Table; -use PhpOffice\PhpWord\Style\Numbering; /** * Word2007 styles part writer @@ -54,7 +53,6 @@ class Styles extends Base if ($styleName == 'Normal') { continue; } - $styleClass = str_replace('PhpOffice\\PhpWord\\Style\\', '', get_class($style)); // Font style if ($style instanceof Font) {