Skip to content

Commit b7480d8

Browse files
committed
Refactor: Apply composite design pattern to Word2007/OOXML writer
1 parent 3249941 commit b7480d8

35 files changed

+1452
-1116
lines changed

phpmd.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
66
xsi:noNamespaceSchemaLocation=" http://pmd.sf.net/ruleset_xml_schema.xsd">
7-
<description>PHPPowerPoint ruleset</description>
87
<rule ref="rulesets/unusedcode.xml" />
98
<rule ref="rulesets/design.xml" />
10-
<rule ref="rulesets/naming.xml/LongVariable">
11-
<properties>
12-
<property name="maximum" value="30" />
13-
</properties>
14-
</rule>
9+
<rule ref="rulesets/naming.xml/LongVariable" />
1510
<rule ref="rulesets/naming.xml/ShortVariable" />
1611
<rule ref="rulesets/naming.xml/ShortMethodName" />
1712
<rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass" />

src/PhpWord/Element/AbstractElement.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use PhpOffice\PhpWord\Shared\String;
1919

2020
/**
21-
* Container abstract class
21+
* Element abstract class
2222
*
2323
* @since 0.10.0
2424
*/
@@ -567,7 +567,7 @@ private function checkValidity($method)
567567
);
568568
// Special condition, e.g. preservetext can only exists in cell when
569569
// the cell is located in header or footer
570-
$validContainerInContainers = array(
570+
$validSubcontainers = array(
571571
'preservetext' => array(array('cell'), array('header', 'footer')),
572572
'footnote' => array(array('cell', 'textrun'), array('section')),
573573
'endnote' => array(array('cell', 'textrun'), array('section')),
@@ -580,8 +580,8 @@ private function checkValidity($method)
580580
}
581581
}
582582
// Check if a method is valid for current container, located in other container
583-
if (array_key_exists($method, $validContainerInContainers)) {
584-
$rules = $validContainerInContainers[$method];
583+
if (array_key_exists($method, $validSubcontainers)) {
584+
$rules = $validSubcontainers[$method];
585585
$containers = $rules[0];
586586
$allowedDocParts = $rules[1];
587587
foreach ($containers as $container) {

src/PhpWord/Element/Section.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
use PhpOffice\PhpWord\Exception\Exception;
1313
use PhpOffice\PhpWord\Style\Section as SectionSettings;
14-
use PhpOffice\PhpWord\TOC;
14+
use PhpOffice\PhpWord\Element\PageBreak;
15+
use PhpOffice\PhpWord\Element\TOC;
1516

1617
/**
1718
* Section
@@ -96,7 +97,7 @@ public function addPageBreak()
9697
* @param mixed $styleTOC
9798
* @param integer $minDepth
9899
* @param integer $maxDepth
99-
* @return \PhpOffice\PhpWord\TOC
100+
* @return \PhpOffice\PhpWord\Element\TOC
100101
*/
101102
public function addTOC($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
102103
{

src/PhpWord/Element/TOC.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Element;
11+
12+
use PhpOffice\PhpWord\TOC as Titles;
13+
use PhpOffice\PhpWord\Style\Font;
14+
use PhpOffice\PhpWord\Style\TOC as TOCStyle;
15+
16+
/**
17+
* Table of contents
18+
*/
19+
class TOC extends AbstractElement
20+
{
21+
/**
22+
* TOC style
23+
*
24+
* @var \PhpOffice\PhpWord\Style\TOC
25+
*/
26+
private $TOCStyle;
27+
28+
/**
29+
* Font style
30+
*
31+
* @var \PhpOffice\PhpWord\Style\Font|array|string
32+
*/
33+
private $fontStyle;
34+
35+
/**
36+
* Min title depth to show
37+
*
38+
* @var int
39+
*/
40+
private $minDepth = 1;
41+
42+
/**
43+
* Max title depth to show
44+
*
45+
* @var int
46+
*/
47+
private $maxDepth = 9;
48+
49+
50+
/**
51+
* Create a new Table-of-Contents Element
52+
*
53+
* @param mixed $styleFont
54+
* @param array $styleTOC
55+
* @param integer $minDepth
56+
* @param integer $maxDepth
57+
*/
58+
public function __construct($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
59+
{
60+
$this->TOCStyle = new TOCStyle();
61+
62+
if (!is_null($styleTOC) && is_array($styleTOC)) {
63+
foreach ($styleTOC as $key => $value) {
64+
$this->TOCStyle->setStyleValue($key, $value);
65+
}
66+
}
67+
68+
if (!is_null($styleFont)) {
69+
if (is_array($styleFont)) {
70+
$this->fontStyle = new Font();
71+
foreach ($styleFont as $key => $value) {
72+
$this->fontStyle->setStyleValue($key, $value);
73+
}
74+
} else {
75+
$this->fontStyle = $styleFont;
76+
}
77+
}
78+
79+
$this->minDepth = $minDepth;
80+
$this->maxDepth = $maxDepth;
81+
}
82+
83+
/**
84+
* Get all titles
85+
*
86+
* @return array
87+
*/
88+
public function getTitles()
89+
{
90+
$titles = Titles::getTitles();
91+
foreach ($titles as $i => $title) {
92+
if ($this->minDepth > $title['depth']) {
93+
unset($titles[$i]);
94+
}
95+
if (($this->maxDepth != 0) && ($this->maxDepth < $title['depth'])) {
96+
unset($titles[$i]);
97+
}
98+
}
99+
$titles = array_merge(array(), $titles);
100+
101+
return $titles;
102+
}
103+
104+
/**
105+
* Get TOC Style
106+
*
107+
* @return \PhpOffice\PhpWord\Style\TOC
108+
*/
109+
public function getStyleTOC()
110+
{
111+
return $this->TOCStyle;
112+
}
113+
114+
/**
115+
* Get Font Style
116+
*
117+
* @return \PhpOffice\PhpWord\Style\Font
118+
*/
119+
public function getStyleFont()
120+
{
121+
return $this->fontStyle;
122+
}
123+
124+
/**
125+
* Set max depth
126+
*
127+
* @param int $value
128+
*/
129+
public function setMaxDepth($value)
130+
{
131+
$this->maxDepth = $value;
132+
}
133+
134+
/**
135+
* Get Max Depth
136+
*
137+
* @return int Max depth of titles
138+
*/
139+
public function getMaxDepth()
140+
{
141+
return $this->maxDepth;
142+
}
143+
144+
/**
145+
* Set min depth
146+
*
147+
* @param int $value
148+
*/
149+
public function setMinDepth($value)
150+
{
151+
$this->minDepth = $value;
152+
}
153+
154+
/**
155+
* Get Min Depth
156+
*
157+
* @return int Min depth of titles
158+
*/
159+
public function getMinDepth()
160+
{
161+
return $this->minDepth;
162+
}
163+
}

src/PhpWord/Shared/XMLWriter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
class XMLWriter
3131
{
32-
/** Temporary storage method */
32+
/** Temporary storage location */
3333
const STORAGE_MEMORY = 1;
3434
const STORAGE_DISK = 2;
3535

@@ -50,20 +50,20 @@ class XMLWriter
5050
/**
5151
* Create new XMLWriter
5252
*
53-
* @param int $pTemporaryStorage Temporary storage location
54-
* @param string $pTemporaryStorageFolder Temporary storage folder
53+
* @param int $tempLocation Temporary storage location
54+
* @param string $tempFolder Temporary storage folder
5555
*/
56-
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = './')
56+
public function __construct($tempLocation = self::STORAGE_MEMORY, $tempFolder = './')
5757
{
5858
// Create internal XMLWriter
5959
$this->xmlWriter = new \XMLWriter();
6060

6161
// Open temporary storage
62-
if ($pTemporaryStorage == self::STORAGE_MEMORY) {
62+
if ($tempLocation == self::STORAGE_MEMORY) {
6363
$this->xmlWriter->openMemory();
6464
} else {
6565
// Create temporary filename
66-
$this->tempFile = @tempnam($pTemporaryStorageFolder, 'xml');
66+
$this->tempFile = @tempnam($tempFolder, 'xml');
6767

6868
// Open storage
6969
if ($this->xmlWriter->openUri($this->tempFile) === false) {

src/PhpWord/Shared/ZipArchive.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
namespace PhpOffice\PhpWord\Shared;
1111

12-
use PhpOffice\PhpWord\Exception\Exception;
13-
1412
// PCLZIP needs the temp path to end in a back slash
1513
// @codeCoverageIgnoreStart
1614
if (!defined('PCLZIP_TEMPORARY_DIR')) {

src/PhpWord/Style/Tab.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
namespace PhpOffice\PhpWord\Style;
1111

12-
use PhpOffice\PhpWord\Shared\XMLWriter;
13-
1412
/**
1513
* Tab style
1614
*/

0 commit comments

Comments
 (0)