/** * Prepare the input. * * @param array $arguments Contains the passed arguments * [0] string $name Radio's name. Can be not unique, but will bind it to a group if not. * [1] string[] $opts Contains the option="value" key-value pairs to be added to the field * [2] boolean $yes If the yes Radio is checked as default */ public function prepare($arguments) { $name = $arguments[0]; $opts = $arguments[1]; $yes = $arguments[2]; $yesLang = Arr::get($opts, 'yes', 'Yes'); $noLang = Arr::get($opts, 'no', 'No'); Arr::remove('yes', $opts); Arr::remove('no', $opts); $html = '<div class="radio"><label>' . $yesLang; $radio = new Radio(); $html .= $radio->prepare([$name, '1', $opts, $yes]); $html .= '</label></div>'; $html .= '<div class="radio"><label>' . $noLang; $radio = new Radio(); $html .= $radio->prepare([$name, '0', $opts, !$yes]); $html .= '</label></div>'; return $html; }
/** * Prepare the input. * * @param array $arguments Contains the passed arguments * [0] string $name Radio group's name. * [1] Array[] $elements Array of radio elements. These must be PHP Arrays which all contain the ["id"] and ["name"] attributes (unless either "key" or "value" are redefined in the opts) * [2] string[] $opts Contains the option="value" key-value pairs to be added to the field * [3] Array[] $values Contains the element that needs to be checked (an is in array comparison is made) */ public function prepare($arguments) { $name = $arguments[0]; $elements = $arguments[1]; $opts = $arguments[2]; $values = $arguments[3]; $values = (array) $values; $hKey = Arr::get($opts, 'key', 'id'); $hValue = Arr::get($opts, 'value', 'name'); Arr::remove('key', $opts); Arr::remove('value', $opts); $html = ''; foreach ($elements as $element) { $html .= '<div class="radio">'; $html .= '<label>'; $html .= $element[$hValue]; $radio = new Radio(); $html .= $radio->prepare([$name, $element[$hKey], $opts, in_array($element[$hKey], $values)]); $html .= '</div>'; $html .= '</label>'; } return $html; }
public function testRadioPrepare() { $control = new Radio(); $expected = '<input type="radio" checked name="test" value="tested" class="toaster">'; $this->assertEquals($control->prepare(['test', 'tested', ['class' => 'toaster'], 'true']), $expected); }