Skip to content

Removed fake namespacing from tests and reformatted code to PSR-2 coding standards #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Classes/PHPWord/Exceptions/InvalidStyleException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace PHPWord\Exceptions;

use InvalidArgumentException;

/**
* InvalidStyleException
*
* Exception used for when a style value is invalid
*
* @package PHPWord
*/
class InvalidStyleException extends InvalidArgumentException
{
}
17 changes: 12 additions & 5 deletions Classes/PHPWord/Section/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PHPWord_Section_Text
/**
* Paragraph style
*
* @var PHPWord_Style_Font
* @var \PHPWord_Style_Paragraph
*/
private $_styleParagraph;

Expand Down Expand Up @@ -116,22 +116,29 @@ public function getParagraphStyle()
/**
* Set Paragraph style
*
* @return PHPWord_Style_Paragraph
* @param array|\PHPWord_Style_Paragraph $styleParagraph
* @return \PHPWord_Style_Paragraph
* @throws \Exception
*/
public function setParagraphStyle($styleParagraph)
{
if (is_array($styleParagraph)) {
$this->_styleParagraph = new PHPWord_Style_Paragraph();

foreach ($styleParagraph as $key => $value) {
if (substr($key, 0, 1) != '_') {
if ($key === 'line-height') {
null;
} elseif (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_styleParagraph->setStyleValue($key, $value);
}
} else {
} elseif ($styleParagraph instanceof PHPWord_Style_Paragraph) {
$this->_styleParagraph = $styleParagraph;
} else {
throw new Exception('Expected array or PHPWord_Style_Paragraph');
}
return $this->_styleParagraph;
}

/**
Expand All @@ -143,4 +150,4 @@ public function getText()
{
return $this->_text;
}
}
}
29 changes: 14 additions & 15 deletions Classes/PHPWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
class PHPWord_Style_Font
{

const UNDERLINE_NONE = 'none';
const UNDERLINE_DASH = 'dash';
const UNDERLINE_DASHHEAVY = 'dashHeavy';
Expand Down Expand Up @@ -153,8 +152,8 @@ class PHPWord_Style_Font
/**
* New font style
*
* @param string $type Type of font
* @param array $styleParagraph Paragraph styles definition
* @param string $type Type of font
* @param array $styleParagraph Paragraph styles definition
*/
public function __construct($type = 'text', $styleParagraph = null)
{
Expand Down Expand Up @@ -187,8 +186,8 @@ public function __construct($type = 'text', $styleParagraph = null)
/**
* Set style value
*
* @param string $key
* @param mixed $value
* @param string $key
* @param mixed $value
*/
public function setStyleValue($key, $value)
{
Expand All @@ -211,7 +210,7 @@ public function getName()
/**
* Set font name
*
* @param string $pValue
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setName($pValue = PHPWord::DEFAULT_FONT_NAME)
Expand All @@ -236,7 +235,7 @@ public function getSize()
/**
* Set font size
*
* @param int|float $pValue
* @param int|float $pValue
* @return PHPWord_Style_Font
*/
public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE)
Expand All @@ -261,7 +260,7 @@ public function getBold()
/**
* Set bold
*
* @param bool $pValue
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setBold($pValue = false)
Expand All @@ -286,7 +285,7 @@ public function getItalic()
/**
* Set italics
*
* @param bool $pValue
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setItalic($pValue = false)
Expand All @@ -311,7 +310,7 @@ public function getSuperScript()
/**
* Set superscript
*
* @param bool $pValue
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setSuperScript($pValue = false)
Expand All @@ -337,7 +336,7 @@ public function getSubScript()
/**
* Set subscript
*
* @param bool $pValue
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setSubScript($pValue = false)
Expand All @@ -363,7 +362,7 @@ public function getUnderline()
/**
* Set underline
*
* @param string $pValue
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE)
Expand All @@ -388,7 +387,7 @@ public function getStrikethrough()
/**
* Set strikethrough
*
* @param bool $pValue
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setStrikethrough($pValue = false)
Expand All @@ -413,7 +412,7 @@ public function getColor()
/**
* Set font color
*
* @param string $pValue
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setColor($pValue = PHPWord::DEFAULT_FONT_COLOR)
Expand All @@ -438,7 +437,7 @@ public function getFgColor()
/**
* Set foreground/highlight color
*
* @param string $pValue
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setFgColor($pValue = null)
Expand Down
92 changes: 57 additions & 35 deletions Classes/PHPWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@
* @version 0.7.0
*/

use PHPWord\Exceptions\InvalidStyleException;

/**
* PHPWord_Style_Paragraph
*/
class PHPWord_Style_Paragraph
{
const LINE_HEIGHT = 240;

/*
* Text line height
*
* @var int
*/
private $lineHeight;

/**
* Paragraph alignment
Expand Down Expand Up @@ -85,7 +95,7 @@ class PHPWord_Style_Paragraph
*
* @var string
*/
private $_basedOn;
private $_basedOn = 'Normal';

/**
* Style for next paragraph
Expand All @@ -99,63 +109,46 @@ class PHPWord_Style_Paragraph
*
* @var bool
*/
private $_widowControl;
private $_widowControl = true;

/**
* Keep paragraph with next paragraph
*
* @var bool
*/
private $_keepNext;
private $_keepNext = false;

/**
* Keep all lines on one page
*
* @var bool
*/
private $_keepLines;
private $_keepLines = false;

/**
* Start paragraph on next page
*
* @var bool
*/
private $_pageBreakBefore;

/**
* New Paragraph Style
*/
public function __construct()
{
$this->_align = null;
$this->_spaceBefore = null;
$this->_spaceAfter = null;
$this->_spacing = null;
$this->_tabs = null;
$this->_indent = null;
$this->_hanging = null;
$this->_basedOn = 'Normal';
$this->_next = null;
$this->_widowControl = true;
$this->_keepNext = false;
$this->_keepLines = false;
$this->_pageBreakBefore = false;
}
private $_pageBreakBefore = false;

/**
* Set Style value
*
* @param string $key
* @param mixed $value
* @param string $key
* @param mixed $value
*/
public function setStyleValue($key, $value)
{
if ($key == '_indent' || $key == '_hanging') {
$value = $value * 720;
}
if ($key == '_spacing') {
} elseif ($key == '_spacing') {
$value += 240; // because line height of 1 matches 240 twips
} elseif ($key === 'line-height') {
$this->setLineHeight($value);
return;
}
$this->$key = $value;
$method = 'set' . substr($key, 1);
if (method_exists($this, $method)) {
$this->$method($value);
Expand Down Expand Up @@ -335,7 +328,7 @@ public function getBasedOn()
/**
* Set parent style ID
*
* @param string $pValue
* @param string $pValue
* @return PHPWord_Style_Paragraph
*/
public function setBasedOn($pValue = 'Normal')
Expand All @@ -357,7 +350,7 @@ public function getNext()
/**
* Set style for next paragraph
*
* @param string $pValue
* @param string $pValue
* @return PHPWord_Style_Paragraph
*/
public function setNext($pValue = null)
Expand All @@ -379,7 +372,7 @@ public function getWidowControl()
/**
* Set keep paragraph with next paragraph setting
*
* @param bool $pValue
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setWidowControl($pValue = true)
Expand All @@ -404,7 +397,7 @@ public function getKeepNext()
/**
* Set keep paragraph with next paragraph setting
*
* @param bool $pValue
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setKeepNext($pValue = false)
Expand All @@ -429,7 +422,7 @@ public function getKeepLines()
/**
* Set keep all lines on one page setting
*
* @param bool $pValue
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setKeepLines($pValue = false)
Expand All @@ -454,7 +447,7 @@ public function getPageBreakBefore()
/**
* Set start paragraph on next page setting
*
* @param bool $pValue
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setPageBreakBefore($pValue = false)
Expand All @@ -466,4 +459,33 @@ public function setPageBreakBefore($pValue = false)
return $this;
}

/**
* Set the line height
*
* @param int|float|string $lineHeight
* @return $this
* @throws \PHPWord\Exceptions\InvalidStyleException
*/
public function setLineHeight($lineHeight)
{
if (is_string($lineHeight)) {
$lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight));
}

if ((!is_integer($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {
throw new InvalidStyleException('Line height must be a valid number');
}

$this->lineHeight = $lineHeight;
$this->setSpacing($lineHeight * self::LINE_HEIGHT);
return $this;
}

/**
* @return int
*/
public function getLineHeight()
{
return $this->lineHeight;
}
}
Loading