From 1821aa7facd0e5dc5d5fb138aba5a0c7dfd1beee Mon Sep 17 00:00:00 2001 From: Arda Beyazoglu Date: Fri, 6 Jun 2025 17:27:08 +0200 Subject: [PATCH 1/5] add the ability to set default checked --- src/PhpWord/Element/CheckBox.php | 29 +++++++++++++++++++ .../Writer/Word2007/Element/CheckBox.php | 2 +- tests/PhpWordTests/Element/CheckBoxTest.php | 12 ++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Element/CheckBox.php b/src/PhpWord/Element/CheckBox.php index b404a6027e..b406346b1a 100644 --- a/src/PhpWord/Element/CheckBox.php +++ b/src/PhpWord/Element/CheckBox.php @@ -34,6 +34,13 @@ class CheckBox extends Text */ private $name; + /** + * Default state. + * + * @var bool + */ + private $defaultChecked; + /** * Create new instance. * @@ -71,4 +78,26 @@ public function getName() { return $this->name; } + + /** + * Set default state. + * + * @return self + */ + public function setDefaultChecked(bool $default = true) + { + $this->defaultChecked = $default; + + return $this; + } + + /** + * Is default state checked? + * + * @return bool + */ + public function isDefaultChecked(): bool + { + return $this->defaultChecked; + } } diff --git a/src/PhpWord/Writer/Word2007/Element/CheckBox.php b/src/PhpWord/Writer/Word2007/Element/CheckBox.php index 1adf7d6eb1..cd1a93387a 100644 --- a/src/PhpWord/Writer/Word2007/Element/CheckBox.php +++ b/src/PhpWord/Writer/Word2007/Element/CheckBox.php @@ -52,7 +52,7 @@ public function write(): void $xmlWriter->startElement('w:checkBox'); $xmlWriter->writeAttribute('w:sizeAuto', ''); $xmlWriter->startElement('w:default'); - $xmlWriter->writeAttribute('w:val', 0); + $xmlWriter->writeAttribute('w:val', $element->isDefaultChecked() ? 1 : 0); $xmlWriter->endElement(); //w:default $xmlWriter->endElement(); //w:checkBox $xmlWriter->endElement(); // w:ffData diff --git a/tests/PhpWordTests/Element/CheckBoxTest.php b/tests/PhpWordTests/Element/CheckBoxTest.php index fbdbf36aa3..204ef99097 100644 --- a/tests/PhpWordTests/Element/CheckBoxTest.php +++ b/tests/PhpWordTests/Element/CheckBoxTest.php @@ -85,4 +85,16 @@ public function testParagraph(): void $oCheckBox->setParagraphStyle(['alignment' => Jc::CENTER, 'spaceAfter' => 100]); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oCheckBox->getParagraphStyle()); } + + /** + * Set and get default value. + */ + public function testDefault(): void + { + $oCheckBox = new CheckBox('chkBox', 'CheckBox'); + self::assertFalse($oCheckBox->isDefaultChecked()); + + $oCheckBox->setDefaultChecked(true); + self::assertTrue($oCheckBox->isDefaultChecked()); + } } From 6f40e2cfb93a47f86b287539d53cf4249f3a1518 Mon Sep 17 00:00:00 2001 From: Arda Beyazoglu Date: Fri, 6 Jun 2025 17:40:24 +0200 Subject: [PATCH 2/5] add the ability to set default checked --- src/PhpWord/Element/CheckBox.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Element/CheckBox.php b/src/PhpWord/Element/CheckBox.php index b406346b1a..e4d1e97ca1 100644 --- a/src/PhpWord/Element/CheckBox.php +++ b/src/PhpWord/Element/CheckBox.php @@ -39,7 +39,7 @@ class CheckBox extends Text * * @var bool */ - private $defaultChecked; + private $defaultChecked = false; /** * Create new instance. From 57b1ae4aee0ec32b99bdffba994cd8ea15c3e3fc Mon Sep 17 00:00:00 2001 From: Arda Beyazoglu Date: Wed, 11 Jun 2025 16:44:48 +0200 Subject: [PATCH 3/5] fixed format and added tests --- src/PhpWord/Element/CheckBox.php | 8 +--- .../Writer/Word2007/Element/CheckBoxTest.php | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php diff --git a/src/PhpWord/Element/CheckBox.php b/src/PhpWord/Element/CheckBox.php index e4d1e97ca1..029bd7d77e 100644 --- a/src/PhpWord/Element/CheckBox.php +++ b/src/PhpWord/Element/CheckBox.php @@ -81,10 +81,8 @@ public function getName() /** * Set default state. - * - * @return self */ - public function setDefaultChecked(bool $default = true) + public function setDefaultChecked(bool $default = true): self { $this->defaultChecked = $default; @@ -93,11 +91,9 @@ public function setDefaultChecked(bool $default = true) /** * Is default state checked? - * - * @return bool */ public function isDefaultChecked(): bool { - return $this->defaultChecked; + return (bool) $this->defaultChecked; } } diff --git a/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php b/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php new file mode 100644 index 0000000000..db2cf15441 --- /dev/null +++ b/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php @@ -0,0 +1,48 @@ +write(); + $output = $xmlWriter->getData(); + + // Assert + self::assertStringContainsString($expectedCheckedAttribute, $output, 'Default checked should be applied.'); + } + + /** + * Data provider for testing different combinations of background and border colors. + */ + public static function checkBoxColorProvider(): array + { + return [ + 'Default checked' => [ + 'w:val="1"', + ], + 'Default unchecked' => [ + 'w:val="0"', + ], + ]; + } +} From e7535d3da68d3683250f2522e9a9d3615cdca984 Mon Sep 17 00:00:00 2001 From: Arda Beyazoglu Date: Wed, 11 Jun 2025 16:49:03 +0200 Subject: [PATCH 4/5] fixed the test --- .../Writer/Word2007/Element/CheckBoxTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php b/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php index db2cf15441..8ecf0ca49c 100644 --- a/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php @@ -12,7 +12,7 @@ class CheckBoxTest extends TestCase { /** - * @dataProvider checkBoxColorProvider + * @dataProvider checkBoxCheckedProvider */ public function testCheckBoxGeneratesCorrectXml( string $expectedCheckedAttribute @@ -32,16 +32,16 @@ public function testCheckBoxGeneratesCorrectXml( } /** - * Data provider for testing different combinations of background and border colors. + * Data provider for testing checked state. */ - public static function checkBoxColorProvider(): array + public static function checkBoxCheckedProvider(): array { return [ 'Default checked' => [ - 'w:val="1"', + 'w:default w:val="1"', ], 'Default unchecked' => [ - 'w:val="0"', + 'w:default w:val="0"', ], ]; } From 1b459e931fa4a38ca5681847556db57ef7f5c553 Mon Sep 17 00:00:00 2001 From: Arda Beyazoglu Date: Wed, 11 Jun 2025 16:50:52 +0200 Subject: [PATCH 5/5] fixed the test --- tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php b/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php index 8ecf0ca49c..0024908ee3 100644 --- a/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php @@ -15,12 +15,15 @@ class CheckBoxTest extends TestCase * @dataProvider checkBoxCheckedProvider */ public function testCheckBoxGeneratesCorrectXml( + bool $checked, string $expectedCheckedAttribute ): void { // Arrange $xmlWriter = new XMLWriter(); $checkBoxElement = new CheckBoxElement('test', 'test'); + $checkBoxElement->setDefaultChecked($checked); + $checkBox = new CheckBox($xmlWriter, $checkBoxElement); // Act @@ -38,9 +41,11 @@ public static function checkBoxCheckedProvider(): array { return [ 'Default checked' => [ + 'checked' => true, 'w:default w:val="1"', ], 'Default unchecked' => [ + 'checked' => false, 'w:default w:val="0"', ], ];