From 6d3445a0e4359fb22ec6d9e97393f3383f67e1fe Mon Sep 17 00:00:00 2001 From: CyberVitexus Date: Wed, 27 Nov 2024 12:04:45 +0100 Subject: [PATCH] checkbox now return '0' or '1' select tag update --- src/Ease/Html/CheckboxTag.php | 8 ++++---- src/Ease/Html/Input.php | 2 +- src/Ease/Html/InputTag.php | 4 ++-- src/Ease/Html/SelectTag.php | 20 ++++++++++++-------- tests/src/Ease/Html/BodyTagTest.php | 7 ++++--- tests/src/Ease/Html/CheckboxTagTest.php | 8 ++++---- tests/src/Ease/Html/SubmitButtonTest.php | 2 +- 7 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/Ease/Html/CheckboxTag.php b/src/Ease/Html/CheckboxTag.php index ced6045..70111c2 100644 --- a/src/Ease/Html/CheckboxTag.php +++ b/src/Ease/Html/CheckboxTag.php @@ -70,12 +70,12 @@ public function setValue($value = true): void } /** - * Obtains curent checkbox state. + * Obtains current checkbox state. * - * @return bool $value + * @return string $value '0' or '1' */ - public function getValue() + public function getValue(): ?string { - return $this->getTagProperty('checked') === 'true'; + return $this->getTagProperty('checked') ? '1' : '0'; } } diff --git a/src/Ease/Html/Input.php b/src/Ease/Html/Input.php index eea9c99..6182b78 100644 --- a/src/Ease/Html/Input.php +++ b/src/Ease/Html/Input.php @@ -32,5 +32,5 @@ public function setValue(string $value); * * @return string $value */ - public function getValue(); + public function getValue(): ?string; } diff --git a/src/Ease/Html/InputTag.php b/src/Ease/Html/InputTag.php index 110fd9d..7d5e0d7 100644 --- a/src/Ease/Html/InputTag.php +++ b/src/Ease/Html/InputTag.php @@ -60,9 +60,9 @@ public function setValue($value): void /** * Returns the value of an input field. * - * @return string $value + * @return null|string $value */ - public function getValue() + public function getValue(): ?string { return $this->getTagProperty('value'); } diff --git a/src/Ease/Html/SelectTag.php b/src/Ease/Html/SelectTag.php index 0d37ec6..bd0de4e 100644 --- a/src/Ease/Html/SelectTag.php +++ b/src/Ease/Html/SelectTag.php @@ -40,10 +40,10 @@ class SelectTag extends PairTag implements Input /** * Html select box. * - * @param string $name name - * @param array $items items - * @param string $defaultValue default item id - * @param array $properties select tag properties + * @param string $name name + * @param array $items items + * @param string $defaultValue default item id + * @param array $properties select tag properties */ public function __construct(string $name, array $items = [], string $defaultValue = '', array $properties = []) { @@ -51,7 +51,7 @@ public function __construct(string $name, array $items = [], string $defaultValu $this->defaultValue = $defaultValue; $this->setTagName($name); - if (\is_array($items)) { + if ($items) { $this->addItems($items); } } @@ -68,7 +68,7 @@ public function addItems(array $items): array foreach ($items as $itemName => $itemValue) { $added[$itemName] = $this->addItem(new OptionTag((string) $itemValue, (string) $itemName)); - if ($this->defaultValue === $itemName) { + if ($this->defaultValue === (string) $itemName) { $this->lastItem()->setDefault(); } } @@ -160,14 +160,18 @@ public function disableItem($itemID): void * Get value of selected item. */ #[\Override] - public function getValue(): string + public function getValue(): ?string { + $value = null; + foreach ($this->pageParts as $option) { $pos = array_search('selected', $option->tagProperties, true); if (($pos !== false) && is_numeric($pos)) { - return $option->getValue(); + $value = $option->getValue(); } } + + return $value; } } diff --git a/tests/src/Ease/Html/BodyTagTest.php b/tests/src/Ease/Html/BodyTagTest.php index cafe7be..169504e 100644 --- a/tests/src/Ease/Html/BodyTagTest.php +++ b/tests/src/Ease/Html/BodyTagTest.php @@ -22,7 +22,8 @@ class BodyTagTest extends PairTagTest { public string $rendered = ''; public string $renderedEmpty = ''; - public string $renderedWithJS = ' + public string $renderedWithJS = <<<'EOD' + -'; - + +EOD; protected $object; /** diff --git a/tests/src/Ease/Html/CheckboxTagTest.php b/tests/src/Ease/Html/CheckboxTagTest.php index b2a7dfb..017b385 100644 --- a/tests/src/Ease/Html/CheckboxTagTest.php +++ b/tests/src/Ease/Html/CheckboxTagTest.php @@ -46,9 +46,9 @@ protected function tearDown(): void public function testSetValue(): void { $this->object->setValue(true); - $this->assertTrue($this->object->getValue()); + $this->assertEquals('1', $this->object->getValue()); $this->object->setValue(false); - $this->assertFalse($this->object->getValue()); + $this->assertEquals('0', $this->object->getValue()); } /** @@ -57,9 +57,9 @@ public function testSetValue(): void public function testGetValue(): void { $this->object->setValue(true); - $this->assertTrue($this->object->getValue()); + $this->assertEquals('1', $this->object->getValue()); $this->object->setValue(false); - $this->assertFalse($this->object->getValue()); + $this->assertEquals('0', $this->object->getValue()); } /** diff --git a/tests/src/Ease/Html/SubmitButtonTest.php b/tests/src/Ease/Html/SubmitButtonTest.php index dca550a..88b0d1d 100644 --- a/tests/src/Ease/Html/SubmitButtonTest.php +++ b/tests/src/Ease/Html/SubmitButtonTest.php @@ -39,7 +39,7 @@ protected function setUp(): void protected function tearDown(): void { } - + /** * @covers \Ease\Html\SubmitButton::__construct */