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 - CheckBox: Ability to add checkbox in header/footer - @ivanlanin GH-187
- Link: Ability to add link 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 - 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 ### Bugfixes

View File

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

View File

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

View File

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

View File

@ -23,7 +23,7 @@ class Style
* *
* @var array * @var array
*/ */
private static $_styleElements = array(); private static $styles = array();
/** /**
* Add paragraph style * Add paragraph style
@ -33,17 +33,7 @@ class Style
*/ */
public static function addParagraphStyle($styleName, $styles) public static function addParagraphStyle($styleName, $styles)
{ {
if (!array_key_exists($styleName, self::$_styleElements)) { self::setStyleValues($styleName, $styles, new Paragraph());
$style = new Paragraph();
foreach ($styles as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$style->setStyleValue($key, $value);
}
self::$_styleElements[$styleName] = $style;
}
} }
/** /**
@ -55,16 +45,7 @@ class Style
*/ */
public static function addFontStyle($styleName, $styleFont, $styleParagraph = null) public static function addFontStyle($styleName, $styleFont, $styleParagraph = null)
{ {
if (!array_key_exists($styleName, self::$_styleElements)) { self::setStyleValues($styleName, $styleFont, new Font('text', $styleParagraph));
$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;
}
} }
/** /**
@ -75,17 +56,7 @@ class Style
*/ */
public static function addLinkStyle($styleName, $styles) public static function addLinkStyle($styleName, $styles)
{ {
if (!array_key_exists($styleName, self::$_styleElements)) { self::setStyleValues($styleName, $styles, new Font('link'));
$style = new Font('link');
foreach ($styles as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$style->setStyleValue($key, $value);
}
self::$_styleElements[$styleName] = $style;
}
} }
/** /**
@ -97,10 +68,10 @@ class Style
*/ */
public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null) 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); $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) public static function addTitleStyle($titleCount, $styleFont, $styleParagraph = null)
{ {
$styleName = 'Heading_' . $titleCount; $styleName = 'Heading_' . $titleCount;
if (!array_key_exists($styleName, self::$_styleElements)) { self::setStyleValues("Heading_{$titleCount}", $styleFont, new Font('title', $styleParagraph));
$font = new Font('title', $styleParagraph); }
foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$font->setStyleValue($key, $value);
}
self::$_styleElements[$styleName] = $font; /**
} * Reset styles
*/
public static function reset()
{
self::$styles = array();
} }
/** /**
@ -144,7 +113,7 @@ class Style
*/ */
public static function getStyles() public static function getStyles()
{ {
return self::$_styleElements; return self::$styles;
} }
/** /**
@ -154,10 +123,31 @@ class Style
*/ */
public static function getStyle($styleName) public static function getStyle($styleName)
{ {
if (array_key_exists($styleName, self::$_styleElements)) { if (array_key_exists($styleName, self::$styles)) {
return self::$_styleElements[$styleName]; return self::$styles[$styleName];
} else { } else {
return null; 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 * @var array
*/ */
private static $_titles = array(); private static $titles = array();
/** /**
* TOC style * TOC style
* *
* @var TOCStyle * @var TOCStyle
*/ */
private static $_styleTOC; private static $TOCStyle;
/** /**
* Font style * Font style
* *
* @var Font|array|string * @var Font|array|string
*/ */
private static $_styleFont; private static $fontStyle;
/** /**
* Title anchor * Title anchor
* *
* @var int * @var int
*/ */
private static $_anchor = 252634154; private static $anchor = 252634154;
/** /**
* Title bookmark * Title bookmark
* *
* @var int * @var int
*/ */
private static $_bookmarkId = 0; private static $bookmarkId = 0;
/** /**
@ -61,28 +61,28 @@ class TOC
*/ */
public function __construct($styleFont = null, $styleTOC = null) public function __construct($styleFont = null, $styleTOC = null)
{ {
self::$_styleTOC = new TOCStyle(); self::$TOCStyle = new TOCStyle();
if (!is_null($styleTOC) && is_array($styleTOC)) { if (!is_null($styleTOC) && is_array($styleTOC)) {
foreach ($styleTOC as $key => $value) { foreach ($styleTOC as $key => $value) {
if (substr($key, 0, 1) != '_') { if (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;
} }
self::$_styleTOC->setStyleValue($key, $value); self::$TOCStyle->setStyleValue($key, $value);
} }
} }
if (!is_null($styleFont)) { if (!is_null($styleFont)) {
if (is_array($styleFont)) { if (is_array($styleFont)) {
self::$_styleFont = new Font(); self::$fontStyle = new Font();
foreach ($styleFont as $key => $value) { foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') { if (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;
} }
self::$_styleFont->setStyleValue($key, $value); self::$fontStyle->setStyleValue($key, $value);
} }
} else { } else {
self::$_styleFont = $styleFont; self::$fontStyle = $styleFont;
} }
} }
} }
@ -96,8 +96,8 @@ class TOC
*/ */
public static function addTitle($text, $depth = 0) public static function addTitle($text, $depth = 0)
{ {
$anchor = '_Toc' . ++self::$_anchor; $anchor = '_Toc' . ++self::$anchor;
$bookmarkId = self::$_bookmarkId++; $bookmarkId = self::$bookmarkId++;
$title = array(); $title = array();
$title['text'] = $text; $title['text'] = $text;
@ -105,7 +105,7 @@ class TOC
$title['anchor'] = $anchor; $title['anchor'] = $anchor;
$title['bookmarkId'] = $bookmarkId; $title['bookmarkId'] = $bookmarkId;
self::$_titles[] = $title; self::$titles[] = $title;
return array($anchor, $bookmarkId); return array($anchor, $bookmarkId);
} }
@ -117,7 +117,15 @@ class TOC
*/ */
public static function getTitles() 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() public static function getStyleTOC()
{ {
return self::$_styleTOC; return self::$TOCStyle;
} }
/** /**
@ -137,6 +145,6 @@ class TOC
*/ */
public static function getStyleFont() 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); $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, $rIdLink);
$this->assertEquals(1, count(Footnote::getFootnoteElements())); $this->assertEquals(1, count(Footnote::getFootnoteElements()));
$this->assertEquals(1, count(Footnote::getFootnoteLinkElements())); $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() public function testAddFooterMediaElement()
{ {
@ -94,5 +94,8 @@ class MediaTest extends \PHPUnit_Framework_TestCase
Media::addMediaElement('footer1', 'image', $remote, new Image($remote)); Media::addMediaElement('footer1', 'image', $remote, new Image($remote));
$this->assertEquals(2, Media::countMediaElements('footer1')); $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->assertInstanceOf("PhpOffice\\PhpWord\\Style\\{$style}", Style::getStyle($name));
} }
$this->assertNull(Style::getStyle('Unknown')); $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']); $this->assertEquals($depth, $savedTitles[$i]['depth']);
$i++; $i++;
} }
TOC::reset();
$this->assertEquals(0, count(TOC::getTitles()));
} }
} }