Skip to content

Commit f7dd9dd

Browse files
committed
Refactor writers and styles
1 parent ea41b08 commit f7dd9dd

33 files changed

+444
-424
lines changed

src/PhpWord/Element/AbstractContainer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function countElements()
7878
public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text')
7979
{
8080
$this->checkValidity($elementName);
81-
$elementClass = __NAMESPACE__ . '\\' . $elementName;
81+
$elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName;
8282

8383
// Reset paragraph style for footnote and textrun. They have their own
8484
if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) {
@@ -248,7 +248,7 @@ public function addObject($source, $style = null)
248248
public function addFootnote($paragraphStyle = null, $elementName = 'Footnote')
249249
{
250250
$this->checkValidity($elementName);
251-
$elementClass = __NAMESPACE__ . '\\' . $elementName;
251+
$elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName;
252252
$docPart = strtolower($elementName);
253253
$addMethod = "add{$elementName}";
254254

src/PhpWord/Element/AbstractElement.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,7 @@ public function isInSection()
231231
protected function setStyle($styleObject, $styleValue = null, $returnObject = false)
232232
{
233233
if (!is_null($styleValue) && is_array($styleValue)) {
234-
foreach ($styleValue as $key => $value) {
235-
$styleObject->setStyleValue($key, $value);
236-
}
234+
$styleObject->setStyleByArray($styleValue);
237235
$style = $styleObject;
238236
} else {
239237
$style = $returnObject ? $styleObject : $styleValue;

src/PhpWord/Element/Section.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,9 @@ public function hasDifferentFirstPage()
222222
*/
223223
private function addHeaderFooter($type = Header::AUTO, $header = true)
224224
{
225+
$containerClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' .
226+
($header ? 'Header' : 'Footer');
225227
$collectionArray = $header ? 'headers' : 'footers';
226-
$containerClass = __NAMESPACE__ . '\\' . ($header ? 'Header' : 'Footer');
227228
$collection = &$this->$collectionArray;
228229

229230
if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) {

src/PhpWord/Element/TOC.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,14 @@ public function __construct($fontStyle = null, $tocStyle = null, $minDepth = 1,
6868
$this->TOCStyle = new TOCStyle();
6969

7070
if (!is_null($tocStyle) && is_array($tocStyle)) {
71-
foreach ($tocStyle as $key => $value) {
72-
$this->TOCStyle->setStyleValue($key, $value);
73-
}
71+
$this->TOCStyle->setStyleByArray($tocStyle);
7472
}
7573

76-
if (!is_null($fontStyle)) {
77-
if (is_array($fontStyle)) {
78-
$this->fontStyle = new Font();
79-
foreach ($fontStyle as $key => $value) {
80-
$this->fontStyle->setStyleValue($key, $value);
81-
}
82-
} else {
83-
$this->fontStyle = $fontStyle;
84-
}
74+
if (!is_null($fontStyle) && is_array($fontStyle)) {
75+
$this->fontStyle = new Font();
76+
$this->fontStyle->setStyleByArray($fontStyle);
77+
} else {
78+
$this->fontStyle = $fontStyle;
8579
}
8680

8781
$this->minDepth = $minDepth;

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 102 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -138,60 +138,59 @@ protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, &$parent,
138138
/**
139139
* Read w:pPr
140140
*
141-
* @return string|array|null
141+
* @return array|null
142142
*/
143143
protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode)
144144
{
145-
$style = null;
146-
if ($xmlReader->elementExists('w:pPr', $domNode)) {
147-
if ($xmlReader->elementExists('w:pPr/w:pStyle', $domNode)) {
148-
$style = $xmlReader->getAttribute('w:val', $domNode, 'w:pPr/w:pStyle');
149-
} else {
150-
$style = array();
151-
$mapping = array(
152-
'w:ind' => 'indent', 'w:spacing' => 'spacing',
153-
'w:jc' => 'align', 'w:basedOn' => 'basedOn', 'w:next' => 'next',
154-
'w:widowControl' => 'widowControl', 'w:keepNext' => 'keepNext',
155-
'w:keepLines' => 'keepLines', 'w:pageBreakBefore' => 'pageBreakBefore',
156-
);
157-
158-
$nodes = $xmlReader->getElements('w:pPr/*', $domNode);
159-
foreach ($nodes as $node) {
160-
if (!array_key_exists($node->nodeName, $mapping)) {
161-
continue;
162-
}
163-
$property = $mapping[$node->nodeName];
164-
switch ($node->nodeName) {
165-
166-
case 'w:ind':
167-
$style['indent'] = $xmlReader->getAttribute('w:left', $node);
168-
$style['hanging'] = $xmlReader->getAttribute('w:hanging', $node);
169-
break;
170-
171-
case 'w:spacing':
172-
$style['spaceAfter'] = $xmlReader->getAttribute('w:after', $node);
173-
$style['spaceBefore'] = $xmlReader->getAttribute('w:before', $node);
174-
// Commented. Need to adjust the number when return value is null
175-
// $style['spacing'] = $xmlReader->getAttribute('w:line', $node);
176-
break;
177-
178-
case 'w:keepNext':
179-
case 'w:keepLines':
180-
case 'w:pageBreakBefore':
181-
$style[$property] = true;
182-
break;
183-
184-
case 'w:widowControl':
185-
$style[$property] = false;
186-
break;
145+
if (!$xmlReader->elementExists('w:pPr', $domNode)) {
146+
return;
147+
}
187148

188-
case 'w:jc':
189-
case 'w:basedOn':
190-
case 'w:next':
191-
$style[$property] = $xmlReader->getAttribute('w:val', $node);
192-
break;
193-
}
194-
}
149+
$style = array();
150+
$mapping = array(
151+
'w:pStyle' => 'styleName',
152+
'w:ind' => 'indent', 'w:spacing' => 'spacing',
153+
'w:jc' => 'align', 'w:basedOn' => 'basedOn', 'w:next' => 'next',
154+
'w:widowControl' => 'widowControl', 'w:keepNext' => 'keepNext',
155+
'w:keepLines' => 'keepLines', 'w:pageBreakBefore' => 'pageBreakBefore',
156+
);
157+
158+
$nodes = $xmlReader->getElements('w:pPr/*', $domNode);
159+
foreach ($nodes as $node) {
160+
if (!array_key_exists($node->nodeName, $mapping)) {
161+
continue;
162+
}
163+
$property = $mapping[$node->nodeName];
164+
switch ($node->nodeName) {
165+
166+
case 'w:ind':
167+
$style['indent'] = $xmlReader->getAttribute('w:left', $node);
168+
$style['hanging'] = $xmlReader->getAttribute('w:hanging', $node);
169+
break;
170+
171+
case 'w:spacing':
172+
$style['spaceAfter'] = $xmlReader->getAttribute('w:after', $node);
173+
$style['spaceBefore'] = $xmlReader->getAttribute('w:before', $node);
174+
// Commented. Need to adjust the number when return value is null
175+
// $style['spacing'] = $xmlReader->getAttribute('w:line', $node);
176+
break;
177+
178+
case 'w:keepNext':
179+
case 'w:keepLines':
180+
case 'w:pageBreakBefore':
181+
$style[$property] = true;
182+
break;
183+
184+
case 'w:widowControl':
185+
$style[$property] = false;
186+
break;
187+
188+
case 'w:pStyle':
189+
case 'w:jc':
190+
case 'w:basedOn':
191+
case 'w:next':
192+
$style[$property] = $xmlReader->getAttribute('w:val', $node);
193+
break;
195194
}
196195
}
197196

@@ -201,70 +200,69 @@ protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode
201200
/**
202201
* Read w:rPr
203202
*
204-
* @return string|array|null
203+
* @return array|null
205204
*/
206205
protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
207206
{
208-
$style = null;
207+
if (is_null($domNode)) {
208+
return;
209+
}
209210
// Hyperlink has an extra w:r child
210211
if ($domNode->nodeName == 'w:hyperlink') {
211212
$domNode = $xmlReader->getElement('w:r', $domNode);
212213
}
213-
if (is_null($domNode)) {
214-
return $style;
214+
if (!$xmlReader->elementExists('w:rPr', $domNode)) {
215+
return;
215216
}
216-
if ($xmlReader->elementExists('w:rPr', $domNode)) {
217-
if ($xmlReader->elementExists('w:rPr/w:rStyle', $domNode)) {
218-
$style = $xmlReader->getAttribute('w:val', $domNode, 'w:rPr/w:rStyle');
219-
} else {
220-
$style = array();
221-
$mapping = array(
222-
'w:b' => 'bold', 'w:i' => 'italic', 'w:color' => 'color',
223-
'w:strike' => 'strikethrough', 'w:u' => 'underline',
224-
'w:highlight' => 'fgColor', 'w:sz' => 'size',
225-
'w:rFonts' => 'name', 'w:vertAlign' => 'superScript',
226-
);
227217

228-
$nodes = $xmlReader->getElements('w:rPr/*', $domNode);
229-
foreach ($nodes as $node) {
230-
if (!array_key_exists($node->nodeName, $mapping)) {
231-
continue;
232-
}
233-
$property = $mapping[$node->nodeName];
234-
switch ($node->nodeName) {
235-
236-
case 'w:rFonts':
237-
$style['name'] = $xmlReader->getAttribute('w:ascii', $node);
238-
$style['hint'] = $xmlReader->getAttribute('w:hint', $node);
239-
break;
240-
241-
case 'w:b':
242-
case 'w:i':
243-
case 'w:strike':
244-
$style[$property] = true;
245-
break;
246-
247-
case 'w:u':
248-
case 'w:highlight':
249-
case 'w:color':
250-
$style[$property] = $xmlReader->getAttribute('w:val', $node);
251-
break;
252-
253-
case 'w:sz':
254-
$style[$property] = $xmlReader->getAttribute('w:val', $node) / 2;
255-
break;
256-
257-
case 'w:vertAlign':
258-
$style[$property] = $xmlReader->getAttribute('w:val', $node);
259-
if ($style[$property] == 'superscript') {
260-
$style['superScript'] = true;
261-
} else {
262-
$style['superScript'] = false;
263-
$style['subScript'] = true;
264-
}
265-
break;
218+
$style = array();
219+
$mapping = array(
220+
'w:rStyle' => 'styleName',
221+
'w:b' => 'bold', 'w:i' => 'italic', 'w:color' => 'color',
222+
'w:strike' => 'strikethrough', 'w:u' => 'underline',
223+
'w:highlight' => 'fgColor', 'w:sz' => 'size',
224+
'w:rFonts' => 'name', 'w:vertAlign' => 'superScript',
225+
);
226+
227+
$nodes = $xmlReader->getElements('w:rPr/*', $domNode);
228+
foreach ($nodes as $node) {
229+
if (!array_key_exists($node->nodeName, $mapping)) {
230+
continue;
231+
}
232+
$property = $mapping[$node->nodeName];
233+
switch ($node->nodeName) {
234+
235+
case 'w:rFonts':
236+
$style['name'] = $xmlReader->getAttribute('w:ascii', $node);
237+
$style['hint'] = $xmlReader->getAttribute('w:hint', $node);
238+
break;
239+
240+
case 'w:b':
241+
case 'w:i':
242+
case 'w:strike':
243+
$style[$property] = true;
244+
break;
245+
246+
case 'w:rStyle':
247+
case 'w:u':
248+
case 'w:highlight':
249+
case 'w:color':
250+
$style[$property] = $xmlReader->getAttribute('w:val', $node);
251+
break;
252+
253+
case 'w:sz':
254+
$style[$property] = $xmlReader->getAttribute('w:val', $node) / 2;
255+
break;
256+
257+
case 'w:vertAlign':
258+
$style[$property] = $xmlReader->getAttribute('w:val', $node);
259+
if ($style[$property] == 'superscript') {
260+
$style['superScript'] = true;
261+
} else {
262+
$style['superScript'] = false;
263+
$style['subScript'] = true;
266264
}
267-
}
265+
break;
268266
}
269267
}
270268

src/PhpWord/Style/AbstractStyle.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ protected function setNonEmptyVal($value, $default)
160160
*/
161161
protected function setBoolVal($value, $default = null)
162162
{
163-
if (is_string($value)) {
164-
$value = (bool)$value;
165-
}
166163
if (!is_bool($value)) {
167164
$value = $default;
168165
}
@@ -187,15 +184,15 @@ protected function setNumericVal($value, $default = null)
187184
}
188185

189186
/**
190-
* Set integer value
187+
* Set float value: Convert string that contains only numeric into integer
191188
*
192189
* @param mixed $value
193190
* @param int|null $default
194191
* @return int|null
195192
*/
196193
protected function setIntVal($value, $default = null)
197194
{
198-
if (is_string($value)) {
195+
if (is_string($value) && (preg_match('/[^\d]/', $value) == 0)) {
199196
$value = intval($value);
200197
}
201198
if (!is_int($value)) {
@@ -206,15 +203,15 @@ protected function setIntVal($value, $default = null)
206203
}
207204

208205
/**
209-
* Set float value
206+
* Set float value: Convert string that contains only numeric into float
210207
*
211208
* @param mixed $value
212209
* @param float|null $default
213210
* @return float|null
214211
*/
215212
protected function setFloatVal($value, $default = null)
216213
{
217-
if (is_string($value)) {
214+
if (is_string($value) && (preg_match('/[^\d\.\,]/', $value) == 0)) {
218215
$value = floatval($value);
219216
}
220217
if (!is_float($value)) {
@@ -231,9 +228,11 @@ protected function setFloatVal($value, $default = null)
231228
* @param array $enum
232229
* @param mixed $default
233230
*/
234-
protected function setEnumVal($value, $enum, $default = null)
231+
protected function setEnumVal($value = null, $enum = array(), $default = null)
235232
{
236-
if (!in_array($value, $enum)) {
233+
if (!is_null($value) && !empty($enum) && !in_array($value, $enum)) {
234+
throw new \InvalidArgumentException('Invalid style value.');
235+
} elseif (is_null($value)) {
237236
$value = $default;
238237
}
239238

@@ -249,7 +248,7 @@ protected function setEnumVal($value, $enum, $default = null)
249248
*/
250249
protected function setObjectVal($value, $styleName, &$style)
251250
{
252-
$styleClass = __NAMESPACE__ . '\\' . $styleName;
251+
$styleClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $styleName;
253252
if (is_array($value)) {
254253
if (!$style instanceof $styleClass) {
255254
$style = new $styleClass();

0 commit comments

Comments
 (0)