From 8a5433e5df1f76d2bfa554b99e08e2bd79e05779 Mon Sep 17 00:00:00 2001 From: troosan Date: Tue, 4 Jul 2017 15:04:30 +0200 Subject: [PATCH] add new NumberFormat values and refactor other uses --- samples/Sample_06_Footnote.php | 5 +- .../FootnoteProperties.php | 89 ++++++---- src/PhpWord/Element/Section.php | 2 +- src/PhpWord/Shared/AbstractEnum.php | 35 ++++ src/PhpWord/SimpleType/NumberFormat.php | 153 ++++++++++++++++++ src/PhpWord/Style/NumberingLevel.php | 16 +- src/PhpWord/Writer/Word2007/Part/Document.php | 1 - .../FootnotePropertiesTest.php | 13 +- .../Writer/Word2007/Part/DocumentTest.php | 7 +- .../Writer/Word2007/Part/NumberingTest.php | 3 +- 10 files changed, 271 insertions(+), 53 deletions(-) rename src/PhpWord/{SimpleType => ComplexType}/FootnoteProperties.php (67%) create mode 100644 src/PhpWord/Shared/AbstractEnum.php create mode 100644 src/PhpWord/SimpleType/NumberFormat.php rename tests/PhpWord/{SimpleType => ComplexType}/FootnotePropertiesTest.php (83%) diff --git a/samples/Sample_06_Footnote.php b/samples/Sample_06_Footnote.php index f9c6b5f7..19d6a524 100644 --- a/samples/Sample_06_Footnote.php +++ b/samples/Sample_06_Footnote.php @@ -1,5 +1,6 @@ addFootnote(); $footnote->addText('The reference for this is wrapped in its own line'); $footnoteProperties = new FootnoteProperties(); -$footnoteProperties->setNumFmt(FootnoteProperties::NUMBER_FORMAT_UPPER_ROMAN); +$footnoteProperties->setNumFmt(NumberFormat::DECIMAL_ENCLOSED_CIRCLE); $section->setFootnoteProperties($footnoteProperties); // Save file diff --git a/src/PhpWord/SimpleType/FootnoteProperties.php b/src/PhpWord/ComplexType/FootnoteProperties.php similarity index 67% rename from src/PhpWord/SimpleType/FootnoteProperties.php rename to src/PhpWord/ComplexType/FootnoteProperties.php index f98f0a94..882834d5 100644 --- a/src/PhpWord/SimpleType/FootnoteProperties.php +++ b/src/PhpWord/ComplexType/FootnoteProperties.php @@ -14,7 +14,9 @@ * @copyright 2010-2016 PHPWord contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ -namespace PhpOffice\PhpWord\SimpleType; +namespace PhpOffice\PhpWord\ComplexType; + +use PhpOffice\PhpWord\SimpleType\NumberFormat; /** * Footnote properties @@ -28,17 +30,6 @@ final class FootnoteProperties const RESTART_NUMBER_EACH_SECTION = 'eachSect'; const RESTART_NUMBER_EACH_PAGE = 'eachPage'; - const NUMBER_FORMAT_DECIMAL = 'decimal'; - const NUMBER_FORMAT_UPPER_ROMAN = 'upperRoman'; - const NUMBER_FORMAT_LOWER_ROMAN = 'lowerRoman'; - const NUMBER_FORMAT_UPPER_LETTER = 'upperLetter'; - const NUMBER_FORMAT_LOWER_LETTER = 'lowerLetter'; - const NUMBER_FORMAT_ORDINAL = 'ordinal'; - const NUMBER_FORMAT_CARDINAL_TEXT = 'cardinalText'; - const NUMBER_FORMAT_ORDINAL_TEXT = 'ordinalText'; - const NUMBER_FORMAT_NONE = 'none'; - const NUMBER_FORMAT_BULLET = 'bullet'; - const POSITION_PAGE_BOTTOM = 'pageBottom'; const POSITION_BENEATH_TEXT = 'beneathText'; const POSITION_SECTION_END = 'sectEnd'; @@ -52,7 +43,7 @@ final class FootnoteProperties private $pos; /** - * Footnote Numbering Format + * Footnote Numbering Format w:numFmt, one of PhpOffice\PhpWord\SimpleType\NumberFormat * * @var string */ @@ -61,7 +52,7 @@ final class FootnoteProperties /** * Footnote and Endnote Numbering Starting Value * - * @var decimal + * @var double */ private $numStart; @@ -72,11 +63,23 @@ final class FootnoteProperties */ private $numRestart; + /** + * Get the Footnote Positioning Location + * + * @return string + */ public function getPos() { return $this->pos; } + /** + * Set the Footnote Positioning Location (pageBottom, beneathText, sectEnd, docEnd) + * + * @param string $pos + * @throws \InvalidArgumentException + * @return self + */ public function setPos($pos) { $position = array( @@ -91,50 +94,71 @@ final class FootnoteProperties } else { throw new \InvalidArgumentException("Invalid value, on of " . implode(', ', $position) . " possible"); } + return $this; } + /** + * Get the Footnote Numbering Format + * + * @return string + */ public function getNumFmt() { return $this->numFmt; } + /** + * Set the Footnote Numbering Format + * + * @param string $numFmt One of NumberFormat + * @return self + */ public function setNumFmt($numFmt) { - $numberFormat = array( - self::NUMBER_FORMAT_DECIMAL, - self::NUMBER_FORMAT_UPPER_ROMAN, - self::NUMBER_FORMAT_LOWER_ROMAN, - self::NUMBER_FORMAT_UPPER_LETTER, - self::NUMBER_FORMAT_LOWER_LETTER, - self::NUMBER_FORMAT_ORDINAL, - self::NUMBER_FORMAT_CARDINAL_TEXT, - self::NUMBER_FORMAT_ORDINAL_TEXT, - self::NUMBER_FORMAT_NONE, - self::NUMBER_FORMAT_BULLET - ); - - if (in_array($numFmt, $numberFormat)) { - $this->numFmt = $numFmt; - } else { - throw new \InvalidArgumentException("Invalid value, on of " . implode(', ', $numberFormat) . " possible"); - } + NumberFormat::validate($numFmt); + $this->numFmt = $numFmt; + return $this; } + /** + * Get the Footnote Numbering Format + * + * @return double + */ public function getNumStart() { return $this->numStart; } + /** + * Set the Footnote Numbering Format + * + * @param double $numStart + * @return self + */ public function setNumStart($numStart) { $this->numStart = $numStart; + return $this; } + /** + * Get the Footnote and Endnote Numbering Starting Value + * + * @return string + */ public function getNumRestart() { return $this->numRestart; } + /** + * Set the Footnote and Endnote Numbering Starting Value (continuous, eachSect, eachPage) + * + * @param string $numRestart + * @throws \InvalidArgumentException + * @return self + */ public function setNumRestart($numRestart) { $restartNumbers = array( @@ -148,5 +172,6 @@ final class FootnoteProperties } else { throw new \InvalidArgumentException("Invalid value, on of " . implode(', ', $restartNumbers) . " possible"); } + return $this; } } diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index c0bfc3cf..6e199bb9 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -17,8 +17,8 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\ComplexType\FootnoteProperties; use PhpOffice\PhpWord\Style\Section as SectionStyle; -use PhpOffice\PhpWord\SimpleType\FootnoteProperties; class Section extends AbstractContainer { diff --git a/src/PhpWord/Shared/AbstractEnum.php b/src/PhpWord/Shared/AbstractEnum.php new file mode 100644 index 00000000..4584c2f9 --- /dev/null +++ b/src/PhpWord/Shared/AbstractEnum.php @@ -0,0 +1,35 @@ +getConstants(); + } + return self::$constCacheArray[$calledClass]; + } + + public static function values() + { + return array_values(self::getConstants()); + } + + public static function validate($value) + { + $values = array_values(self::getConstants()); + if (!in_array($value, $values, true)) { + $calledClass = get_called_class(); + throw new \InvalidArgumentException("$value is not a valid value for $calledClass, possible values are " . implode(', ', $values)); + } + } +} diff --git a/src/PhpWord/SimpleType/NumberFormat.php b/src/PhpWord/SimpleType/NumberFormat.php new file mode 100644 index 00000000..4c98dbef --- /dev/null +++ b/src/PhpWord/SimpleType/NumberFormat.php @@ -0,0 +1,153 @@ +format = $this->setEnumVal($value, $enum, $this->format); + $this->format = $this->setEnumVal($value, NumberFormat::values(), $this->format); return $this; } /** - * Get start + * Get restart * * @return integer */ @@ -201,7 +203,7 @@ class NumberingLevel extends AbstractStyle } /** - * Set start + * Set restart * * @param integer $value * @return self diff --git a/src/PhpWord/Writer/Word2007/Part/Document.php b/src/PhpWord/Writer/Word2007/Part/Document.php index a2dc999b..11e3f510 100644 --- a/src/PhpWord/Writer/Word2007/Part/Document.php +++ b/src/PhpWord/Writer/Word2007/Part/Document.php @@ -21,7 +21,6 @@ use PhpOffice\Common\XMLWriter; use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Writer\Word2007\Element\Container; use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter; -use PhpOffice\PhpWord\SimpleType\FootnoteProperties; /** * Word2007 document part writer: word/document.xml diff --git a/tests/PhpWord/SimpleType/FootnotePropertiesTest.php b/tests/PhpWord/ComplexType/FootnotePropertiesTest.php similarity index 83% rename from tests/PhpWord/SimpleType/FootnotePropertiesTest.php rename to tests/PhpWord/ComplexType/FootnotePropertiesTest.php index f977b32a..025e8c91 100644 --- a/tests/PhpWord/SimpleType/FootnotePropertiesTest.php +++ b/tests/PhpWord/ComplexType/FootnotePropertiesTest.php @@ -15,9 +15,10 @@ * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ -namespace PhpOffice\PhpWord\SimpleType; +namespace PhpOffice\PhpWord\ComplexType; -use PhpOffice\PhpWord\SimpleType\FootnoteProperties; +use PhpOffice\PhpWord\ComplexType\FootnoteProperties; +use PhpOffice\PhpWord\SimpleType\NumberFormat; /** * Test class for PhpOffice\PhpWord\SimpleType\FootnoteProperties @@ -34,12 +35,12 @@ class FootnotePropertiesTest extends \PHPUnit_Framework_TestCase { $footnoteProp = new FootnoteProperties(); $footnoteProp->setPos(FootnoteProperties::POSITION_DOC_END); - $footnoteProp->setNumFmt(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN); + $footnoteProp->setNumFmt(NumberFormat::LOWER_ROMAN); $footnoteProp->setNumStart(2); $footnoteProp->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE); $this->assertEquals(FootnoteProperties::POSITION_DOC_END, $footnoteProp->getPos()); - $this->assertEquals(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN, $footnoteProp->getNumFmt()); + $this->assertEquals(NumberFormat::LOWER_ROMAN, $footnoteProp->getNumFmt()); $this->assertEquals(2, $footnoteProp->getNumStart()); $this->assertEquals(FootnoteProperties::RESTART_NUMBER_EACH_PAGE, $footnoteProp->getNumRestart()); } @@ -52,7 +53,7 @@ class FootnotePropertiesTest extends \PHPUnit_Framework_TestCase public function testWrongPos() { $footnoteProp= new FootnoteProperties(); - $footnoteProp->setPos(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN); + $footnoteProp->setPos(NumberFormat::LOWER_ROMAN); } /** @@ -74,6 +75,6 @@ class FootnotePropertiesTest extends \PHPUnit_Framework_TestCase public function testWrongNumRestart() { $footnoteProp= new FootnoteProperties(); - $footnoteProp->setNumRestart(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN); + $footnoteProp->setNumRestart(NumberFormat::LOWER_ROMAN); } } diff --git a/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php index 0f64d7a7..d45cde6b 100644 --- a/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php @@ -16,11 +16,12 @@ */ namespace PhpOffice\PhpWord\Writer\Word2007\Part; +use PhpOffice\PhpWord\ComplexType\FootnoteProperties; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\TestHelperDOCX; -use PhpOffice\PhpWord\SimpleType\FootnoteProperties; +use PhpOffice\PhpWord\SimpleType\NumberFormat; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Document @@ -65,7 +66,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase { $properties = new FootnoteProperties(); $properties->setPos(FootnoteProperties::POSITION_DOC_END); - $properties->setNumFmt(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN); + $properties->setNumFmt(NumberFormat::LOWER_ROMAN); $properties->setNumStart(1); $properties->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE); @@ -79,7 +80,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $this->assertEquals(FootnoteProperties::POSITION_DOC_END, $element->getAttribute('w:val')); $element = $doc->getElement('/w:document/w:body/w:sectPr/w:footnotePr/w:numFmt'); - $this->assertEquals(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN, $element->getAttribute('w:val')); + $this->assertEquals(NumberFormat::LOWER_ROMAN, $element->getAttribute('w:val')); $element = $doc->getElement('/w:document/w:body/w:sectPr/w:footnotePr/w:numStart'); $this->assertEquals(1, $element->getAttribute('w:val')); diff --git a/tests/PhpWord/Writer/Word2007/Part/NumberingTest.php b/tests/PhpWord/Writer/Word2007/Part/NumberingTest.php index 9d11e5cb..bca4b562 100644 --- a/tests/PhpWord/Writer/Word2007/Part/NumberingTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/NumberingTest.php @@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\TestHelperDOCX; +use PhpOffice\PhpWord\SimpleType\NumberFormat; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Numbering @@ -52,7 +53,7 @@ class NumberingTest extends \PHPUnit_Framework_TestCase 'levels' => array( array( 'start' => 1, - 'format' => 'decimal', + 'format' => NumberFormat::DECIMAL, 'restart' => 1, 'suffix' => 'space', 'text' => '%1.',