diff --git a/samples/Sample_14_ListItem.php b/samples/Sample_14_ListItem.php index cd22df8e06..892771bc62 100644 --- a/samples/Sample_14_ListItem.php +++ b/samples/Sample_14_ListItem.php @@ -54,6 +54,18 @@ $section->addListItem('List Item 7', 0, 'myOwnStyle', $predefinedMultilevel, 'P-Style'); $section->addTextBreak(2); +$section->addText('List with inline formatting.'); +$listItemRun = $section->addListItemRun(); +$listItemRun->addText('List item 1'); +$listItemRun->addText(' in bold', array('bold'=>true)); +$listItemRun = $section->addListItemRun(); +$listItemRun->addText('List item 2'); +$listItemRun->addText(' in italic', array('italic'=>true)); +$listItemRun = $section->addListItemRun(); +$listItemRun->addText('List item 3'); +$listItemRun->addText(' underlined', array('underline'=>'dash')); +$section->addTextBreak(2); + // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); if (!CLI) { diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 431a306624..2c14d9a3b1 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -106,7 +106,7 @@ public function addText($text, $fontStyle = null, $paragraphStyle = null, $eleme $elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName; // Reset paragraph style for footnote and textrun. They have their own - if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) { + if (in_array($this->container, array('textrun', 'footnote', 'endnote', 'listitemrun'))) { $paragraphStyle = null; } @@ -205,6 +205,26 @@ public function addListItem($text, $depth = 0, $fontStyle = null, $listStyle = n return $element; } + /** + * Add listitemrun element + * + * @param int $depth + * @param mixed $fontStyle + * @param mixed $listStyle + * @param mixed $paragraphStyle + * @return \PhpOffice\PhpWord\Element\ListItemRun + */ + public function addListItemRun($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null) + { + $this->checkValidity('ListItemRun'); + + $element = new ListItemRun($depth, $fontStyle, $listStyle, $paragraphStyle); + $element->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->addElement($element); + + return $element; + } + /** * Add table element * @@ -345,7 +365,7 @@ public function addTextBox($style = null) private function checkValidity($method) { // Valid containers for each element - $allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote', 'textbox'); + $allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote', 'textbox', 'listitemrun'); $validContainers = array( 'Text' => $allContainers, 'Link' => $allContainers, @@ -354,6 +374,7 @@ private function checkValidity($method) 'Object' => $allContainers, 'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'), 'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'), + 'ListItemRun' => array('section', 'header', 'footer', 'cell', 'textbox'), 'Table' => array('section', 'header', 'footer', 'textbox'), 'CheckBox' => array('section', 'header', 'footer', 'cell'), 'TextBox' => array('section', 'header', 'footer', 'cell'), @@ -395,7 +416,7 @@ private function checkValidity($method) */ private function checkElementDocPart() { - $inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox')); + $inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox', 'listitemrun')); $docPart = $inOtherPart ? $this->getDocPart() : $this->container; $docPartId = $inOtherPart ? $this->getDocPartId() : $this->sectionId; $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); diff --git a/src/PhpWord/Element/ListItemRun.php b/src/PhpWord/Element/ListItemRun.php new file mode 100644 index 0000000000..1a52bd8912 --- /dev/null +++ b/src/PhpWord/Element/ListItemRun.php @@ -0,0 +1,80 @@ +container = 'listitemrun'; + $this->depth = $depth; + + // Version >= 0.10.0 will pass numbering style name. Older version will use old method + if (!is_null($listStyle) && is_string($listStyle)) { + $this->style = new ListItemStyle($listStyle); + } else { + $this->style = $this->setStyle(new ListItemStyle(), $listStyle, true); + } + $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); + } + + /** + * Get ListItem style + */ + public function getStyle() + { + return $this->style; + } + + /** + * Get ListItem depth + */ + public function getDepth() + { + return $this->depth; + } +} diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index ecf5716b9a..6c8aa010db 100644 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -29,7 +29,7 @@ class TextRun extends AbstractContainer * * @var string|\PhpOffice\PhpWord\Style\Paragraph */ - private $paragraphStyle; + protected $paragraphStyle; /** * Create new instance diff --git a/src/PhpWord/Writer/Word2007/Element/Container.php b/src/PhpWord/Writer/Word2007/Element/Container.php index 384fbd5bb2..bf45e760fa 100644 --- a/src/PhpWord/Writer/Word2007/Element/Container.php +++ b/src/PhpWord/Writer/Word2007/Element/Container.php @@ -41,7 +41,7 @@ public function write() $xmlWriter = $this->getXmlWriter(); $container = $this->getElement(); $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); - $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; + $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')) ? true : false; // Loop through subelements $subelements = $container->getElements(); diff --git a/src/PhpWord/Writer/Word2007/Element/ListItemRun.php b/src/PhpWord/Writer/Word2007/Element/ListItemRun.php new file mode 100644 index 0000000000..4431ffe60e --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Element/ListItemRun.php @@ -0,0 +1,61 @@ +getXmlWriter(); + $element = $this->getElement(); + + $xmlWriter->startElement('w:p'); + + $xmlWriter->startElement('w:pPr'); + $paragraphStyle = $element->getParagraphStyle(); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle); + $styleWriter->setIsInline(true); + $styleWriter->write(); + + $xmlWriter->startElement('w:numPr'); + $xmlWriter->startElement('w:ilvl'); + $xmlWriter->writeAttribute('w:val', $element->getDepth()); + $xmlWriter->endElement(); // w:ilvl + $xmlWriter->startElement('w:numId'); + $xmlWriter->writeAttribute('w:val', $element->getStyle()->getNumId()); + $xmlWriter->endElement(); // w:numId + $xmlWriter->endElement(); // w:numPr + + $xmlWriter->endElement(); // w:pPr + + $containerWriter = new Container($xmlWriter, $element); + $containerWriter->write(); + + $xmlWriter->endElement(); // w:p + } +} diff --git a/tests/PhpWord/Tests/Element/ListItemRunTest.php b/tests/PhpWord/Tests/Element/ListItemRunTest.php new file mode 100644 index 0000000000..5b3f72c822 --- /dev/null +++ b/tests/PhpWord/Tests/Element/ListItemRunTest.php @@ -0,0 +1,171 @@ +assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); + $this->assertCount(0, $oListItemRun->getElements()); + $this->assertEquals($oListItemRun->getParagraphStyle(), null); + } + + /** + * New instance with string + */ + public function testConstructString() + { + $oListItemRun = new ListItemRun(0, null, null, 'pStyle'); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); + $this->assertCount(0, $oListItemRun->getElements()); + $this->assertEquals($oListItemRun->getParagraphStyle(), 'pStyle'); + } + + /** + * New instance with array + */ + public function testConstructArray() + { + $oListItemRun = new ListItemRun(0, null, null, array('spacing' => 100)); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); + $this->assertCount(0, $oListItemRun->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oListItemRun->getParagraphStyle()); + } + + /** + * Get style + */ + public function testStyle() + { + $oListItemRun = new ListItemRun( + 1, + null, + array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER) + ); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\ListItem', $oListItemRun->getStyle()); + $this->assertEquals( + $oListItemRun->getStyle()->getListType(), + \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER + ); + } + /** + * getDepth + */ + public function testDepth() + { + $iVal = rand(1, 1000); + $oListItemRun = new ListItemRun($iVal); + + $this->assertEquals($oListItemRun->getDepth(), $iVal); + } + + /** + * Add text + */ + public function testAddText() + { + $oListItemRun = new ListItemRun(); + $element = $oListItemRun->addText('text'); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); + $this->assertCount(1, $oListItemRun->getElements()); + $this->assertEquals($element->getText(), 'text'); + } + + /** + * Add text non-UTF8 + */ + public function testAddTextNotUTF8() + { + $oListItemRun = new ListItemRun(); + $element = $oListItemRun->addText(utf8_decode('ééé')); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); + $this->assertCount(1, $oListItemRun->getElements()); + $this->assertEquals($element->getText(), 'ééé'); + } + + /** + * Add link + */ + public function testAddLink() + { + $oListItemRun = new ListItemRun(); + $element = $oListItemRun->addLink('http://www.google.fr'); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); + $this->assertCount(1, $oListItemRun->getElements()); + $this->assertEquals($element->getTarget(), 'http://www.google.fr'); + } + + /** + * Add link with name + */ + public function testAddLinkWithName() + { + $oListItemRun = new ListItemRun(); + $element = $oListItemRun->addLink('http://www.google.fr', utf8_decode('ééé')); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); + $this->assertCount(1, $oListItemRun->getElements()); + $this->assertEquals($element->getTarget(), 'http://www.google.fr'); + $this->assertEquals($element->getText(), 'ééé'); + } + + /** + * Add text break + */ + public function testAddTextBreak() + { + $oListItemRun = new ListItemRun(); + $oListItemRun->addTextBreak(2); + + $this->assertCount(2, $oListItemRun->getElements()); + } + + /** + * Add image + */ + public function testAddImage() + { + $src = __DIR__ . "/../_files/images/earth.jpg"; + + $oListItemRun = new ListItemRun(); + $element = $oListItemRun->addImage($src); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); + $this->assertCount(1, $oListItemRun->getElements()); + } +}