Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Nov 29, 2022
1 parent f307b32 commit bc71c72
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 12 deletions.
19 changes: 9 additions & 10 deletions src/FormElement/RadioElement.php
Expand Up @@ -36,7 +36,10 @@ public function setOptions(array $options): self
$this->options = [];
foreach ($options as $value => $label) {
$option = (new RadioOption($value, $label))
->setDisabled(in_array($value, $this->disabledOptions, true));
->setDisabled(
in_array($value, $this->disabledOptions, ! is_int($value))
|| ($value === '' && in_array(null, $this->disabledOptions))
);

$this->options[$value] = $option;
}
Expand Down Expand Up @@ -74,9 +77,10 @@ public function getOption($value): RadioOption
public function setDisabledOptions(array $disabledOptions): self
{
if (! empty($this->options)) {
foreach ($this->options as $option) {
foreach ($this->options as $value => $option) {
$option->setDisabled(
in_array($option->getValue(), $disabledOptions, true)
in_array($value, $disabledOptions, ! is_int($value))
|| ($value === '' && in_array(null, $disabledOptions))
);
}

Expand Down Expand Up @@ -111,12 +115,7 @@ protected function assemble()
->merge($option->getAttributes())
->registerAttributeCallback('checked',
function () use ($option) {
$optionValue = $option->getValue();

if ($optionValue === '') {
$optionValue = null;
}
return $this->getValue() === $optionValue;
return $this->getValue() === $option->getValue();
}
)
->registerAttributeCallback('disabled',
Expand All @@ -126,7 +125,7 @@ function () use ($option) {
);

$labelAttributes = (new Attributes(['class' => RadioOption::LABEL_CLASS]))
->add('class', $option->getlabelCssClass());
->add('class', $option->getLabelCssClass());

$label = new HtmlElement(
'label',
Expand Down
6 changes: 4 additions & 2 deletions src/FormElement/RadioOption.php
Expand Up @@ -27,11 +27,13 @@ class RadioOption
/**
* RadioOption constructor.
*
* @param string|int $value
* @param string|int|null $value
* @param string $label
*/
public function __construct($value, string $label)
{
$value = $value === '' ? null : $value;

$this->value = $value;
$this->label = $label;
}
Expand Down Expand Up @@ -73,7 +75,7 @@ public function getValue()
/**
* Add css class to the option label
*
* @param string|array|Attribute $labelCssClass
* @param string|string[] $labelCssClass
*
* @return $this
*/
Expand Down
126 changes: 126 additions & 0 deletions tests/FormElement/RadioElementTest.php
Expand Up @@ -280,5 +280,131 @@ public function testNullAndTheEmptyStringAreEquallyHandled()
HTML;

$this->assertHtml($html, $radio2);

$radio->setDisabledOptions([null, 'foo']);
$radio2->setDisabledOptions(['', 'foo']);

$html = <<<'HTML'
<label class="radio-label"><input name="radio" type="radio" disabled checked>Please choose</label>
HTML;

$this->assertHtml($html, $radio);

$html = <<<'HTML'
<label class="radio-label"><input name="radio2" type="radio" disabled checked>Please choose</label>
HTML;

$this->assertHtml($html, $radio2);
}

public function testSetOptionsResetsOptions()
{
$radio = new RadioElement('radio');
$radio->setOptions(['foo' => 'Foo', 'bar' => 'Bar']);

$this->assertInstanceOf(RadioOption::class, $radio->getOption('foo'));
$this->assertInstanceOf(RadioOption::class, $radio->getOption('bar'));

$radio->setOptions(['car' => 'Car', 'train' => 'Train']);

$this->expectExceptionMessage('There is no such option "foo"');
$radio->getOption('foo');

$this->expectExceptionMessage('There is no such option "bar"');
$radio->getOption('bar');

$this->assertInstanceOf(RadioOption::class, $radio->getOption('car'));
$this->assertInstanceOf(RadioOption::class, $radio->getOption('train'));
}

public function testOrderOfOptionsAndDisabledOptionsDoesNotMatter()
{
$radio = new RadioElement('test', [
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar'
],
'disabledOptions' => ['foo', 'bar']
]);

$html = <<<'HTML'
<label class="radio-label"><input value="foo" name="test" type="radio" disabled>Foo</label>
<label class="radio-label"><input value="bar" name="test" type="radio" disabled>Bar</label>
HTML;
$this->assertHtml($html, $radio);

$radio = new RadioElement('test', [
'disabledOptions' => ['foo', 'bar'],
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar'
]
]);

$this->assertHtml($html, $radio);
}

public function testGetOptionReturnsPreviouslySetOption()
{
$radio = new RadioElement('radio');
$radio->setOptions(['' => 'Empty String', 'foo' => 'Foo', 'bar' => 'Bar']);

$this->assertNull($radio->getOption('')->getValue());
$this->assertSame('Empty String', $radio->getOption('')->getLabel());

$this->assertSame('foo', $radio->getOption('foo')->getValue());
$this->assertSame('Foo', $radio->getOption('foo')->getLabel());

$radio->setOptions(['' => 'Please Choose', 'car' => 'Car', 'train' => 'Train']);

$this->assertNull($radio->getOption('')->getValue());
$this->assertSame('Please Choose', $radio->getOption('')->getLabel());

$this->assertSame('car', $radio->getOption('car')->getValue());
$this->assertSame('Car', $radio->getOption('car')->getLabel());
}

public function testNullAndTheEmptyStringAreAlsoEquallyHandledWhileDisablingOptions()
{
$radio = new RadioElement('radio');
$radio->setOptions([null => 'Foo', 'bar' => 'Bar']);
$radio->setDisabledOptions([null]);

$this->assertTrue($radio->getOption(null)->isDisabled());

$radio = new RadioElement('radio');
$radio->setOptions(['' => 'Foo', 'bar' => 'Bar']);
$radio->setDisabledOptions(['']);

$this->assertTrue($radio->getOption('')->isDisabled());

$radio = new RadioElement('radio');
$radio->setOptions([null => 'Foo', 'bar' => 'Bar']);
$radio->setDisabledOptions(['']);

$this->assertTrue($radio->getOption(null)->isDisabled());
$radio = new RadioElement('radio');
$radio->setOptions(['' => 'Foo', 'bar' => 'Bar']);
$radio->setDisabledOptions([null]);

$this->assertTrue($radio->getOption('')->isDisabled());
}

public function testGetOptionGetValueAndElementGetValueHandleNullAndTheEmptyStringEqually()
{
$radio = new RadioElement('radio');
$radio->setOptions(['' => 'Foo']);
$radio->setValue('');

$this->assertNull($radio->getValue());
$this->assertNull($radio->getOption('')->getValue());

$radio = new RadioElement('radio');
$radio->setOptions([null => 'Foo']);

$this->assertNull($radio->getValue());
$this->assertNull($radio->getOption(null)->getValue());
}
}

0 comments on commit bc71c72

Please sign in to comment.