Add Media::reset(), Style::reset(), Footnote::reset(), and TOC::reset()

This commit is contained in:
Ivan Lanin 2014-04-05 00:57:50 +07:00
parent d7c18fe4b8
commit dd9faaee06
11 changed files with 114 additions and 71 deletions

View File

@ -21,6 +21,10 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
- CheckBox: Ability to add checkbox in header/footer - @ivanlanin GH-187
- Link: Ability to add link in header/footer - @ivanlanin GH-187
- Object: Ability to add object in header, footer, textrun, and footnote - @ivanlanin GH-187
- Media: Add `Media::reset()` to reset all media data - @juzi GH-19
- Style: Add `Style::reset()` to reset all styles
- Footnote: Add `Footnote::reset()` to reset all footnotes
- TOC: Add `TOC::reset()` to reset all TOC
### Bugfixes

View File

@ -59,6 +59,14 @@ class Footnote
return count(self::$elements);
}
/**
* Reset footer elements
*/
public static function reset()
{
self::$elements = array();
}
/**
* Add new Footnote Link Element
*

View File

@ -145,6 +145,14 @@ class Media
return $mediaElements;
}
/**
* Reset media elements
*/
public static function reset()
{
self::$media = array();
}
/**
* Add new Section Media Element
*

View File

@ -23,7 +23,7 @@ class Settings
*
* @var boolean
*/
private static $_xmlWriterCompatibility = true;
private static $xmlWriterCompatibility = true;
/**
* Name of the class used for Zip file management
@ -32,7 +32,7 @@ class Settings
*
* @var string
*/
private static $_zipClass = self::ZIPARCHIVE;
private static $zipClass = self::ZIPARCHIVE;
/**
* Set the compatibility option used by the XMLWriter
@ -43,7 +43,7 @@ class Settings
public static function setCompatibility($compatibility)
{
if (is_bool($compatibility)) {
self::$_xmlWriterCompatibility = $compatibility;
self::$xmlWriterCompatibility = $compatibility;
return true;
}
return false;
@ -56,7 +56,7 @@ class Settings
*/
public static function getCompatibility()
{
return self::$_xmlWriterCompatibility;
return self::$xmlWriterCompatibility;
}
/**
@ -70,7 +70,7 @@ class Settings
{
if (($zipClass === self::PCLZIP) ||
($zipClass === self::ZIPARCHIVE)) {
self::$_zipClass = $zipClass;
self::$zipClass = $zipClass;
return true;
}
return false;
@ -86,6 +86,6 @@ class Settings
*/
public static function getZipClass()
{
return self::$_zipClass;
return self::$zipClass;
} // function getZipClass()
}

View File

@ -23,7 +23,7 @@ class Style
*
* @var array
*/
private static $_styleElements = array();
private static $styles = array();
/**
* Add paragraph style
@ -33,17 +33,7 @@ class Style
*/
public static function addParagraphStyle($styleName, $styles)
{
if (!array_key_exists($styleName, self::$_styleElements)) {
$style = new Paragraph();
foreach ($styles as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$style->setStyleValue($key, $value);
}
self::$_styleElements[$styleName] = $style;
}
self::setStyleValues($styleName, $styles, new Paragraph());
}
/**
@ -55,16 +45,7 @@ class Style
*/
public static function addFontStyle($styleName, $styleFont, $styleParagraph = null)
{
if (!array_key_exists($styleName, self::$_styleElements)) {
$font = new Font('text', $styleParagraph);
foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$font->setStyleValue($key, $value);
}
self::$_styleElements[$styleName] = $font;
}
self::setStyleValues($styleName, $styleFont, new Font('text', $styleParagraph));
}
/**
@ -75,17 +56,7 @@ class Style
*/
public static function addLinkStyle($styleName, $styles)
{
if (!array_key_exists($styleName, self::$_styleElements)) {
$style = new Font('link');
foreach ($styles as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$style->setStyleValue($key, $value);
}
self::$_styleElements[$styleName] = $style;
}
self::setStyleValues($styleName, $styles, new Font('link'));
}
/**
@ -97,10 +68,10 @@ class Style
*/
public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
{
if (!array_key_exists($styleName, self::$_styleElements)) {
if (!array_key_exists($styleName, self::$styles)) {
$style = new Table($styleTable, $styleFirstRow);
self::$_styleElements[$styleName] = $style;
self::$styles[$styleName] = $style;
}
}
@ -114,17 +85,15 @@ class Style
public static function addTitleStyle($titleCount, $styleFont, $styleParagraph = null)
{
$styleName = 'Heading_' . $titleCount;
if (!array_key_exists($styleName, self::$_styleElements)) {
$font = new Font('title', $styleParagraph);
foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$font->setStyleValue($key, $value);
}
self::setStyleValues("Heading_{$titleCount}", $styleFont, new Font('title', $styleParagraph));
}
self::$_styleElements[$styleName] = $font;
}
/**
* Reset styles
*/
public static function reset()
{
self::$styles = array();
}
/**
@ -144,7 +113,7 @@ class Style
*/
public static function getStyles()
{
return self::$_styleElements;
return self::$styles;
}
/**
@ -154,10 +123,31 @@ class Style
*/
public static function getStyle($styleName)
{
if (array_key_exists($styleName, self::$_styleElements)) {
return self::$_styleElements[$styleName];
if (array_key_exists($styleName, self::$styles)) {
return self::$styles[$styleName];
} else {
return null;
}
}
/**
* Set style values
*
* @param string $styleName
* @param array $styleValues
* @param mixed $styleObject
*/
private static function setStyleValues($styleName, $styleValues, $styleObject)
{
if (!array_key_exists($styleName, self::$styles)) {
foreach ($styleValues as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$styleObject->setStyleValue($key, $value);
}
self::$styles[$styleName] = $styleObject;
}
}
}

