diff --git a/CHANGELOG.md b/CHANGELOG.md index 80a6253a..fea9e22c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/PhpWord/Footnote.php b/src/PhpWord/Footnote.php index f656dc3a..1c11bbff 100644 --- a/src/PhpWord/Footnote.php +++ b/src/PhpWord/Footnote.php @@ -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 * diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index d354e399..63c88361 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -145,6 +145,14 @@ class Media return $mediaElements; } + /** + * Reset media elements + */ + public static function reset() + { + self::$media = array(); + } + /** * Add new Section Media Element * diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 0fb8bad0..0af6cf72 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -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() } diff --git a/src/PhpWord/Style.php b/src/PhpWord/Style.php index f7b8e4c6..3db67cb5 100755 --- a/src/PhpWord/Style.php +++ b/src/PhpWord/Style.php @@ -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; + } + } } diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index bd1c3f38..0dc7c874 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -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; } } diff --git a/tests/PhpWord/Tests/Container/HeaderTest.php b/tests/PhpWord/Tests/Container/HeaderTest.php index d7425bdc..9d13d57c 100644 --- a/tests/PhpWord/Tests/Container/HeaderTest.php +++ b/tests/PhpWord/Tests/Container/HeaderTest.php @@ -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(); + } } diff --git a/tests/PhpWord/Tests/FootnoteTest.php b/tests/PhpWord/Tests/FootnoteTest.php index 955d02e5..47aea9a3 100644 --- a/tests/PhpWord/Tests/FootnoteTest.php +++ b/tests/PhpWord/Tests/FootnoteTest.php @@ -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())); } } diff --git a/tests/PhpWord/Tests/MediaTest.php b/tests/PhpWord/Tests/MediaTest.php index b1a49059..3df52e4a 100644 --- a/tests/PhpWord/Tests/MediaTest.php +++ b/tests/PhpWord/Tests/MediaTest.php @@ -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')); } } diff --git a/tests/PhpWord/Tests/StyleTest.php b/tests/PhpWord/Tests/StyleTest.php index 6381cbc9..b323ece4 100644 --- a/tests/PhpWord/Tests/StyleTest.php +++ b/tests/PhpWord/Tests/StyleTest.php @@ -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())); + } /** diff --git a/tests/PhpWord/Tests/TOCTest.php b/tests/PhpWord/Tests/TOCTest.php index 100206a4..0a060eb8 100644 --- a/tests/PhpWord/Tests/TOCTest.php +++ b/tests/PhpWord/Tests/TOCTest.php @@ -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())); + } }