Skip to content

Commit 30b224b

Browse files
authored
Word2007 parsing title formatting (#1297)
* Improve Title parsing - Title should be able to contain TextRun - Style 'Title' should be treated the same with as Heading - Add tests for Heading/Title reader * update the documentation and the changelog * PHP 7.2 build should not fail anymore * reduce dependencies versions * fix parsing of footnotes and endnotes * add method to remove an element from a section
1 parent 740e66a commit 30b224b

File tree

24 files changed

+539
-99
lines changed

24 files changed

+539
-99
lines changed

.scrutinizer.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
build:
2+
nodes:
3+
analysis:
4+
tests:
5+
override: [php-scrutinizer-run]
16
filter:
27
excluded_paths: [ 'vendor/*', 'tests/*', 'samples/*', 'src/PhpWord/Shared/PCLZip/*' ]
38

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ matrix:
1515
include:
1616
- php: 5.6
1717
env: COVERAGE=1
18-
allow_failures:
19-
- php: 7.2
2018

2119
cache:
2220
directories:
23-
- vendor
2421
- $HOME/.composer/cache
2522
- .php-cs.cache
2623

@@ -38,7 +35,7 @@ before_script:
3835
- if [ -z "$COVERAGE" ]; then phpenv config-rm xdebug.ini ; fi
3936
## Composer
4037
- composer self-update
41-
- composer install --prefer-source
38+
- travis_wait composer install --prefer-source
4239
## PHPDocumentor
4340
- mkdir -p build/docs
4441
- mkdir -p build/coverage

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ v0.15.0 (?? ??? 2018)
2323
- Fix parsing of `<w:br/>` tag. @troosan #1274
2424
- Bookmark are not writton as internal link in html writer @troosan #1263
2525
- It should be possible to add a Footnote in a ListItemRun @troosan #1287 #1287
26+
- Fix parsing of Heading and Title formating @troosan @gthomas2 #465
2627

2728
### Changed
2829
- Remove zend-stdlib dependency @Trainmaster #1284

docs/elements.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Titles
8989

9090
If you want to structure your document or build table of contents, you need titles or headings.
9191
To add a title to the document, use the ``addTitleStyle`` and ``addTitle`` method.
92+
If `depth` is 0, a Title will be inserted, otherwise a Heading1, Heading2, ...
9293

9394
.. code-block:: php
9495
@@ -98,7 +99,7 @@ To add a title to the document, use the ``addTitleStyle`` and ``addTitle`` metho
9899
- ``depth``.
99100
- ``$fontStyle``. See :ref:`font-style`.
100101
- ``$paragraphStyle``. See :ref:`paragraph-style`.
101-
- ``$text``. Text to be displayed in the document.
102+
- ``$text``. Text to be displayed in the document. This can be `string` or a `\PhpOffice\PhpWord\Element\TextRun`
102103

103104
It's necessary to add a title style to your document because otherwise the title won't be detected as a real title.
104105

samples/Sample_17_TitleTOC.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
// Define styles
1313
$fontStyle12 = array('spaceAfter' => 60, 'size' => 12);
1414
$fontStyle10 = array('size' => 10);
15+
$phpWord->addTitleStyle(null, array('size' => 22, 'bold' => true));
1516
$phpWord->addTitleStyle(1, array('size' => 20, 'color' => '333333', 'bold' => true));
1617
$phpWord->addTitleStyle(2, array('size' => 16, 'color' => '666666'));
1718
$phpWord->addTitleStyle(3, array('size' => 14, 'italic' => true));
1819
$phpWord->addTitleStyle(4, array('size' => 12));
1920

2021
// Add text elements
21-
$section->addText('Table of contents 1');
22+
$section->addTitle('Table of contents 1', 0);
2223
$section->addTextBreak(2);
2324

2425
// Add TOC #1

src/PhpWord/Collection/AbstractCollection.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ abstract class AbstractCollection
2727
/**
2828
* Items
2929
*
30-
* @var array
30+
* @var \PhpOffice\PhpWord\Element\AbstractContainer[]
3131
*/
3232
private $items = array();
3333

3434
/**
3535
* Get items
3636
*
37-
* @return array
37+
* @return \PhpOffice\PhpWord\Element\AbstractContainer[]
3838
*/
3939
public function getItems()
4040
{
@@ -45,7 +45,7 @@ public function getItems()
4545
* Get item by index
4646
*
4747
* @param int $index
48-
* @return mixed
48+
* @return \PhpOffice\PhpWord\Element\AbstractContainer
4949
*/
5050
public function getItem($index)
5151
{
@@ -60,7 +60,7 @@ public function getItem($index)
6060
* Set item.
6161
*
6262
* @param int $index
63-
* @param mixed $item
63+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $item
6464
*/
6565
public function setItem($index, $item)
6666
{
@@ -72,7 +72,7 @@ public function setItem($index, $item)
7272
/**
7373
* Add new item
7474
*
75-
* @param mixed $item
75+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $item
7676
* @return int
7777
*/
7878
public function addItem($item)

src/PhpWord/Element/AbstractContainer.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ abstract class AbstractContainer extends AbstractElement
5454
/**
5555
* Elements collection
5656
*
57-
* @var array
57+
* @var \PhpOffice\PhpWord\Element\AbstractElement[]
5858
*/
5959
protected $elements = array();
6060

@@ -164,6 +164,41 @@ public function getElements()
164164
return $this->elements;
165165
}
166166

167+
/**
168+
* Returns the element at the requested position
169+
*
170+
* @param int $index
171+
* @return \PhpOffice\PhpWord\Element\AbstractElement|null
172+
*/
173+
public function getElement($index)
174+
{
175+
if (array_key_exists($index, $this->elements)) {
176+
return $this->elements[$index];
177+
}
178+
179+
return null;
180+
}
181+
182+
/**
183+
* Removes the element at requested index
184+
*
185+
* @param int|\PhpOffice\PhpWord\Element\AbstractElement $toRemove
186+
*/
187+
public function removeElement($toRemove)
188+
{
189+
if (is_int($toRemove) && array_key_exists($toRemove, $this->elements)) {
190+
unset($this->elements[$toRemove]);
191+
} elseif ($toRemove instanceof \PhpOffice\PhpWord\Element\AbstractElement) {
192+
foreach ($this->elements as $key => $element) {
193+
if ($element->getElementId() === $toRemove->getElementId()) {
194+
unset($this->elements[$key]);
195+
196+
return;
197+
}
198+
}
199+
}
200+
}
201+
167202
/**
168203
* Count elements
169204
*

src/PhpWord/Element/Bookmark.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Bookmark extends AbstractElement
4343
*
4444
* @param string $name
4545
*/
46-
public function __construct($name)
46+
public function __construct($name = '')
4747
{
4848
$this->name = CommonText::toUTF8($name);
4949
}

src/PhpWord/Element/ListItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = n
6262

6363
// Version >= 0.10.0 will pass numbering style name. Older version will use old method
6464
if (!is_null($listStyle) && is_string($listStyle)) {
65-
$this->style = new ListItemStyle($listStyle);
65+
$this->style = new ListItemStyle($listStyle); // @codeCoverageIgnore
6666
} else {
6767
$this->style = $this->setNewStyle(new ListItemStyle(), $listStyle, true);
6868
}

src/PhpWord/Element/Title.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Title extends AbstractElement
2828
/**
2929
* Title Text content
3030
*
31-
* @var string
31+
* @var string|TextRun
3232
*/
3333
private $text;
3434

@@ -56,15 +56,25 @@ class Title extends AbstractElement
5656
/**
5757
* Create a new Title Element
5858
*
59-
* @param string $text
59+
* @param string|TextRun $text
6060
* @param int $depth
6161
*/
6262
public function __construct($text, $depth = 1)
6363
{
64-
$this->text = CommonText::toUTF8($text);
64+
if (isset($text)) {
65+
if (is_string($text)) {
66+
$this->text = CommonText::toUTF8($text);
67+
} elseif ($text instanceof TextRun) {
68+
$this->text = $text;
69+
} else {
70+
throw new \InvalidArgumentException('Invalid text, should be a string or a TextRun');
71+
}
72+
}
73+
6574
$this->depth = $depth;
66-
if (array_key_exists("Heading_{$this->depth}", Style::getStyles())) {
67-
$this->style = "Heading{$this->depth}";
75+
$styleName = $depth === 0 ? 'Title' : "Heading_{$this->depth}";
76+
if (array_key_exists($styleName, Style::getStyles())) {
77+
$this->style = str_replace('_', '', $styleName);
6878
}
6979

7080
return $this;

0 commit comments

Comments
 (0)