| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Html\FormElement; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use ipl\Html\Attributes; |
| 7 |
use ipl\Html\HtmlDocument; |
| 8 |
use ipl\Html\HtmlElement; |
| 9 |
use ipl\Html\Text; |
| 10 |
use ipl\I18n\Translation; |
| 11 |
use ipl\Validator\DeferredInArrayValidator; |
| 12 |
use ipl\Validator\ValidatorChain; |
| 13 |
|
| 14 |
class RadioElement extends BaseFormElement |
| 15 |
{ |
| 16 |
use Translation; |
| 17 |
|
| 18 |
/** @var string The element type */ |
| 19 |
protected $type = 'radio'; |
| 20 |
|
| 21 |
/** @var RadioOption[] Radio options */ |
| 22 |
protected $options = []; |
| 23 |
|
| 24 |
/** @var array Disabled radio options */ |
| 25 |
protected $disabledOptions = []; |
| 26 |
|
| 27 |
protected function tag(): string |
| 28 |
{ |
| 29 |
// In order to be able to decorate this element, we need to return a tag. |
| 30 |
// If we'd have a distinct form element base implementation, that doesn't |
| 31 |
// extend BaseHtmlElement, this wouldn't be necessary. |
| 32 |
return 'bogus'; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Set the options |
| 37 |
* |
| 38 |
* @param array $options |
| 39 |
* |
| 40 |
* @return $this |
| 41 |
*/ |
| 42 |
public function setOptions(array $options): self |
| 43 |
{ |
| 44 |
$this->options = []; |
| 45 |
foreach ($options as $value => $label) { |
| 46 |
$option = (new RadioOption($value, $label)) |
| 47 |
->setDisabled( |
| 48 |
in_array($value, $this->disabledOptions, ! is_int($value)) |
| 49 |
|| ($value === '' && in_array(null, $this->disabledOptions, true)) |
| 50 |
); |
| 51 |
|
| 52 |
$this->options[$value] = $option; |
| 53 |
} |
| 54 |
|
| 55 |
$this->disabledOptions = []; |
| 56 |
|
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the option with specified value |
| 62 |
* |
| 63 |
* @param string|int $value |
| 64 |
* |
| 65 |
* @return RadioOption |
| 66 |
* |
| 67 |
* @throws InvalidArgumentException If no option with the specified value exists |
| 68 |
*/ |
| 69 |
public function getOption(string|int $value): RadioOption |
| 70 |
{ |
| 71 |
if (! isset($this->options[$value])) { |
| 72 |
throw new InvalidArgumentException(sprintf('There is no such option "%s"', $value)); |
| 73 |
} |
| 74 |
|
| 75 |
return $this->options[$value]; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Set the specified options as disable |
| 80 |
* |
| 81 |
* @param array $disabledOptions |
| 82 |
* |
| 83 |
* @return $this |
| 84 |
*/ |
| 85 |
public function setDisabledOptions(array $disabledOptions): self |
| 86 |
{ |
| 87 |
if (! empty($this->options)) { |
| 88 |
foreach ($this->options as $value => $option) { |
| 89 |
$option->setDisabled( |
| 90 |
in_array($value, $disabledOptions, ! is_int($value)) |
| 91 |
|| ($value === '' && in_array(null, $disabledOptions, true)) |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
$this->disabledOptions = []; |
| 96 |
} else { |
| 97 |
$this->disabledOptions = $disabledOptions; |
| 98 |
} |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
public function renderUnwrapped() |
| 104 |
{ |
| 105 |
// Parent::renderUnwrapped() requires $tag and the content should be empty. However, since we are wrapping |
| 106 |
// each button in a label, the call to parent cannot work here and must be overridden. |
| 107 |
return HtmlDocument::renderUnwrapped(); |
| 108 |
} |
| 109 |
|
| 110 |
protected function assemble() |
| 111 |
{ |
| 112 |
// To avoid duplicate ids for options. Required for tests or if someone sets explicitly. |
| 113 |
$this->getAttributes()->remove('id'); |
| 114 |
foreach ($this->options as $option) { |
| 115 |
$radio = (new InputElement($this->getValueOfNameAttribute())) |
| 116 |
->setType($this->type) |
| 117 |
->setValue($option->getValue()); |
| 118 |
|
| 119 |
// Only add the non-callback attributes to all options |
| 120 |
foreach ($this->getAttributes() as $attribute) { |
| 121 |
$radio->getAttributes()->addAttribute(clone $attribute); |
| 122 |
} |
| 123 |
|
| 124 |
$radio->getAttributes() |
| 125 |
->merge($option->getAttributes()) |
| 126 |
->registerAttributeCallback( |
| 127 |
'checked', |
| 128 |
function () use ($option) { |
| 129 |
$optionValue = $option->getValue(); |
| 130 |
$value = $this->getValue(); |
| 131 |
|
| 132 |
if ($optionValue === '' && $value === null) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
return ! is_int($optionValue) |
| 137 |
? $value === $optionValue |
| 138 |
: $value == $optionValue; |
| 139 |
} |
| 140 |
) |
| 141 |
->registerAttributeCallback( |
| 142 |
'disabled', |
| 143 |
function () use ($option) { |
| 144 |
return $this->getAttributes()->get('disabled')->getValue() || $option->isDisabled(); |
| 145 |
} |
| 146 |
); |
| 147 |
|
| 148 |
$label = new HtmlElement( |
| 149 |
'label', |
| 150 |
new Attributes(['class' => $option->getLabelCssClass()]), |
| 151 |
$radio, |
| 152 |
Text::create($option->getLabel()) |
| 153 |
); |
| 154 |
|
| 155 |
$this->addHtml($label); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
protected function addDefaultValidators(ValidatorChain $chain): void |
| 160 |
{ |
| 161 |
$chain->add(new DeferredInArrayValidator(function (): array { |
| 162 |
$possibleValues = []; |
| 163 |
|
| 164 |
foreach ($this->options as $option) { |
| 165 |
if ($option->isDisabled()) { |
| 166 |
continue; |
| 167 |
} |
| 168 |
|
| 169 |
$possibleValues[] = $option->getValue(); |
| 170 |
} |
| 171 |
|
| 172 |
return $possibleValues; |
| 173 |
})); |
| 174 |
} |
| 175 |
|
| 176 |
protected function registerAttributeCallbacks(Attributes $attributes) |
| 177 |
{ |
| 178 |
parent::registerAttributeCallbacks($attributes); |
| 179 |
|
| 180 |
$this->getAttributes()->registerAttributeCallback( |
| 181 |
'options', |
| 182 |
null, |
| 183 |
[$this, 'setOptions'] |
| 184 |
); |
| 185 |
|
| 186 |
$this->getAttributes()->registerAttributeCallback( |
| 187 |
'disabledOptions', |
| 188 |
null, |
| 189 |
[$this, 'setDisabledOptions'] |
| 190 |
); |
| 191 |
} |
| 192 |
} |