View File

@ -22,35 +22,35 @@ class TOC
*
* @var array
*/
private static $_titles = array();
private static $titles = array();
/**
* TOC style
*
* @var TOCStyle
*/
private static $_styleTOC;
private static $TOCStyle;
/**
* Font style
*
* @var Font|array|string
*/
private static $_styleFont;
private static $fontStyle;
/**
* Title anchor
*
* @var int
*/
private static $_anchor = 252634154;
private static $anchor = 252634154;
/**
* Title bookmark
*
* @var int
*/
private static $_bookmarkId = 0;
private static $bookmarkId = 0;
/**
@ -61,28 +61,28 @@ class TOC
*/
public function __construct($styleFont = null, $styleTOC = null)
{
self::$_styleTOC = new TOCStyle();
self::$TOCStyle = new TOCStyle();
if (!is_null($styleTOC) && is_array($styleTOC)) {
foreach ($styleTOC as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
self::$_styleTOC->setStyleValue($key, $value);
self::$TOCStyle->setStyleValue($key, $value);
}
}
if (!is_null($styleFont)) {
if (is_array($styleFont)) {
self::$_styleFont = new Font();
self::$fontStyle = new Font();
foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
self::$_styleFont->setStyleValue($key, $value);
self::$fontStyle->setStyleValue($key, $value);
}
} else {
self::$_styleFont = $styleFont;
self::$fontStyle = $styleFont;
}
}
}
@ -96,8 +96,8 @@ class TOC
*/
public static function addTitle($text, $depth = 0)
{
$anchor = '_Toc' . ++self::$_anchor;
$bookmarkId = self::$_bookmarkId++;
$anchor = '_Toc' . ++self::$anchor;
$bookmarkId = self::$bookmarkId++;
$title = array();
$title['text'] = $text;
@ -105,7 +105,7 @@ class TOC
$title['anchor'] = $anchor;
$title['bookmarkId'] = $bookmarkId;
self::$_titles[] = $title;
self::$titles[] = $title;
return array($anchor, $bookmarkId);
}
@ -117,7 +117,15 @@ class TOC
*/
public static function getTitles()
{
return self::$_titles;
return self::$titles;
}
/**
* Reset footnotes
*/
public static function reset()
{
self::$titles = array();
}
/**
@ -127,7 +135,7 @@ class TOC
*/
public static function getStyleTOC()
{
return self::$_styleTOC;
return self::$TOCStyle;
}
/**
@ -137,6 +145,6 @@ class TOC
*/
public static function getStyleFont()
{
return self::$_styleFont;
return self::$fontStyle;
}
}

View File

@ -220,4 +220,15 @@ class HeaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($oHeader->getType(), Header::EVEN);
}
/**
* Add footnote exception
*
* @expectedException BadMethodCallException
*/
public function testAddFootnoteException()
{
$header = new Header(1);
$header->addFootnote();
}
}

View File

@ -31,5 +31,8 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, $rIdLink);
$this->assertEquals(1, count(Footnote::getFootnoteElements()));
$this->assertEquals(1, count(Footnote::getFootnoteLinkElements()));
Footnote::reset();
$this->assertEquals(0, count(Footnote::getFootnoteElements()));
}
}

View File

@ -83,7 +83,7 @@ class MediaTest extends \PHPUnit_Framework_TestCase
}
/**
* Add footer media element
* Add footer media element and reset media
*/
public function testAddFooterMediaElement()
{
@ -94,5 +94,8 @@ class MediaTest extends \PHPUnit_Framework_TestCase
Media::addMediaElement('footer1', 'image', $remote, new Image($remote));
$this->assertEquals(2, Media::countMediaElements('footer1'));
Media::reset();
$this->assertEquals(0, Media::countMediaElements('footer1'));
}
}

View File

@ -43,6 +43,10 @@ class StyleTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf("PhpOffice\\PhpWord\\Style\\{$style}", Style::getStyle($name));
}
$this->assertNull(Style::getStyle('Unknown'));
Style::reset();
$this->assertEquals(0, count(Style::getStyles()));
}
/**

View File

@ -78,5 +78,9 @@ class TOCTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($depth, $savedTitles[$i]['depth']);
$i++;
}
TOC::reset();
$this->assertEquals(0, count(TOC::getTitles()));
}
}