Exemplo n.º 1
0
 public function buildContent()
 {
     $ret = '';
     if (empty($this->attributes['required'])) {
         $ret = Tag::option(array('value' => ''))->setContent('')->build() . "\n";
     }
     if (!empty($this->options)) {
         if (is_array($this->options)) {
             foreach ($this->options as $value => $label) {
                 $option = Tag::option(array('value' => $value, 'content' => $label));
                 if ($value === 'NULL' && !isset($this->attributes['value'])) {
                     $option->setSelected(true);
                 } else {
                     if (isset($this->attributes['value']) && self::compareValues($this->attributes['value'], $value)) {
                         $option->setSelected(true);
                     }
                 }
                 $ret .= $option->build() . "\n";
             }
         } else {
             if (isset($this->attributes['value'])) {
                 $this->options = preg_replace('%(value=(?:\'|")' . preg_quote($this->attributes['value'], '%') . '(?:\'|"))%is', '$1 selected', $this->options);
             }
             $ret .= $this->options;
         }
     }
     return $ret;
 }
Exemplo n.º 2
0
 /**
  * @param array $options
  * @return string
  * @throws \Swayok\Html\HtmlTagException
  */
 private function buildFieldOptions(array $options)
 {
     $ret = '';
     foreach ($options as $value => $label) {
         if (!is_array($label)) {
             $ret .= Tag::option()->setContent($label)->setValue($value)->build();
         } else {
             $ret .= Tag::create()->setName('optgroup')->setAttribute('label', $value)->setContent($this->buildFieldOptions($label))->build();
         }
     }
     return $ret;
 }