diff --git a/src/PhpWord/Element/CheckBox.php b/src/PhpWord/Element/CheckBox.php index b404a6027e..029bd7d77e 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 = false; + /** * Create new instance. * @@ -71,4 +78,22 @@ public function getName() { return $this->name; } + + /** + * Set default state. + */ + public function setDefaultChecked(bool $default = true): self + { + $this->defaultChecked = $default; + + return $this; + } + + /** + * Is default state checked? + */ + public function isDefaultChecked(): bool + { + return (bool) $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()); + } } diff --git a/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php b/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php new file mode 100644 index 0000000000..0024908ee3 --- /dev/null +++ b/tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php @@ -0,0 +1,53 @@ +setDefaultChecked($checked); + + $checkBox = new CheckBox($xmlWriter, $checkBoxElement); + + // Act + $checkBox->write(); + $output = $xmlWriter->getData(); + + // Assert + self::assertStringContainsString($expectedCheckedAttribute, $output, 'Default checked should be applied.'); + } + + /** + * Data provider for testing checked state. + */ + public static function checkBoxCheckedProvider(): array + { + return [ + 'Default checked' => [ + 'checked' => true, + 'w:default w:val="1"', + ], + 'Default unchecked' => [ + 'checked' => false, + 'w:default w:val="0"', + ], + ]; + } +}