From 41a5b74f93ec919fcdefc59ffd6defcc84ab3ff2 Mon Sep 17 00:00:00 2001 From: Owen Leibman Date: Thu, 16 Apr 2020 23:30:27 -0700 Subject: [PATCH 1/2] Make Default Paper Configurable Each section currently has a hard-coded default paper of A4. It would make sense to allow the user to set this default, and specify it in a configuration file, just as is done with default font name and size. --- docs/general.rst | 10 ++ phpword.ini.dist | 4 + src/PhpWord/Settings.php | 34 ++++++ src/PhpWord/Style/Section.php | 6 +- tests/PhpWord/SettingsTest.php | 100 ++++++++++++++++++ .../AbstractWebServerEmbeddedTest.php | 21 +++- 6 files changed, 173 insertions(+), 2 deletions(-) diff --git a/docs/general.rst b/docs/general.rst index f40a08c367..d4b9b99c45 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -130,6 +130,16 @@ To turn it on set ``outputEscapingEnabled`` option to ``true`` in your PHPWord c \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true); +Default Paper +~~~~~~~~~~~~~ + +By default, all sections of the document will print on A4 paper. +You can alter the default paper by using the following function: + +.. code-block:: php + + \PhpOffice\PhpWord\Settings::setDefaultPaper('Letter'); + Default font ~~~~~~~~~~~~ diff --git a/phpword.ini.dist b/phpword.ini.dist index 0f4cc358df..f3f66dbe2e 100644 --- a/phpword.ini.dist +++ b/phpword.ini.dist @@ -14,3 +14,7 @@ outputEscapingEnabled = false defaultFontName = Arial defaultFontSize = 10 + +[Paper] + +defaultPaper = "A4" diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 8de1a8df80..c493132f94 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -70,6 +70,7 @@ class Settings const DEFAULT_FONT_SIZE = 10; const DEFAULT_FONT_COLOR = '000000'; const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs + const DEFAULT_PAPER = 'A4'; /** * Compatibility option for XMLWriter @@ -119,6 +120,12 @@ class Settings */ private static $defaultFontSize = self::DEFAULT_FONT_SIZE; + /** + * Default paper + * @var int + */ + private static $defaultPaper = self::DEFAULT_PAPER; + /** * The user defined temporary directory. * @@ -432,6 +439,33 @@ public static function loadConfig($filename = null) return $config; } + /** + * Get default paper + * + * @return string + */ + public static function getDefaultPaper() + { + return self::$defaultPaper; + } + + /** + * Set default paper + * + * @param string $value + * @return bool + */ + public static function setDefaultPaper($value) + { + if (is_string($value) && trim($value) !== '') { + self::$defaultPaper = $value; + + return true; + } + + return false; + } + /** * Return the compatibility option used by the XMLWriter * diff --git a/src/PhpWord/Style/Section.php b/src/PhpWord/Style/Section.php index ff9b0be09a..697add7456 100644 --- a/src/PhpWord/Style/Section.php +++ b/src/PhpWord/Style/Section.php @@ -17,6 +17,7 @@ namespace PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\SimpleType\VerticalJc; /** @@ -200,8 +201,11 @@ public function getPaperSize() * @param string $value * @return self */ - public function setPaperSize($value = 'A4') + public function setPaperSize($value = '') { + if (!$value) { + $value = Settings::getDefaultPaper(); + } if ($this->paper === null) { $this->paper = new Paper(); } diff --git a/tests/PhpWord/SettingsTest.php b/tests/PhpWord/SettingsTest.php index afe5954969..748ec71b10 100644 --- a/tests/PhpWord/SettingsTest.php +++ b/tests/PhpWord/SettingsTest.php @@ -25,6 +25,45 @@ */ class SettingsTest extends \PHPUnit\Framework\TestCase { + private $compatibility; + private $defaultFontSize; + private $defaultFontName; + private $defaultPaper; + private $measurementUnit; + private $outputEscapingEnabled; + private $pdfRendererName; + private $pdfRendererPath; + private $tempDir; + private $zipClass; + + public function setUp() + { + $this->compatibility = Settings::hasCompatibility(); + $this->defaultFontSize = Settings::getDefaultFontSize(); + $this->defaultFontName = Settings::getDefaultFontName(); + $this->defaultPaper = Settings::getDefaultPaper(); + $this->measurementUnit = Settings::getMeasurementUnit(); + $this->outputEscapingEnabled = Settings::isOutputEscapingEnabled(); + $this->pdfRendererName = Settings::getPdfRendererName(); + $this->pdfRendererPath = Settings::getPdfRendererPath(); + $this->tempDir = Settings::getTempDir(); + $this->zipClass = Settings::getZipClass(); + } + + public function tearDown() + { + Settings::setCompatibility($this->compatibility); + Settings::setDefaultFontSize($this->defaultFontSize); + Settings::setDefaultFontName($this->defaultFontName); + Settings::setDefaultPaper($this->defaultPaper); + Settings::setMeasurementUnit($this->measurementUnit); + Settings::setOutputEscapingEnabled($this->outputEscapingEnabled); + Settings::setPdfRendererName($this->pdfRendererName); + Settings::setPdfRendererPath($this->pdfRendererPath); + Settings::setTempDir($this->tempDir); + Settings::setZipClass($this->zipClass); + } + /** * Test set/get compatibity option */ @@ -35,14 +74,28 @@ public function testSetGetCompatibility() $this->assertFalse(Settings::hasCompatibility()); } + /** + * Test set/get outputEscapingEnabled option + */ + public function testSetGetOutputEscapingEnabled() + { + $this->assertFalse(Settings::isOutputEscapingEnabled()); + Settings::setOutputEscapingEnabled(true); + $this->assertTrue(Settings::isOutputEscapingEnabled()); + } + /** * Test set/get zip class */ public function testSetGetZipClass() { + $this->assertEquals(Settings::ZIPARCHIVE, Settings::getZipClass()); + $this->assertFalse(Settings::setZipClass('foo')); $this->assertEquals(Settings::ZIPARCHIVE, Settings::getZipClass()); $this->assertTrue(Settings::setZipClass(Settings::PCLZIP)); + $this->assertEquals(Settings::getZipClass(), Settings::PCLZIP); $this->assertFalse(Settings::setZipClass('foo')); + $this->assertEquals(Settings::getZipClass(), Settings::PCLZIP); } /** @@ -57,6 +110,7 @@ public function testSetGetPdfRenderer() $this->assertEquals(Settings::PDF_RENDERER_DOMPDF, Settings::getPdfRendererName()); $this->assertEquals($domPdfPath, Settings::getPdfRendererPath()); $this->assertFalse(Settings::setPdfRendererPath('dummy/path')); + $this->assertEquals($domPdfPath, Settings::getPdfRendererPath()); } /** @@ -64,9 +118,13 @@ public function testSetGetPdfRenderer() */ public function testSetGetMeasurementUnit() { + $this->assertEquals(Settings::UNIT_TWIP, Settings::getMeasurementUnit()); + $this->assertFalse(Settings::setMeasurementUnit('foo')); $this->assertEquals(Settings::UNIT_TWIP, Settings::getMeasurementUnit()); $this->assertTrue(Settings::setMeasurementUnit(Settings::UNIT_INCH)); + $this->assertEquals(Settings::UNIT_INCH, Settings::getMeasurementUnit()); $this->assertFalse(Settings::setMeasurementUnit('foo')); + $this->assertEquals(Settings::UNIT_INCH, Settings::getMeasurementUnit()); } /** @@ -98,9 +156,13 @@ public function testTempDirCanBeSet() */ public function testSetGetDefaultFontName() { + $this->assertEquals(Settings::DEFAULT_FONT_NAME, Settings::getDefaultFontName()); + $this->assertFalse(Settings::setDefaultFontName(' ')); $this->assertEquals(Settings::DEFAULT_FONT_NAME, Settings::getDefaultFontName()); $this->assertTrue(Settings::setDefaultFontName('Times New Roman')); + $this->assertEquals('Times New Roman', Settings::getDefaultFontName()); $this->assertFalse(Settings::setDefaultFontName(' ')); + $this->assertEquals('Times New Roman', Settings::getDefaultFontName()); } /** @@ -108,9 +170,36 @@ public function testSetGetDefaultFontName() */ public function testSetGetDefaultFontSize() { + $this->assertEquals(Settings::DEFAULT_FONT_SIZE, Settings::getDefaultFontSize()); + $this->assertFalse(Settings::setDefaultFontSize(null)); $this->assertEquals(Settings::DEFAULT_FONT_SIZE, Settings::getDefaultFontSize()); $this->assertTrue(Settings::setDefaultFontSize(12)); + $this->assertEquals(12, Settings::getDefaultFontSize()); $this->assertFalse(Settings::setDefaultFontSize(null)); + $this->assertEquals(12, Settings::getDefaultFontSize()); + } + + /** + * Test set/get default paper + */ + public function testSetGetDefaultPaper() + { + $dflt = Settings::DEFAULT_PAPER; + $chng = ($dflt === 'A4') ? 'Letter' : 'A4'; + $doc = new PhpWord(); + $this->assertEquals($dflt, Settings::getDefaultPaper()); + $sec1 = $doc->addSection(); + $this->assertEquals($dflt, $sec1->getStyle()->getPaperSize()); + $this->assertFalse(Settings::setDefaultPaper('')); + $this->assertEquals($dflt, Settings::getDefaultPaper()); + $this->assertTrue(Settings::setDefaultPaper($chng)); + $this->assertEquals($chng, Settings::getDefaultPaper()); + $sec2 = $doc->addSection(); + $this->assertEquals($chng, $sec2->getStyle()->getPaperSize()); + $sec3 = $doc->addSection(array('paperSize' => 'Legal')); + $this->assertEquals('Legal', $sec3->getStyle()->getPaperSize()); + $this->assertFalse(Settings::setDefaultPaper('')); + $this->assertEquals($chng, Settings::getDefaultPaper()); } /** @@ -126,6 +215,7 @@ public function testLoadConfig() 'defaultFontName' => 'Arial', 'defaultFontSize' => 10, 'outputEscapingEnabled' => false, + 'defaultPaper' => 'A4', ); // Test default value @@ -133,6 +223,16 @@ public function testLoadConfig() // Test with valid file $this->assertEquals($expected, Settings::loadConfig(__DIR__ . '/../../phpword.ini.dist')); + foreach ($expected as $key => $value) { + if ($key === 'compatibility') { + $meth = 'hasCompatibility'; + } elseif ($key === 'outputEscapingEnabled') { + $meth = 'isOutputEscapingEnabled'; + } else { + $meth = 'get' . ucfirst($key); + } + $this->assertEquals(Settings::$meth(), $value); + } // Test with invalid file $this->assertEmpty(Settings::loadConfig(__DIR__ . '/../../phpunit.xml.dist')); diff --git a/tests/PhpWord/_includes/AbstractWebServerEmbeddedTest.php b/tests/PhpWord/_includes/AbstractWebServerEmbeddedTest.php index 9316a9fe61..25fe836ac1 100644 --- a/tests/PhpWord/_includes/AbstractWebServerEmbeddedTest.php +++ b/tests/PhpWord/_includes/AbstractWebServerEmbeddedTest.php @@ -26,7 +26,26 @@ abstract class AbstractWebServerEmbeddedTest extends \PHPUnit\Framework\TestCase public static function setUpBeforeClass() { if (self::isBuiltinServerSupported()) { - self::$httpServer = new Process('php -S localhost:8080 -t tests/PhpWord/_files'); + $commandLine = 'php -S localhost:8080 -t tests/PhpWord/_files'; + + /* + * Make sure to invoke \Symfony\Component\Process\Process correctly + * regardless of PHP version used. + * + * In Process version >= 5 / PHP >= 7.2.5, the constructor requires + * an array, while in version < 3.3 / PHP < 5.5.9 it requires a string. + * In between, it can accept both. + * + * Process::fromShellCommandLine() was introduced in version 4.2.0, + * to enable recent versions of Process to parse a command string, + * so if it is not available it means it is still possible to pass + * a string to the constructor. + */ + if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandLine')) { + self::$httpServer = Process::fromShellCommandline($commandLine); + } else { + self::$httpServer = new Process($commandLine); + } self::$httpServer->start(); while (!self::$httpServer->isRunning()) { usleep(1000); From 6da3fd3c066bd004e314ea3a138b304b983b2646 Mon Sep 17 00:00:00 2001 From: oleibman Date: Thu, 16 Apr 2020 23:55:24 -0700 Subject: [PATCH 2/2] Correct Scrutinizer Warning Type was declared incorrectly in comment. --- src/PhpWord/Settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index c493132f94..80e512d2a7 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -122,7 +122,7 @@ class Settings /** * Default paper - * @var int + * @var string */ private static $defaultPaper = self::DEFAULT_PAPER;