Skip to content

add default state to checkbox inside textRun #1790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/PhpWord/Element/CheckBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class CheckBox extends Text
*/
private $name;

/**
* Default state.
*
* @var bool
*/
private $defaultChecked = false;

/**
* Create new instance.
*
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/Word2007/Element/CheckBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/PhpWordTests/Element/CheckBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
53 changes: 53 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Element/CheckBoxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpWordTests\Writer\Word2007\Element;

use PhpOffice\PhpWord\Element\CheckBox as CheckBoxElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Writer\Word2007\Element\CheckBox;
use PHPUnit\Framework\TestCase;

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
$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"',
],
];
}
}
Loading