Пример #1
0
 public function testSetOptionsOptions()
 {
     $element = new MultiCheckboxElement();
     $element->setOptions(array('value_options' => array('bar' => 'baz'), 'options' => array('foo' => 'bar')));
     $this->assertEquals(array('bar' => 'baz'), $element->getOption('value_options'));
     $this->assertEquals(array('foo' => 'bar'), $element->getOption('options'));
 }
Пример #2
0
 /**
  * Render options
  *
  * @param  MultiCheckboxElement $element
  * @param  array                $options
  * @param  array                $selectedOptions
  * @param  array                $attributes
  * @return string
  */
 protected function renderOptions(MultiCheckboxElement $element, array $options, array $selectedOptions, array $attributes)
 {
     $escapeHtmlHelper = $this->getEscapeHtmlHelper();
     $labelHelper = $this->getLabelHelper();
     $labelClose = $labelHelper->closeTag();
     $labelPosition = $this->getLabelPosition();
     $globalLabelAttributes = $element->getLabelAttributes();
     $closingBracket = $this->getInlineClosingBracket();
     if (empty($globalLabelAttributes)) {
         $globalLabelAttributes = $this->labelAttributes;
     }
     $combinedMarkup = array();
     $count = 0;
     foreach ($options as $key => $optionSpec) {
         $count++;
         if ($count > 1 && array_key_exists('id', $attributes)) {
             unset($attributes['id']);
         }
         $value = '';
         $label = '';
         $inputAttributes = $attributes;
         $labelAttributes = $globalLabelAttributes;
         $selected = isset($inputAttributes['selected']) && $inputAttributes['type'] != 'radio' && $inputAttributes['selected'] != false ? true : false;
         $disabled = isset($inputAttributes['disabled']) && $inputAttributes['disabled'] != false ? true : false;
         if (is_scalar($optionSpec)) {
             $optionSpec = array('label' => $optionSpec, 'value' => $key);
         }
         if (isset($optionSpec['value'])) {
             $value = $optionSpec['value'];
         }
         if (isset($optionSpec['label'])) {
             $label = $optionSpec['label'];
         }
         if (isset($optionSpec['selected'])) {
             $selected = $optionSpec['selected'];
         }
         if (isset($optionSpec['disabled'])) {
             $disabled = $optionSpec['disabled'];
         }
         if (isset($optionSpec['label_attributes'])) {
             $labelAttributes = isset($labelAttributes) ? array_merge($labelAttributes, $optionSpec['label_attributes']) : $optionSpec['label_attributes'];
         }
         if (isset($optionSpec['attributes'])) {
             $inputAttributes = array_merge($inputAttributes, $optionSpec['attributes']);
         }
         if (in_array($value, $selectedOptions)) {
             $selected = true;
         }
         $inputAttributes['value'] = $value;
         $inputAttributes['checked'] = $selected;
         $inputAttributes['disabled'] = $disabled;
         $input = sprintf('<input %s%s', $this->createAttributesString($inputAttributes), $closingBracket);
         if (null !== ($translator = $this->getTranslator())) {
             $label = $translator->translate($label, $this->getTranslatorTextDomain());
         }
         if (empty($labelAttributes)) {
             $labelAttributes = array();
         }
         $mode = $element->getOption('radio_mode');
         $mode = $mode ? $mode : 'inline';
         $labelAttributes['class'] = $element->getAttribute('type') . ' ' . $mode . (empty($labelAttributes['class']) ? '' : ' ' . $labelAttributes['class']);
         $label = $escapeHtmlHelper($label);
         $labelOpen = $labelHelper->openTag($labelAttributes);
         $template = $labelOpen . '%s%s' . $labelClose;
         switch ($labelPosition) {
             case self::LABEL_PREPEND:
                 $markup = sprintf($template, $label, $input);
                 break;
             case self::LABEL_APPEND:
             default:
                 $markup = sprintf($template, $input, $label);
                 break;
         }
         $combinedMarkup[] = $markup;
     }
     return implode($this->getSeparator(), $combinedMarkup);
 }