Skip to content

Commit

Permalink
checkbox now return '0' or '1'
Browse files Browse the repository at this point in the history
select tag update
  • Loading branch information
Vitexus committed Nov 27, 2024
1 parent d6f36eb commit 6d3445a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/Ease/Html/CheckboxTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
2 changes: 1 addition & 1 deletion src/Ease/Html/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public function setValue(string $value);
*
* @return string $value
*/
public function getValue();
public function getValue(): ?string;
}
4 changes: 2 additions & 2 deletions src/Ease/Html/InputTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
20 changes: 12 additions & 8 deletions src/Ease/Html/SelectTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ 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<string, string> $items items
* @param string $defaultValue default item id
* @param array<string, string> $properties select tag properties
*/
public function __construct(string $name, array $items = [], string $defaultValue = '', array $properties = [])
{
parent::__construct('select', $properties);
$this->defaultValue = $defaultValue;
$this->setTagName($name);

if (\is_array($items)) {
if ($items) {
$this->addItems($items);
}
}
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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;
}
}
7 changes: 4 additions & 3 deletions tests/src/Ease/Html/BodyTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class BodyTagTest extends PairTagTest
{
public string $rendered = '<body></body>';
public string $renderedEmpty = '<body></body>';
public string $renderedWithJS = '<body>
public string $renderedWithJS = <<<'EOD'
<body>
<script>
// <![CDATA[
Expand All @@ -36,8 +37,8 @@ class BodyTagTest extends PairTagTest
$(document).ready(function () { alert("hallo"); });
// ]]>
</script>
</body>';
</body>
EOD;
protected $object;

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/src/Ease/Html/CheckboxTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Ease/Html/SubmitButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function setUp(): void
protected function tearDown(): void
{
}

/**
* @covers \Ease\Html\SubmitButton::__construct
*/
Expand Down

0 comments on commit 6d3445a

Please sign in to comment.