/** * {@inheritdoc} */ public function __invoke(OutputBuffer $out) { $options = $this->options; $multiple = $this->multiple; if (!is_array($options) && !is_object($options)) { throw new \InvalidArgumentException(sprintf('Invalid combobox options: "%s"', gettype($options))); } $builder = new TagBuilder('select'); $this->populatePassthroughAttributes($builder); $inspection = $this->inspectAttribute('bind'); if (NULL !== ($name = $this->name)) { $builder->set('name', $name . ($multiple ? '[]' : '')); } elseif (NULL !== $inspection->name) { $builder->set('name', $inspection->name); } $builder->addClass('kk'); $builder->setBoolean('disabled', $this->disabled); $builder->setBoolean('multiple', $multiple); if (NULL !== ($placeholder = $this->placeholder)) { $builder->set('data-placeholder', $placeholder); } $bind = $inspection->value; if ($multiple) { if ($bind === NULL || is_scalar($bind)) { $bound = [(string) $bind]; } else { if (!is_array($bind) && !$bind instanceof \Traversable) { throw new \InvalidArgumentException(sprintf('Invalid combobox bound value: "%s"', gettype($bind))); } $bound = []; foreach ($bind as $tmp) { $bound[] = (string) $tmp; } } } else { $bound = (array) $bind; } $buffer = $this->renderNestedNodes($out->createBuffer(), $builder); if ($options instanceof GroupedOptions) { foreach ($options->getOptions() as $group => $entries) { $buffer .= '<optgroup label="' . $out->escape($group) . '">'; foreach ($entries as $k => $v) { $buffer .= '<option value="' . $out->escape($k); if (in_array((string) $k, $bound, true)) { $buffer .= '" selected="selected'; } $buffer .= '">' . $out->escape($v) . '</option>'; } $buffer .= '</optgroup>'; } } else { foreach ($options as $k => $v) { $buffer .= '<option value="' . $out->escape($k); if (in_array((string) $k, $bound, true)) { $buffer .= '" selected="selected'; } $buffer .= '">' . $out->escape($v) . '</option>'; } } $out->writeTag($builder, $buffer, false); }