| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\FormElement\ScheduleElement; |
| 4 |
|
| 5 |
use ipl\Html\Attributes; |
| 6 |
use ipl\Html\FormElement\InputElement; |
| 7 |
use ipl\Html\FormElement\RadioElement; |
| 8 |
use ipl\Html\HtmlElement; |
| 9 |
use ipl\Web\FormElement\ScheduleElement\Common\FieldsProtector; |
| 10 |
|
| 11 |
class FieldsRadio extends RadioElement |
| 12 |
{ |
| 13 |
use FieldsProtector; |
| 14 |
|
| 15 |
protected function assemble() |
| 16 |
{ |
| 17 |
$listItems = HtmlElement::create('ul', ['class' => ['schedule-element-fields', 'single-fields']]); |
| 18 |
foreach ($this->options as $option) { |
| 19 |
$radio = (new InputElement($this->getValueOfNameAttribute())) |
| 20 |
->setValue($option->getValue()) |
| 21 |
->setType($this->type); |
| 22 |
|
| 23 |
$radio->setAttributes(clone $this->getAttributes()); |
| 24 |
|
| 25 |
$htmlId = $this->protectId($option->getValue()); |
| 26 |
$radio->getAttributes() |
| 27 |
->set('id', $htmlId) |
| 28 |
->registerAttributeCallback('checked', function () use ($option) { |
| 29 |
return (string) $this->getValue() === (string) $option->getValue(); |
| 30 |
}) |
| 31 |
->registerAttributeCallback('required', [$this, 'getRequiredAttribute']) |
| 32 |
->registerAttributeCallback('disabled', function () use ($option) { |
| 33 |
return $this->getAttributes()->get('disabled')->getValue() || $option->isDisabled(); |
| 34 |
}); |
| 35 |
|
| 36 |
$listItem = HtmlElement::create('li'); |
| 37 |
$listItem->addHtml( |
| 38 |
$radio, |
| 39 |
HtmlElement::create('label', [ |
| 40 |
'for' => $htmlId, |
| 41 |
'class' => $option->getLabelCssClass(), |
| 42 |
'tabindex' => -1 |
| 43 |
], $option->getLabel()) |
| 44 |
); |
| 45 |
$listItems->addHtml($listItem); |
| 46 |
} |
| 47 |
|
| 48 |
$this->addHtml($listItems); |
| 49 |
} |
| 50 |
|
| 51 |
protected function registerAttributeCallbacks(Attributes $attributes) |
| 52 |
{ |
| 53 |
parent::registerAttributeCallbacks($attributes); |
| 54 |
|
| 55 |
$attributes |
| 56 |
->registerAttributeCallback('protector', null, [$this, 'setIdProtector']); |
| 57 |
} |
| 58 |
} |