Skip to content

Commit 8883667

Browse files
committed
Merge pull request #95 from gabrielbull/develop
Removed fake namespacing from tests and reformatted code to PSR-2 coding standards
2 parents 99b43ce + 69a8192 commit 8883667

File tree

18 files changed

+336
-262
lines changed

18 files changed

+336
-262
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace PHPWord\Exceptions;
3+
4+
use InvalidArgumentException;
5+
6+
/**
7+
* InvalidStyleException
8+
*
9+
* Exception used for when a style value is invalid
10+
*
11+
* @package PHPWord
12+
*/
13+
class InvalidStyleException extends InvalidArgumentException
14+
{
15+
}

Classes/PHPWord/Section/Text.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class PHPWord_Section_Text
4848
/**
4949
* Paragraph style
5050
*
51-
* @var PHPWord_Style_Font
51+
* @var \PHPWord_Style_Paragraph
5252
*/
5353
private $_styleParagraph;
5454

@@ -116,22 +116,29 @@ public function getParagraphStyle()
116116
/**
117117
* Set Paragraph style
118118
*
119-
* @return PHPWord_Style_Paragraph
119+
* @param array|\PHPWord_Style_Paragraph $styleParagraph
120+
* @return \PHPWord_Style_Paragraph
121+
* @throws \Exception
120122
*/
121123
public function setParagraphStyle($styleParagraph)
122124
{
123125
if (is_array($styleParagraph)) {
124126
$this->_styleParagraph = new PHPWord_Style_Paragraph();
125127

126128
foreach ($styleParagraph as $key => $value) {
127-
if (substr($key, 0, 1) != '_') {
129+
if ($key === 'line-height') {
130+
null;
131+
} elseif (substr($key, 0, 1) != '_') {
128132
$key = '_' . $key;
129133
}
130134
$this->_styleParagraph->setStyleValue($key, $value);
131135
}
132-
} else {
136+
} elseif ($styleParagraph instanceof PHPWord_Style_Paragraph) {
133137
$this->_styleParagraph = $styleParagraph;
138+
} else {
139+
throw new Exception('Expected array or PHPWord_Style_Paragraph');
134140
}
141+
return $this->_styleParagraph;
135142
}
136143

137144
/**
@@ -143,4 +150,4 @@ public function getText()
143150
{
144151
return $this->_text;
145152
}
146-
}
153+
}

Classes/PHPWord/Style/Font.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
*/
3131
class PHPWord_Style_Font
3232
{
33-
3433
const UNDERLINE_NONE = 'none';
3534
const UNDERLINE_DASH = 'dash';
3635
const UNDERLINE_DASHHEAVY = 'dashHeavy';
@@ -153,8 +152,8 @@ class PHPWord_Style_Font
153152
/**
154153
* New font style
155154
*
156-
* @param string $type Type of font
157-
* @param array $styleParagraph Paragraph styles definition
155+
* @param string $type Type of font
156+
* @param array $styleParagraph Paragraph styles definition
158157
*/
159158
public function __construct($type = 'text', $styleParagraph = null)
160159
{
@@ -187,8 +186,8 @@ public function __construct($type = 'text', $styleParagraph = null)
187186
/**
188187
* Set style value
189188
*
190-
* @param string $key
191-
* @param mixed $value
189+
* @param string $key
190+
* @param mixed $value
192191
*/
193192
public function setStyleValue($key, $value)
194193
{
@@ -211,7 +210,7 @@ public function getName()
211210
/**
212211
* Set font name
213212
*
214-
* @param string $pValue
213+
* @param string $pValue
215214
* @return PHPWord_Style_Font
216215
*/
217216
public function setName($pValue = PHPWord::DEFAULT_FONT_NAME)
@@ -236,7 +235,7 @@ public function getSize()
236235
/**
237236
* Set font size
238237
*
239-
* @param int|float $pValue
238+
* @param int|float $pValue
240239
* @return PHPWord_Style_Font
241240
*/
242241
public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE)
@@ -261,7 +260,7 @@ public function getBold()
261260
/**
262261
* Set bold
263262
*
264-
* @param bool $pValue
263+
* @param bool $pValue
265264
* @return PHPWord_Style_Font
266265
*/
267266
public function setBold($pValue = false)
@@ -286,7 +285,7 @@ public function getItalic()
286285
/**
287286
* Set italics
288287
*
289-
* @param bool $pValue
288+
* @param bool $pValue
290289
* @return PHPWord_Style_Font
291290
*/
292291
public function setItalic($pValue = false)
@@ -311,7 +310,7 @@ public function getSuperScript()
311310
/**
312311
* Set superscript
313312
*
314-
* @param bool $pValue
313+
* @param bool $pValue
315314
* @return PHPWord_Style_Font
316315
*/
317316
public function setSuperScript($pValue = false)
@@ -337,7 +336,7 @@ public function getSubScript()
337336
/**
338337
* Set subscript
339338
*
340-
* @param bool $pValue
339+
* @param bool $pValue
341340
* @return PHPWord_Style_Font
342341
*/
343342
public function setSubScript($pValue = false)
@@ -363,7 +362,7 @@ public function getUnderline()
363362
/**
364363
* Set underline
365364
*
366-
* @param string $pValue
365+
* @param string $pValue
367366
* @return PHPWord_Style_Font
368367
*/
369368
public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE)
@@ -388,7 +387,7 @@ public function getStrikethrough()
388387
/**
389388
* Set strikethrough
390389
*
391-
* @param bool $pValue
390+
* @param bool $pValue
392391
* @return PHPWord_Style_Font
393392
*/
394393
public function setStrikethrough($pValue = false)
@@ -413,7 +412,7 @@ public function getColor()
413412
/**
414413
* Set font color
415414
*
416-
* @param string $pValue
415+
* @param string $pValue
417416
* @return PHPWord_Style_Font
418417
*/
419418
public function setColor($pValue = PHPWord::DEFAULT_FONT_COLOR)
@@ -438,7 +437,7 @@ public function getFgColor()
438437
/**
439438
* Set foreground/highlight color
440439
*
441-
* @param string $pValue
440+
* @param string $pValue
442441
* @return PHPWord_Style_Font
443442
*/
444443
public function setFgColor($pValue = null)

Classes/PHPWord/Style/Paragraph.php

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@
2525
* @version 0.7.0
2626
*/
2727

28+
use PHPWord\Exceptions\InvalidStyleException;
29+
2830
/**
2931
* PHPWord_Style_Paragraph
3032
*/
3133
class PHPWord_Style_Paragraph
3234
{
35+
const LINE_HEIGHT = 240;
36+
37+
/*
38+
* Text line height
39+
*
40+
* @var int
41+
*/
42+
private $lineHeight;
3343

3444
/**
3545
* Paragraph alignment
@@ -85,7 +95,7 @@ class PHPWord_Style_Paragraph
8595
*
8696
* @var string
8797
*/
88-
private $_basedOn;
98+
private $_basedOn = 'Normal';
8999

90100
/**
91101
* Style for next paragraph
@@ -99,63 +109,46 @@ class PHPWord_Style_Paragraph
99109
*
100110
* @var bool
101111
*/
102-
private $_widowControl;
112+
private $_widowControl = true;
103113

104114
/**
105115
* Keep paragraph with next paragraph
106116
*
107117
* @var bool
108118
*/
109-
private $_keepNext;
119+
private $_keepNext = false;
110120

111121
/**
112122
* Keep all lines on one page
113123
*
114124
* @var bool
115125
*/
116-
private $_keepLines;
126+
private $_keepLines = false;
117127

118128
/**
119129
* Start paragraph on next page
120130
*
121131
* @var bool
122132
*/
123-
private $_pageBreakBefore;
124-
125-
/**
126-
* New Paragraph Style
127-
*/
128-
public function __construct()
129-
{
130-
$this->_align = null;
131-
$this->_spaceBefore = null;
132-
$this->_spaceAfter = null;
133-
$this->_spacing = null;
134-
$this->_tabs = null;
135-
$this->_indent = null;
136-
$this->_hanging = null;
137-
$this->_basedOn = 'Normal';
138-
$this->_next = null;
139-
$this->_widowControl = true;
140-
$this->_keepNext = false;
141-
$this->_keepLines = false;
142-
$this->_pageBreakBefore = false;
143-
}
133+
private $_pageBreakBefore = false;
144134

145135
/**
146136
* Set Style value
147137
*
148-
* @param string $key
149-
* @param mixed $value
138+
* @param string $key
139+
* @param mixed $value
150140
*/
151141
public function setStyleValue($key, $value)
152142
{
153143
if ($key == '_indent' || $key == '_hanging') {
154144
$value = $value * 720;
155-
}
156-
if ($key == '_spacing') {
145+
} elseif ($key == '_spacing') {
157146
$value += 240; // because line height of 1 matches 240 twips
147+
} elseif ($key === 'line-height') {
148+
$this->setLineHeight($value);
149+
return;
158150
}
151+
$this->$key = $value;
159152
$method = 'set' . substr($key, 1);
160153
if (method_exists($this, $method)) {
161154
$this->$method($value);
@@ -335,7 +328,7 @@ public function getBasedOn()
335328
/**
336329
* Set parent style ID
337330
*
338-
* @param string $pValue
331+
* @param string $pValue
339332
* @return PHPWord_Style_Paragraph
340333
*/
341334
public function setBasedOn($pValue = 'Normal')
@@ -357,7 +350,7 @@ public function getNext()
357350
/**
358351
* Set style for next paragraph
359352
*
360-
* @param string $pValue
353+
* @param string $pValue
361354
* @return PHPWord_Style_Paragraph
362355
*/
363356
public function setNext($pValue = null)
@@ -379,7 +372,7 @@ public function getWidowControl()
379372
/**
380373
* Set keep paragraph with next paragraph setting
381374
*
382-
* @param bool $pValue
375+
* @param bool $pValue
383376
* @return PHPWord_Style_Paragraph
384377
*/
385378
public function setWidowControl($pValue = true)
@@ -404,7 +397,7 @@ public function getKeepNext()
404397
/**
405398
* Set keep paragraph with next paragraph setting
406399
*
407-
* @param bool $pValue
400+
* @param bool $pValue
408401
* @return PHPWord_Style_Paragraph
409402
*/
410403
public function setKeepNext($pValue = false)
@@ -429,7 +422,7 @@ public function getKeepLines()
429422
/**
430423
* Set keep all lines on one page setting
431424
*
432-
* @param bool $pValue
425+
* @param bool $pValue
433426
* @return PHPWord_Style_Paragraph
434427
*/
435428
public function setKeepLines($pValue = false)
@@ -454,7 +447,7 @@ public function getPageBreakBefore()
454447
/**
455448
* Set start paragraph on next page setting
456449
*
457-
* @param bool $pValue
450+
* @param bool $pValue
458451
* @return PHPWord_Style_Paragraph
459452
*/
460453
public function setPageBreakBefore($pValue = false)
@@ -466,4 +459,33 @@ public function setPageBreakBefore($pValue = false)
466459
return $this;
467460
}
468461

462+
/**
463+
* Set the line height
464+
*
465+
* @param int|float|string $lineHeight
466+
* @return $this
467+
* @throws \PHPWord\Exceptions\InvalidStyleException
468+
*/
469+
public function setLineHeight($lineHeight)
470+
{
471+
if (is_string($lineHeight)) {
472+
$lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight));
473+
}
474+
475+
if ((!is_integer($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {
476+
throw new InvalidStyleException('Line height must be a valid number');
477+
}
478+
479+
$this->lineHeight = $lineHeight;
480+
$this->setSpacing($lineHeight * self::LINE_HEIGHT);
481+
return $this;
482+
}
483+
484+
/**
485+
* @return int
486+
*/
487+
public function getLineHeight()
488+
{
489+
return $this->lineHeight;
490+
}
469491
}

0 commit comments

Comments
 (0)