Merge branch 'ListItemRun' of github.com:basjan/PHPWord into #234-#235-basjan

This commit is contained in:
Ivan Lanin 2014-05-11 17:58:16 +07:00
commit da77ba6a71
7 changed files with 350 additions and 5 deletions

View File

@ -54,6 +54,18 @@ $section->addListItem('List Item 6', 1, 'myOwnStyle', $predefinedMultilevel, 'P-
$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) {

View File

@ -106,7 +106,7 @@ abstract class AbstractContainer extends AbstractElement
$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 @@ abstract class AbstractContainer extends AbstractElement
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 @@ abstract class AbstractContainer extends AbstractElement
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 @@ abstract class AbstractContainer extends AbstractElement
'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 @@ abstract class AbstractContainer extends AbstractElement
*/
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');

View File

@ -0,0 +1,80 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;
use PhpOffice\PhpWord\Style\Paragraph;
/**
* List item element
*/
class ListItemRun extends TextRun
{
/**
* ListItem Style
*
* @var \PhpOffice\PhpWord\Style\ListItem
*/
private $style;
/**
* ListItem Depth
*
* @var int
*/
private $depth;
/**
* Create a new ListItem
*
* @param int $depth
* @param mixed $fontStyle
* @param array|string|null $listStyle
* @param mixed $paragraphStyle
*/
public function __construct($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
{
$this->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;
}
}

View File

@ -29,7 +29,7 @@ class TextRun extends AbstractContainer
*
* @var string|\PhpOffice\PhpWord\Style\Paragraph
*/
private $paragraphStyle;
protected $paragraphStyle;
/**
* Create new instance

View File

@ -41,7 +41,7 @@ class Container extends AbstractElement
$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();

View File

@ -0,0 +1,61 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* ListItemRun element writer
*
* @since 0.10.0
*/
class ListItemRun extends AbstractElement
{
/**
* Write list item element
*/
public function write()
{
$xmlWriter = $this->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
}
}

View File

@ -0,0 +1,171 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Element;
use PhpOffice\PhpWord\Element\ListItemRun;
use PhpOffice\PhpWord\PhpWord;
/**
* Test class for PhpOffice\PhpWord\Element\ListItemRun
*
* @runTestsInSeparateProcesses
*/
class ListItemRunTest extends \PHPUnit_Framework_TestCase
{
/**
* New instance
*/
public function testConstructNull()
{
$oListItemRun = new ListItemRun();
$this->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());
}
}