Skip to content

Commit e7540c0

Browse files
committed
Add $nestedLevel to elements
1 parent 022cdeb commit e7540c0

File tree

4 files changed

+76
-38
lines changed

4 files changed

+76
-38
lines changed

src/PhpWord/Element/AbstractContainer.php

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,18 @@ protected function addElement($elementName)
6565
$reflection = new \ReflectionClass($elementClass);
6666
$elementArgs = $args;
6767
array_shift($elementArgs); // Shift an element off the beginning of array: the $elementName
68+
6869
/** @var \PhpOffice\PhpWord\Element\AbstractElement $element Type hint */
6970
$element = $reflection->newInstanceArgs($elementArgs);
7071

72+
// Set nested level
73+
if ($this->container == 'Cell') {
74+
$element->setNestedLevel($this->getNestedLevel() + 1);
75+
} else {
76+
$element->setNestedLevel($this->getNestedLevel());
77+
}
78+
79+
7180
// Set relation Id for media collection
7281
$mediaContainer = $this->getMediaContainer();
7382
if (in_array($elementName, array('Link', 'Image', 'Object'))) {
@@ -143,32 +152,6 @@ public function addTextRun($paragraphStyle = null)
143152
return $this->addElement('TextRun', $paragraphStyle);
144153
}
145154

146-
/**
147-
* Add field element
148-
*
149-
* @param string $type
150-
* @param array $properties
151-
* @param array $options
152-
* @return \PhpOffice\PhpWord\Element\Field
153-
*/
154-
public function addField($type = null, $properties = array(), $options = array())
155-
{
156-
return $this->addElement('Field', $type, $properties, $options);
157-
}
158-
159-
/**
160-
* Add line element
161-
*
162-
* @param mixed $lineStyle
163-
* @return \PhpOffice\PhpWord\Element\Line
164-
*/
165-
public function addLine($lineStyle = null)
166-
{
167-
return $this->addElement('Line', $lineStyle);
168-
169-
}
170-
171-
172155
/**
173156
* Add link element
174157
*
@@ -323,6 +306,31 @@ public function addTextBox($style = null)
323306
return $this->addElement('TextBox', $style);
324307
}
325308

309+
/**
310+
* Add field element
311+
*
312+
* @param string $type
313+
* @param array $properties
314+
* @param array $options
315+
* @return \PhpOffice\PhpWord\Element\Field
316+
*/
317+
public function addField($type = null, $properties = array(), $options = array())
318+
{
319+
return $this->addElement('Field', $type, $properties, $options);
320+
}
321+
322+
/**
323+
* Add line element
324+
*
325+
* @param mixed $lineStyle
326+
* @return \PhpOffice\PhpWord\Element\Line
327+
*/
328+
public function addLine($lineStyle = null)
329+
{
330+
return $this->addElement('Line', $lineStyle);
331+
332+
}
333+
326334
/**
327335
* Check if a method is allowed for the current container
328336
*

src/PhpWord/Element/AbstractElement.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ abstract class AbstractElement
8484
*/
8585
protected $relationId;
8686

87+
/**
88+
* Depth of table container nested level; Primarily used for RTF writer/reader
89+
*
90+
* 0 = Not in a table; 1 = in a table; 2 = in a table inside another table, etc.
91+
*
92+
* @var int
93+
*/
94+
private $nestedLevel = 0;
95+
8796
/**
8897
* Get PhpWord
8998
*
@@ -197,11 +206,31 @@ public function getRelationId()
197206
/**
198207
* Set relation Id
199208
*
200-
* @param int $rId
209+
* @param int $value
210+
*/
211+
public function setRelationId($value)
212+
{
213+
$this->relationId = $value;
214+
}
215+
216+
/**
217+
* Get nested level
218+
*
219+
* @return int
220+
*/
221+
public function getNestedLevel()
222+
{
223+
return $this->nestedLevel;
224+
}
225+
226+
/**
227+
* Set nested level
228+
*
229+
* @param int $value
201230
*/
202-
public function setRelationId($rId)
231+
public function setNestedLevel($value)
203232
{
204-
$this->relationId = $rId;
233+
$this->nestedLevel = $value;
205234
}
206235

207236
/**

src/PhpWord/Element/Row.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function addCell($width = null, $style = null)
7171
$cell = new Cell($width, $style);
7272
$cell->setDocPart($this->getDocPart(), $this->getDocPartId());
7373
$cell->setPhpWord($this->phpWord);
74+
$cell->setNestedLevel($this->getNestedLevel());
7475
$this->cells[] = $cell;
7576

7677
return $cell;

src/PhpWord/Element/Table.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class Table extends AbstractElement
4545
*/
4646
private $width = null;
4747

48-
4948
/**
5049
* Create a new table
5150
*
@@ -68,6 +67,7 @@ public function addRow($height = null, $style = null)
6867
$row = new Row($height, $style);
6968
$row->setDocPart($this->getDocPart(), $this->getDocPartId());
7069
$row->setPhpWord($this->phpWord);
70+
$row->setNestedLevel($this->getNestedLevel());
7171
$this->rows[] = $row;
7272

7373
return $row;
@@ -110,23 +110,23 @@ public function getStyle()
110110
}
111111

112112
/**
113-
* Set table width
113+
* Get table width
114114
*
115-
* @param int $width
115+
* @return int
116116
*/
117-
public function setWidth($width)
117+
public function getWidth()
118118
{
119-
$this->width = $width;
119+
return $this->width;
120120
}
121121

122122
/**
123-
* Get table width
123+
* Set table width
124124
*
125-
* @return int
125+
* @param int $width
126126
*/
127-
public function getWidth()
127+
public function setWidth($width)
128128
{
129-
return $this->width;
129+
$this->width = $width;
130130
}
131131

132132
/**

0 commit comments

Comments
 (0)