Skip to content

Commit 88560de

Browse files
committed
Refactor writers
1 parent e0638f5 commit 88560de

Some content is hidden

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

68 files changed

+1521
-1541
lines changed

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ tools:
99
enabled: true
1010
timeout: 900
1111
php_sim:
12-
min_mass: 25
12+
min_mass: 30
1313
php_pdepend: true
1414
php_analyzer: true
1515
sensiolabs_security_checker: true

src/PhpWord/Element/AbstractContainer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ abstract class AbstractContainer extends AbstractElement
3939
*/
4040
protected function addElement(AbstractElement $element)
4141
{
42-
// $type = get_class($element);
43-
// $type = str_replace('PhpOffice\\PhpWord\\Element\\', '', $type);
42+
// $type = basename(get_class($element));
4443
$element->setElementIndex($this->countElements() + 1);
4544
$element->setElementId();
4645
$element->setPhpWord($this->phpWord);
@@ -79,7 +78,7 @@ public function countElements()
7978
public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text')
8079
{
8180
$this->checkValidity($elementName);
82-
$elementClass = 'PhpOffice\\PhpWord\\Element\\' . $elementName;
81+
$elementClass = dirname(get_class($this)) . '\\' . $elementName;
8382

8483
// Reset paragraph style for footnote and textrun. They have their own
8584
if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) {
@@ -249,7 +248,7 @@ public function addObject($source, $style = null)
249248
public function addFootnote($paragraphStyle = null, $elementName = 'Footnote')
250249
{
251250
$this->checkValidity($elementName);
252-
$elementClass = 'PhpOffice\\PhpWord\\Element\\' . $elementName;
251+
$elementClass = dirname(get_class($this)) . '\\' . $elementName;
253252
$docPart = strtolower($elementName);
254253
$addMethod = "add{$elementName}";
255254

src/PhpWord/Element/Section.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ public function hasDifferentFirstPage()
223223
private function addHeaderFooter($type = Header::AUTO, $header = true)
224224
{
225225
$collectionArray = $header ? 'headers' : 'footers';
226-
$containerClass = 'PhpOffice\\PhpWord\\Element\\';
227-
$containerClass .= ($header ? 'Header' : 'Footer');
226+
$containerClass = dirname(get_class($this)) . '\\' . ($header ? 'Header' : 'Footer');
228227
$collection = &$this->$collectionArray;
229228

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

src/PhpWord/Element/Text.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function setFontStyle($style = null, $paragraphStyle = null)
7575
$this->setParagraphStyle($paragraphStyle);
7676
} elseif (is_array($style)) {
7777
$this->fontStyle = new Font('text', $paragraphStyle);
78-
$this->fontStyle->setArrayStyle($style);
78+
$this->fontStyle->setStyleByArray($style);
7979
} elseif (null === $style) {
8080
$this->fontStyle = new Font('text', $paragraphStyle);
8181
} else {
@@ -106,7 +106,7 @@ public function setParagraphStyle($style = null)
106106
{
107107
if (is_array($style)) {
108108
$this->paragraphStyle = new Paragraph;
109-
$this->paragraphStyle->setArrayStyle($style);
109+
$this->paragraphStyle->setStyleByArray($style);
110110
} elseif ($style instanceof Paragraph) {
111111
$this->paragraphStyle = $style;
112112
} elseif (null === $style) {

src/PhpWord/Element/TextBreak.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function setFontStyle($style = null, $paragraphStyle = null)
6969
$this->setParagraphStyle($paragraphStyle);
7070
} elseif (is_array($style)) {
7171
$this->fontStyle = new Font('text', $paragraphStyle);
72-
$this->fontStyle->setArrayStyle($style);
72+
$this->fontStyle->setStyleByArray($style);
7373
} else {
7474
$this->fontStyle = $style;
7575
$this->setParagraphStyle($paragraphStyle);
@@ -97,7 +97,7 @@ public function setParagraphStyle($style = null)
9797
{
9898
if (is_array($style)) {
9999
$this->paragraphStyle = new Paragraph;
100-
$this->paragraphStyle->setArrayStyle($style);
100+
$this->paragraphStyle->setStyleByArray($style);
101101
} elseif ($style instanceof Paragraph) {
102102
$this->paragraphStyle = $style;
103103
} else {
@@ -115,4 +115,14 @@ public function getParagraphStyle()
115115
{
116116
return $this->paragraphStyle;
117117
}
118+
119+
/**
120+
* Has font/paragraph style defined
121+
*
122+
* @return bool
123+
*/
124+
public function hasStyle()
125+
{
126+
return !is_null($this->fontStyle) || !is_null($this->paragraphStyle);
127+
}
118128
}

src/PhpWord/Shared/XMLWriter.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,39 @@ public function writeRaw($text)
149149

150150
return $this->text($text);
151151
}
152+
153+
/**
154+
* Write element if ...
155+
*
156+
* @param bool $condition
157+
* @param string $element
158+
* @param string $attribute
159+
* @param string $value
160+
*/
161+
public function writeElementIf($condition, $element, $attribute = null, $value = null)
162+
{
163+
if ($condition) {
164+
if (is_null($attribute)) {
165+
$this->xmlWriter->writeElement($element, $value);
166+
} else {
167+
$this->xmlWriter->startElement($element);
168+
$this->xmlWriter->writeAttribute($attribute, $value);
169+
$this->xmlWriter->endElement();
170+
}
171+
}
172+
}
173+
174+
/**
175+
* Write attribute if ...
176+
*
177+
* @param bool $condition
178+
* @param string $attribute
179+
* @param string $value
180+
*/
181+
public function writeAttributeIf($condition, $attribute, $value)
182+
{
183+
if ($condition) {
184+
$this->xmlWriter->writeAttribute($attribute, $value);
185+
}
186+
}
152187
}

src/PhpWord/Style/AbstractStyle.php

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ abstract class AbstractStyle
3838
*
3939
* This number starts from one and defined in Style::setStyleValues()
4040
*
41-
* @var integer|null
41+
* @var int|null
4242
*/
4343
protected $index;
4444

45+
/**
46+
* Aliases
47+
*
48+
* @var array
49+
*/
50+
protected $aliases = array();
51+
4552
/**
4653
* Get style name
4754
*
@@ -68,7 +75,7 @@ public function setStyleName($value)
6875
/**
6976
* Get index number
7077
*
71-
* @return integer|null
78+
* @return int|null
7279
*/
7380
public function getIndex()
7481
{
@@ -78,7 +85,7 @@ public function getIndex()
7885
/**
7986
* Set index number
8087
*
81-
* @param integer|null $value
88+
* @param int|null $value
8289
* @return self
8390
*/
8491
public function setIndex($value = null)
@@ -102,6 +109,9 @@ public function setIndex($value = null)
102109
*/
103110
public function setStyleValue($key, $value)
104111
{
112+
if (isset($this->aliases[$key])) {
113+
$key = $this->aliases[$key];
114+
}
105115
$method = 'set' . String::removeUnderscorePrefix($key);
106116
if (method_exists($this, $method)) {
107117
$this->$method($value);
@@ -150,6 +160,9 @@ protected function setNonEmptyVal($value, $default)
150160
*/
151161
protected function setBoolVal($value, $default = null)
152162
{
163+
if (is_string($value)) {
164+
$value = (bool)$value;
165+
}
153166
if (!is_bool($value)) {
154167
$value = $default;
155168
}
@@ -161,8 +174,8 @@ protected function setBoolVal($value, $default = null)
161174
* Set numeric value
162175
*
163176
* @param mixed $value
164-
* @param integer|float|null $default
165-
* @return integer|float|null
177+
* @param int|float|null $default
178+
* @return int|float|null
166179
*/
167180
protected function setNumericVal($value, $default = null)
168181
{
@@ -177,11 +190,14 @@ protected function setNumericVal($value, $default = null)
177190
* Set integer value
178191
*
179192
* @param mixed $value
180-
* @param integer|null $default
181-
* @return integer|null
193+
* @param int|null $default
194+
* @return int|null
182195
*/
183196
protected function setIntVal($value, $default = null)
184197
{
198+
if (is_string($value)) {
199+
$value = intval($value);
200+
}
185201
if (!is_int($value)) {
186202
$value = $default;
187203
}
@@ -198,6 +214,9 @@ protected function setIntVal($value, $default = null)
198214
*/
199215
protected function setFloatVal($value, $default = null)
200216
{
217+
if (is_string($value)) {
218+
$value = floatval($value);
219+
}
201220
if (!is_float($value)) {
202221
$value = $default;
203222
}
@@ -220,4 +239,38 @@ protected function setEnumVal($value, $enum, $default = null)
220239

221240
return $value;
222241
}
242+
243+
/**
244+
* Set object value
245+
*
246+
* @param mixed $value
247+
* @param string $styleName
248+
* @param mixed $style
249+
*/
250+
protected function setObjectVal($value, $styleName, &$style)
251+
{
252+
$styleClass = dirname(get_class($this)) . '\\' . $styleName;
253+
if (is_array($value)) {
254+
if (!$style instanceof $styleClass) {
255+
$style = new $styleClass();
256+
}
257+
$style->setStyleByArray($value);
258+
} else {
259+
$style = $value;
260+
}
261+
262+
return $style;
263+
}
264+
265+
/**
266+
* Set style using associative array
267+
*
268+
* @param array $style
269+
* @deprecated 0.11.0
270+
* @codeCoverageIgnore
271+
*/
272+
public function setArrayStyle(array $style = array())
273+
{
274+
return $this->setStyleByArray($style);
275+
}
223276
}

src/PhpWord/Style/Border.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,22 @@ public function getBorderBottomColor()
323323
{
324324
return $this->borderBottomColor;
325325
}
326+
327+
/**
328+
* Has borders?
329+
*
330+
* @return bool
331+
*/
332+
public function hasBorders()
333+
{
334+
$hasBorders = false;
335+
$borders = $this->getBorderSize();
336+
for ($i = 0; $i < count($borders); $i++) {
337+
if (!is_null($borders[$i])) {
338+
$hasBorders = true;
339+
}
340+
}
341+
342+
return $hasBorders;
343+
}
326344
}

src/PhpWord/Style/Cell.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Cell extends Border
4242
*
4343
* @var string
4444
*/
45-
private $valign;
45+
private $vAlign;
4646

4747
/**
4848
* Text Direction
@@ -80,7 +80,7 @@ class Cell extends Border
8080
*/
8181
public function getVAlign()
8282
{
83-
return $this->valign;
83+
return $this->vAlign;
8484
}
8585

8686
/**
@@ -90,7 +90,7 @@ public function getVAlign()
9090
*/
9191
public function setVAlign($value = null)
9292
{
93-
$this->valign = $value;
93+
$this->vAlign = $value;
9494
}
9595

9696
/**
@@ -183,19 +183,12 @@ public function getShading()
183183
/**
184184
* Set shading
185185
*
186-
* @param array $value
186+
* @param mixed $value
187187
* @return self
188188
*/
189189
public function setShading($value = null)
190190
{
191-
if (is_array($value)) {
192-
if (!$this->shading instanceof Shading) {
193-
$this->shading = new Shading();
194-
}
195-
$this->shading->setStyleByArray($value);
196-
} else {
197-
$this->shading = null;
198-
}
191+
$this->setObjectVal($value, 'Shading', $this->shading);
199192

200193
return $this;
201194
}

0 commit comments

Comments
 (0)