Skip to content

Commit 8e17598

Browse files
committed
Merge pull request #115 from ivanlanin/develop
Reorganize samples and write unsupported element to ODText and RTF
2 parents f711e8a + 1aa83b9 commit 8e17598

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+780
-388
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ vendor
1818
*.docx
1919
*.rtf
2020
*.txt
21-
*.xml
22-
nbproject
21+
*.xml

Classes/PHPWord/Reader/Word2007.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ public function load($pFilename)
254254
if ($elm->getName() != 'style') {
255255
continue;
256256
}
257-
unset($pStyle);
258-
unset($fStyle);
257+
$pStyle = null;
258+
$fStyle = null;
259259
$hasParagraphStyle = isset($elm->pPr);
260260
$hasFontStyle = isset($elm->rPr);
261261
$styleName = (string)$elm->name['val'];

Classes/PHPWord/Section.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ public function addObject($src, $style = null)
243243
$this->_elementCollection[] = $object;
244244
return $object;
245245
} else {
246-
trigger_error('Source does not exist or unsupported object type.');
246+
throw new PHPWord_Exception(
247+
'Source does not exist or unsupported object type.'
248+
);
247249
}
248250
}
249251

@@ -265,7 +267,9 @@ public function addImage($src, $style = null)
265267
$this->_elementCollection[] = $image;
266268
return $image;
267269
} else {
268-
trigger_error('Source does not exist or unsupported image type.');
270+
throw new PHPWord_Exception(
271+
'Source does not exist or unsupported image type.'
272+
);
269273
}
270274
}
271275

@@ -286,7 +290,9 @@ public function addMemoryImage($link, $style = null)
286290
$this->_elementCollection[] = $memoryImage;
287291
return $memoryImage;
288292
} else {
289-
trigger_error('Unsupported image type.');
293+
throw new PHPWord_Exception(
294+
'Unsupported image type.'
295+
);
290296
}
291297
}
292298

Classes/PHPWord/Section/Footer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function addImage($src, $style = null)
133133
$this->_elementCollection[] = $image;
134134
return $image;
135135
} else {
136-
trigger_error('Src does not exist or invalid image type.', E_USER_ERROR);
136+
throw new Exception('Src does not exist or invalid image type.');
137137
}
138138
}
139139

@@ -154,7 +154,7 @@ public function addMemoryImage($link, $style = null)
154154
$this->_elementCollection[] = $memoryImage;
155155
return $memoryImage;
156156
} else {
157-
trigger_error('Unsupported image type.');
157+
throw new Exception('Unsupported image type.');
158158
}
159159
}
160160

Classes/PHPWord/Section/Header.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function addImage($src, $style = null)
162162
$this->_elementCollection[] = $image;
163163
return $image;
164164
} else {
165-
trigger_error('Src does not exist or invalid image type.', E_USER_ERROR);
165+
throw new Exception('Src does not exist or invalid image type.');
166166
}
167167
}
168168

@@ -183,7 +183,7 @@ public function addMemoryImage($link, $style = null)
183183
$this->_elementCollection[] = $memoryImage;
184184
return $memoryImage;
185185
} else {
186-
trigger_error('Unsupported image type.');
186+
throw new Exception('Unsupported image type.');
187187
}
188188
}
189189

@@ -223,7 +223,7 @@ public function addWatermark($src, $style = null)
223223
$this->_elementCollection[] = $image;
224224
return $image;
225225
} else {
226-
trigger_error('Src does not exist or invalid image type.', E_USER_ERROR);
226+
throw new Exception('Src does not exist or invalid image type.');
227227
}
228228
}
229229

Classes/PHPWord/Section/MemoryImage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,4 @@ public function getImageExtension()
229229
{
230230
return $this->_imageExtension;
231231
}
232-
}
232+
}

Classes/PHPWord/Section/Table/Cell.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function addLink($linkSrc, $linkName = null, $style = null)
140140
$this->_elementCollection[] = $link;
141141
return $link;
142142
} else {
143-
trigger_error('Unsupported Link header / footer reference');
143+
throw new Exception('Unsupported Link header / footer reference');
144144
return false;
145145
}
146146
}
@@ -198,7 +198,7 @@ public function addImage($src, $style = null)
198198
$this->_elementCollection[] = $image;
199199
return $image;
200200
} else {
201-
trigger_error('Source does not exist or unsupported image type.');
201+
throw new Exception('Source does not exist or unsupported image type.');
202202
}
203203
}
204204

