Skip to content
Merged
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
170 changes: 170 additions & 0 deletions src/Forms/Select.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php
declare(strict_types=1);

namespace Eightfold\HTMLBuilder\Forms;

use Stringable;

use Eightfold\HTMLBuilder\Element;

class Select implements Stringable
{
/**
* @var string[]
*/
private array $wrapperProperties = [];

private bool $dropdown = true;

private bool $checkbox = false;

/**
* @param array<string, string> $options
* @param string|string[] $selected
*/
public static function create(
string|Stringable $label,
string|Stringable $name,
array $options,
string|array $selected = []
): self {
return new self($label, $name, $options, $selected);
}

/**
* @param array<string, string> $options
* @param string|string[] $selected
*/
final private function __construct(
private readonly string|Stringable $label,
private readonly string|Stringable $name,
private readonly array $options,
private string|array $selected = []
) {
}

public function wrapperProps(string ...$propperties): self
{
$this->wrapperProperties = $propperties;
return $this;
}

public function dropdown(): self
{
$this->dropdown = true;
$this->checkbox = false;
return $this;
}

public function radio(): self
{
$this->dropdown = false;
$this->checkbox = false;
return $this;
}

public function checkbox(): self
{
$this->checkbox = true;
$this->dropdown = false;
return $this;
}

private function hasSelected(): bool
{
$selected = $this->selected();
if (is_string($selected) and strlen($selected) > 0) {
return true;

} elseif (is_array($selected) and count($selected) > 0) {
return true;

}
return false;
}

/**
* @return string|string[]
*/
private function selected(): string|array
{
if ($this->checkbox) {
if (is_array($this->selected)) {
return $this->selected;
}
return [$this->selected];
}

if (is_array($this->selected) and count($this->selected) > 0) {
return $this->selected[0];
}
return $this->selected;
}

private function isSelected(string $value): bool
{
if ($this->hasSelected() === false) {
return false;
}

if (is_array($this->selected())) {
return in_array($value, $this->selected());
}
return $value === $this->selected();
}

public function __toString(): string
{
if ($this->dropdown) {
return (string) $this->selectDropdown();
}
return (string) $this->selectOther();
}

private function selectDropdown(): Element
{
$elements = [];
foreach ($this->options as $value => $content) {
$option = Element::option($content)->props('value ' . $value);
if ($this->isSelected($value)) {
$option = $option->prop('selected selected');
}
$elements[] = $option;
}

return Element::div(
Element::label(
$this->label
)->props('for ' . $this->name),
Element::select(
...$elements
)->props('id ' . $this->name, 'name ' . $this->name)
)->props(...$this->wrapperProperties);
}

private function selectOther(): Element
{
$elements = [];
$type = 'radio';
if ($this->checkbox) {
$type = 'checkbox';
}
foreach ($this->options as $value => $content) {
$id = $this->name . '-' . $value;
$label = Element::label($content)->props('for ' . $id);
$input = Element::input()->omitEndTag()->props(
'id ' . $id,
'type ' . $type,
'value ' . $value
);
if ($this->isSelected($value)) {
$input = $input->prop('checked checked');
}
$elements[] = Element::div($input, $label);
}
return Element::fieldset(
Element::legend($this->label),
...$elements
);
}
}
121 changes: 121 additions & 0 deletions tests/Forms/SelectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);

namespace Eightfold\HTMLBuilder\Tests;

use PHPUnit\Framework\TestCase;

// use Eightfold\HTMLBuilder\Tests\Extensions\ElementExtension;

// use Eightfold\XMLBuilder\Comment;

// use Eightfold\HTMLBuilder\Document;
use Eightfold\HTMLBuilder\Forms\Select;

class SelectTest extends TestCase
{
/**
* @test
*/
public function can_be_checkboxes(): void // phpcs: ignore
{
$expected = <<<html
<fieldset><legend>Select your option</legend><div><input id="select-value" type="checkbox" value="value" checked><label for="select-value">display</label></div><div><input id="select-value2" type="checkbox" value="value2" checked><label for="select-value2">display2</label></div></fieldset>
html;

$result = (string) Select::create(
'Select your option',
'select',
[
'value' => 'display',
'value2' => 'display2'
],
['value', 'value2']
)->checkbox();

$this->assertSame($expected, $result);
}

/**
* @test
*/
public function can_be_radio_buttons(): void // phpcs: ignore
{
$expected = <<<html
<fieldset><legend>Select your option</legend><div><input id="select-value" type="radio" value="value" checked><label for="select-value">display</label></div></fieldset>
html;

$result = (string) Select::create(
'Select your option',
'select',
[
'value' => 'display'
],
'value'
)->radio();

$this->assertSame($expected, $result);
}

/**
* @test
*/
public function can_add_properties_to_wrapper(): void // phpcs: ignore
{
$expected = <<<html
<div id="some-id" class="some-token"><label for="select">Select your option</label><select id="select" name="select"><option value="value">display</option></select></div>
html;

$result = (string) Select::create(
'Select your option',
'select',
[
'value' => 'display'
]
)->wrapperProps('id some-id', 'class some-token');

$this->assertSame($expected, $result);
}

/**
* @test
*/
public function can_preselect_option(): void // phpcs:ignore
{
$expected = <<<html
<div><label for="select">Select your option</label><select id="select" name="select"><option value="value">display</option><option value="value2" selected>display2</option></select></div>
html;

$result = (string) Select::create(
'Select your option',
'select',
[
'value' => 'display',
'value2' => 'display2'
],
'value2'
);

$this->assertSame($expected, $result);
}

/**
* @test
*/
public function is_expected_base(): void // phpcs:ignore
{
$expected = <<<html
<div><label for="select">Select your option</label><select id="select" name="select"><option value="value">display</option></select></div>
html;

$result = (string) Select::create(
'Select your option',
'select',
[
'value' => 'display'
]
);

$this->assertSame($expected, $result);
}
}