Add $nestedLevel to elements

This commit is contained in:
Ivan Lanin 2014-05-31 09:29:09 +07:00
parent 022cdeb570
commit e7540c079e
4 changed files with 78 additions and 40 deletions

View File

@ -65,9 +65,18 @@ abstract class AbstractContainer extends AbstractElement
$reflection = new \ReflectionClass($elementClass); $reflection = new \ReflectionClass($elementClass);
$elementArgs = $args; $elementArgs = $args;
array_shift($elementArgs); // Shift an element off the beginning of array: the $elementName array_shift($elementArgs); // Shift an element off the beginning of array: the $elementName
/** @var \PhpOffice\PhpWord\Element\AbstractElement $element Type hint */ /** @var \PhpOffice\PhpWord\Element\AbstractElement $element Type hint */
$element = $reflection->newInstanceArgs($elementArgs); $element = $reflection->newInstanceArgs($elementArgs);
// Set nested level
if ($this->container == 'Cell') {
$element->setNestedLevel($this->getNestedLevel() + 1);
} else {
$element->setNestedLevel($this->getNestedLevel());
}
// Set relation Id for media collection // Set relation Id for media collection
$mediaContainer = $this->getMediaContainer(); $mediaContainer = $this->getMediaContainer();
if (in_array($elementName, array('Link', 'Image', 'Object'))) { if (in_array($elementName, array('Link', 'Image', 'Object'))) {
@ -143,32 +152,6 @@ abstract class AbstractContainer extends AbstractElement
return $this->addElement('TextRun', $paragraphStyle); return $this->addElement('TextRun', $paragraphStyle);
} }
/**
* Add field element
*
* @param string $type
* @param array $properties
* @param array $options
* @return \PhpOffice\PhpWord\Element\Field
*/
public function addField($type = null, $properties = array(), $options = array())
{
return $this->addElement('Field', $type, $properties, $options);
}
/**
* Add line element
*
* @param mixed $lineStyle
* @return \PhpOffice\PhpWord\Element\Line
*/
public function addLine($lineStyle = null)
{
return $this->addElement('Line', $lineStyle);
}
/** /**
* Add link element * Add link element
* *
@ -323,6 +306,31 @@ abstract class AbstractContainer extends AbstractElement
return $this->addElement('TextBox', $style); return $this->addElement('TextBox', $style);
} }
/**
* Add field element
*
* @param string $type
* @param array $properties
* @param array $options
* @return \PhpOffice\PhpWord\Element\Field
*/
public function addField($type = null, $properties = array(), $options = array())
{
return $this->addElement('Field', $type, $properties, $options);
}
/**
* Add line element
*
* @param mixed $lineStyle
* @return \PhpOffice\PhpWord\Element\Line
*/
public function addLine($lineStyle = null)
{
return $this->addElement('Line', $lineStyle);
}
/** /**
* Check if a method is allowed for the current container * Check if a method is allowed for the current container
* *

View File

@ -84,6 +84,15 @@ abstract class AbstractElement
*/ */
protected $relationId; protected $relationId;
/**
* Depth of table container nested level; Primarily used for RTF writer/reader
*
* 0 = Not in a table; 1 = in a table; 2 = in a table inside another table, etc.
*
* @var int
*/
private $nestedLevel = 0;
/** /**
* Get PhpWord * Get PhpWord
* *
@ -197,11 +206,31 @@ abstract class AbstractElement
/** /**
* Set relation Id * Set relation Id
* *
* @param int $rId * @param int $value
*/ */
public function setRelationId($rId) public function setRelationId($value)
{ {
$this->relationId = $rId; $this->relationId = $value;
}
/**
* Get nested level
*
* @return int
*/
public function getNestedLevel()
{
return $this->nestedLevel;
}
/**
* Set nested level
*
* @param int $value
*/
public function setNestedLevel($value)
{
$this->nestedLevel = $value;
} }
/** /**

View File

@ -71,6 +71,7 @@ class Row extends AbstractElement
$cell = new Cell($width, $style); $cell = new Cell($width, $style);
$cell->setDocPart($this->getDocPart(), $this->getDocPartId()); $cell->setDocPart($this->getDocPart(), $this->getDocPartId());
$cell->setPhpWord($this->phpWord); $cell->setPhpWord($this->phpWord);
$cell->setNestedLevel($this->getNestedLevel());
$this->cells[] = $cell; $this->cells[] = $cell;
return $cell; return $cell;

View File

@ -45,7 +45,6 @@ class Table extends AbstractElement
*/ */
private $width = null; private $width = null;
/** /**
* Create a new table * Create a new table
* *
@ -68,6 +67,7 @@ class Table extends AbstractElement
$row = new Row($height, $style); $row = new Row($height, $style);
$row->setDocPart($this->getDocPart(), $this->getDocPartId()); $row->setDocPart($this->getDocPart(), $this->getDocPartId());
$row->setPhpWord($this->phpWord); $row->setPhpWord($this->phpWord);
$row->setNestedLevel($this->getNestedLevel());
$this->rows[] = $row; $this->rows[] = $row;
return $row; return $row;
@ -109,16 +109,6 @@ class Table extends AbstractElement
return $this->style; return $this->style;
} }
/**
* Set table width
*
* @param int $width
*/
public function setWidth($width)
{
$this->width = $width;
}
/** /**
* Get table width * Get table width
* *
@ -129,6 +119,16 @@ class Table extends AbstractElement
return $this->width; return $this->width;
} }
/**
* Set table width
*
* @param int $width
*/
public function setWidth($width)
{
$this->width = $width;
}
/** /**
* Get column count * Get column count
* *