/** * @return string */ public function getInput() { $res = $this->renderTemplate(); if ($res !== null) { return $res; } $list = $this->getList(); $value = $this->getValue(); $options = $this->getAttributes(); $options['name'] = $this->getNameChainString(); $isMultiple = $this->multiple; $content = ''; $listItemsOptions = $this->getListItemsOptions(); foreach ($list as $optionValue => $optionContent) { $optionOptions = isset($listItemsOptions[$optionValue]) && is_array($listItemsOptions[$optionValue]) ? array_merge($options, $listItemsOptions[$optionValue]) : $options; $optionOptions['value'] = $optionValue; $optionOptions['type'] = $isMultiple ? 'checkbox' : 'radio'; $optionOptions['id'] = Html::toId($options['name'] . '_' . $optionValue); if ($value !== null && (!$isMultiple && $optionValue == $value || $isMultiple && is_array($value) && in_array($optionValue, $value))) { $optionOptions['checked'] = 'checked'; } $option = Html::tag('input', $optionOptions, false); $labelOptions = $this->getLabelOptions(); $labelOptions['for'] = $optionOptions['id']; $content .= Html::tag('label', $labelOptions, $option . Html::clearText($optionContent)); } return $content; }
/** * @dataProvider toIdProvider */ public function testToId($input, $expected) { $res = \serviform\helpers\Html::toId($input); $this->assertEquals($expected, $res); }