@@ -225,7 +225,7 @@ public function addMemoryImage($link, $style = null)
225225
$this->_elementCollection[] = $memoryImage;
226226
return $memoryImage;
227227
} else {
228-
trigger_error('Unsupported image type.');
228+
throw new Exception('Unsupported image type.');
229229
}
230230
}
231231

@@ -266,7 +266,7 @@ public function addObject($src, $style = null)
266266
$this->_elementCollection[] = $object;
267267
return $object;
268268
} else {
269-
trigger_error('Source does not exist or unsupported object type.');
269+
throw new Exception('Source does not exist or unsupported object type.');
270270
}
271271
}
272272

@@ -288,7 +288,7 @@ public function addPreserveText($text, $styleFont = null, $styleParagraph = null
288288
$this->_elementCollection[] = $ptext;
289289
return $ptext;
290290
} else {
291-
trigger_error('addPreserveText only supported in footer/header.');
291+
throw new Exception('addPreserveText only supported in footer/header.');
292292
}
293293
}
294294

Classes/PHPWord/Section/TextRun.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function addImage($imageSrc, $style = null)
127127
$this->_elementCollection[] = $image;
128128
return $image;
129129
} else {
130-
trigger_error('Source does not exist or unsupported image type.');
130+
throw new Exception('Source does not exist or unsupported image type.');
131131
}
132132
}
133133

