/** * Constructor * * Instantiate the set of checkbox input form elements * * @param string $name * @param array $values * @param string $indent * @param string|array $marked * @return CheckboxSet */ public function __construct($name, array $values, $indent = null, $marked = null) { if (null !== $marked) { if (!is_array($marked)) { $marked = [$marked]; } } else { $marked = []; } parent::__construct('fieldset', null, null, false, $indent); $this->attributes['class'] = 'checkbox-fieldset'; $this->setMarked($marked); $this->setName($name . '[]'); // Create the checkbox elements and related span elements. $i = null; foreach ($values as $k => $v) { $checkbox = new Input\Checkbox($name . '[]', null, $indent); $checkbox->setAttributes(['class' => 'checkbox', 'id' => $name . $i, 'value' => $k]); // Determine if the current radio element is checked. if (in_array($k, $this->marked)) { $checkbox->setAttribute('checked', 'checked'); } $span = new Child('span', null, null, false, $indent); $span->setAttribute('class', 'checkbox-span'); $span->setNodeValue($v); $this->addChildren([$checkbox, $span]); $this->checkboxes[] = $checkbox; $i++; } $this->value = $values; }
/** * Constructor * * Instantiate the radio input form elements * * @param string $name * @param array $values * @param string $indent * @param string $marked * @return RadioSet */ public function __construct($name, array $values, $indent = null, $marked = null) { parent::__construct('fieldset', null, null, false, $indent); $this->attributes['class'] = 'radio-fieldset'; $this->setMarked($marked); $this->setName($name); // Create the radio elements and related span elements. $i = null; foreach ($values as $k => $v) { $radio = new Input\Radio($name, null, $indent); $radio->setAttributes(['class' => 'radio', 'id' => $name . $i, 'value' => $k]); // Determine if the current radio element is checked. if (null !== $this->marked && $k == $this->marked) { $radio->setAttribute('checked', 'checked'); } $span = new Child('span', null, null, false, $indent); $span->setAttribute('class', 'radio-span'); $span->setNodeValue($v); $this->addChildren([$radio, $span]); $this->radios[] = $radio; $i++; } $this->value = $values; }
/** * Constructor * * Instantiate the select form element object * * @param string $name * @param string|array $values * @param string $indent * @param array $config * @return Select */ public function __construct($name, $values, $indent = null, array $config = null) { $marked = isset($config['marked']) ? $config['marked'] : null; $multiple = isset($config['multiple']) ? (bool) $config['multiple'] : false; $data = isset($config['data']) ? $config['data'] : null; parent::__construct($this->type, null, null, false, $indent); $this->setAttributes(['name' => $name, 'id' => $name]); $this->setAsMultiple($multiple); $this->setMarked($marked); $values = self::parseValues($values, $data); // Create the child option elements. foreach ($values as $k => $v) { if (is_array($v)) { $opt = new Child('optgroup', null, null, false, $indent); $opt->setAttribute('label', $k); foreach ($v as $ky => $vl) { $o = new Child('option', null, null, false, $indent); $o->setAttribute('value', $ky); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($ky, $this->marked, true)) { $o->setAttribute('selected', 'selected'); } } else { if (null !== $this->marked && $ky == $this->marked) { $o->setAttribute('selected', 'selected'); } } $o->setNodeValue($vl); $opt->addChild($o); } } else { $opt = new Child('option', null, null, false, $indent); $opt->setAttribute('value', $k); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($k, $this->marked, true)) { $opt->setAttribute('selected', 'selected'); } } else { if (null !== $this->marked && $k == $this->marked) { $opt->setAttribute('selected', 'selected'); } } $opt->setNodeValue($v); } $this->addChild($opt); } $this->setValue($values); $this->setName($name); }