/** {@inheritdoc} */ public function renderOptions(array $options, array $selectedOptions = array()) { $escapeHtml = $this->getEscapeHtmlHelper(); $optionStrings = array(); foreach ($options as $option) { $this->validTagAttributes = $this->validOptionAttributes; if (\Zend\Stdlib\ArrayUtils::inArray($option, $selectedOptions)) { $attributes = ' selected="selected"'; } else { $attributes = ''; } $optionStrings[] = sprintf('<option%s>%s</option>', $attributes, $escapeHtml($option)); } return "\n" . implode("\n", $optionStrings) . "\n"; }
/** * Option setter with validation * If option can have the specified value then it is set, otherwise this method throws exception * * Tip: call it into your setter methods. * * @param $key * @param $value * @return $this * @throws Exception\DomainException * @throws Exception\InvalidArgumentException */ protected function setOption($key, $value) { if (!isset($this->config[$key])) { throw new Exception\DomainException(sprintf('Option "%s" does not exist; available options are (%s)', $key, implode(', ', array_map(function ($opt) { return '"' . $opt . '"'; }, array_keys($this->config))))); } if (!ArrayUtils::isList($this->config[$key], false)) { throw new Exception\DomainException(sprintf('Option "%s" does not have a list of allowed values', $key)); } if (!ArrayUtils::inArray($value, $this->config[$key], true)) { throw new Exception\InvalidArgumentException(sprintf('Option "%s" can not be set to value "%s"; allowed values are (%s)', $key, $value, implode(', ', array_map(function ($val) { return '"' . $val . '"'; }, $this->config[$key])))); } $this->options[$key] = $value; return $this; }
/** * Render an array of options * * Individual options should be of the form: * * <code> * array( * 'value' => 'value', * 'label' => 'label', * 'disabled' => $booleanFlag, * 'selected' => $booleanFlag, * ) * </code> * * @param array $options * @param array $selectedOptions Option values that should be marked as selected * @return string */ public function renderOptions(array $options, array $selectedOptions = array()) { $template = '<option %s>%s</option>'; $optionStrings = array(); $escapeHtml = $this->getEscapeHtmlHelper(); foreach ($options as $key => $optionSpec) { $value = ''; $label = ''; $selected = false; $disabled = false; if (is_scalar($optionSpec)) { $optionSpec = array('label' => $optionSpec, 'value' => $key); } if (isset($optionSpec['options']) && is_array($optionSpec['options'])) { $optionStrings[] = $this->renderOptgroup($optionSpec, $selectedOptions); continue; } if (isset($optionSpec['value'])) { $value = $optionSpec['value']; } if (isset($optionSpec['label'])) { $label = $optionSpec['label']; unset($optionSpec['label']); } if (isset($optionSpec['selected'])) { $selected = $optionSpec['selected']; } if (isset($optionSpec['disabled'])) { $disabled = $optionSpec['disabled']; } if (ArrayUtils::inArray($value, $selectedOptions)) { $selected = true; } if (null !== ($translator = $this->getTranslator())) { $label = $translator->translate($label, $this->getTranslatorTextDomain()); } $attributes = array_merge($optionSpec, compact('value', 'selected', 'disabled')); $this->validTagAttributes = $this->validOptionAttributes; $optionStrings[] = sprintf($template, $this->createAttributesString($attributes), $escapeHtml($label)); } return implode("\n", $optionStrings); }