Classes/PHPWord/Settings.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
/**
2929
* PHPWord_Settings
3030
*/
31-
class PHPWord_Settings {
31+
class PHPWord_Settings
32+
{
3233
/**
3334
* Compatibility option for XMLWriter
3435
*
@@ -42,12 +43,13 @@ class PHPWord_Settings {
4243
* @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility
4344
* @return boolean Success or failure
4445
*/
45-
public static function setCompatibility($compatibility) {
46+
public static function setCompatibility($compatibility)
47+
{
4648
if (is_bool($compatibility)) {
4749
self::$_xmlWriterCompatibility = $compatibility;
48-
return TRUE;
50+
return true;
4951
}
50-
return FALSE;
52+
return false;
5153
} // function setCompatibility()
5254

5355

@@ -56,7 +58,8 @@ public static function setCompatibility($compatibility) {
5658
*
5759
* @return boolean Compatibility
5860
*/
59-
public static function getCompatibility() {
61+
public static function getCompatibility()
62+
{
6063
return self::$_xmlWriterCompatibility;
6164
} // function getCompatibility()
62-
}
65+
}

Classes/PHPWord/Style/Font.php

-2
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ public function __construct($type = 'text', $paragraphStyle = null)
179179
} elseif (is_array($paragraphStyle)) {
180180
$this->_paragraphStyle = new PHPWord_Style_Paragraph;
181181
$this->_paragraphStyle->setArrayStyle($paragraphStyle);
182-
} elseif (null === $paragraphStyle) {
183-
$this->_paragraphStyle = new PHPWord_Style_Paragraph;
184182
} else {
185183
$this->_paragraphStyle = $paragraphStyle;
186184
}

Classes/PHPWord/Style/Row.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,20 @@ class PHPWord_Style_Row
3636
*
3737
* @var bool
3838
*/
39-
private $_tblHeader;
39+
private $_tblHeader = false;
4040

4141
/**
4242
* Table row cannot break across pages
4343
*
4444
* @var bool
4545
*/
46-
private $_cantSplit;
46+
private $_cantSplit = false;
4747

4848
/**
4949
* Create a new row style
5050
*/
5151
public function __construct()
5252
{
53-
$this->_tblHeader = null;
54-
$this->_cantSplit = null;
5553
}
5654

5755
/**
@@ -62,23 +60,31 @@ public function setStyleValue($key, $value)
6260
$this->$key = $value;
6361
}
6462

65-
public function setTblHeader($pValue = null)
63+
public function setTblHeader($pValue = false)
6664
{
65+
if (!is_bool($pValue)) {
66+
$pValue = false;
67+
}
6768
$this->_tblHeader = $pValue;
69+
return $this;
6870
}
6971

7072
public function getTblHeader()
7173
{
72-
return $this->_tblHeader ? 1 : 0;
74+
return $this->_tblHeader;
7375
}
7476

75-
public function setCantSplit($pValue = null)
77+
public function setCantSplit($pValue = false)
7678
{
79+
if (!is_bool($pValue)) {
80+
$pValue = false;
81+
}
7782
$this->_cantSplit = $pValue;
83+
return $this;
7884
}
7985

8086
public function getCantSplit()
8187
{
82-
return $this->_cantSplit ? 1 : 0;
88+
return $this->_cantSplit;
8389
}
8490
}

Classes/PHPWord/Template.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private function _findRowStart($offset)
164164
$rowStart = strrpos($this->_documentXML, "<w:tr>", ((strlen($this->_documentXML) - $offset) * -1));
165165
}
166166
if (!$rowStart) {
167-
trigger_error("Can not find the start position of the row to clone.");
167+
throw new Exception("Can not find the start position of the row to clone.");
168168
return false;
169169
}
170170
return $rowStart;
@@ -208,7 +208,7 @@ public function cloneRow($search, $numberOfClones)
208208

209209
$tagPos = strpos($this->_documentXML, $search);
210210
if (!$tagPos) {
211-
trigger_error("Can not clone row, template variable not found or variable contains markup.");
211+
throw new Exception("Can not clone row, template variable not found or variable contains markup.");
212212
return false;
213213
}
214214

Classes/PHPWord/Writer/ODText/Content.php

+22-12
Original file line numberDiff line numberDiff line change
@@ -253,28 +253,25 @@ public function writeContent(PHPWord $pPHPWord = null)
253253
$this->_writeTextRun($objWriter, $element);
254254
} elseif ($element instanceof PHPWord_Section_TextBreak) {
255255
$this->_writeTextBreak($objWriter);
256-
/*
257256
} elseif ($element instanceof PHPWord_Section_Link) {
258-
$this->_writeLink($objWriter, $element);
257+
$this->writeUnsupportedElement($objWriter, 'link');
259258
} elseif ($element instanceof PHPWord_Section_Title) {
260-
$this->_writeTitle($objWriter, $element);
259+
$this->writeUnsupportedElement($objWriter, 'title');
261260
} elseif ($element instanceof PHPWord_Section_PageBreak) {
262-
$this->_writePageBreak($objWriter);
261+
$this->writeUnsupportedElement($objWriter, 'page break');
263262
} elseif ($element instanceof PHPWord_Section_Table) {
264-
$this->_writeTable($objWriter, $element);
263+
$this->writeUnsupportedElement($objWriter, 'table');
265264
} elseif ($element instanceof PHPWord_Section_ListItem) {
266-
$this->_writeListItem($objWriter, $element);
265+
$this->writeUnsupportedElement($objWriter, 'list item');
267266
} elseif ($element instanceof PHPWord_Section_Image ||
268267
$element instanceof PHPWord_Section_MemoryImage) {
269-
$this->_writeImage($objWriter, $element);
268+
$this->writeUnsupportedElement($objWriter, 'image');
270269
} elseif ($element instanceof PHPWord_Section_Object) {
271-
$this->_writeObject($objWriter, $element);
270+
$this->writeUnsupportedElement($objWriter, 'object');
272271
} elseif ($element instanceof PHPWord_TOC) {
273-
$this->_writeTOC($objWriter);
274-
*/
272+
$this->writeUnsupportedElement($objWriter, 'TOC');
275273
} else {
276-
print_r($element);
277-
echo '<br />';
274+
$this->writeUnsupportedElement($objWriter, 'other');
278275
}
279276
}
280277

@@ -387,4 +384,17 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
387384
private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section = null)
388385
{
389386
}
387+
388+
/**
389+
* Write unsupported element
390+
*
391+
* @param PHPWord_Shared_XMLWriter $objWriter
392+
* @param string $element
393+
*/
394+
private function writeUnsupportedElement($objWriter, $element)
395+
{
396+
$objWriter->startElement('text:p');
397+
$objWriter->writeRaw("Cannot write content. This version of PHPWord has not supported {$element} element in ODText.");
398+
$objWriter->endElement();
399+
}
390400
}

0 commit comments

Comments
 (0)