formatProperties() public static method

Takes an array of key-value pairs and formats them in the syntax of HTML-container properties
public static formatProperties ( array $properties ) : string
$properties array
return string
コード例 #1
0
 /**
  * Returns a set of radio form elements
  *
  * array(
  * 'name' => '',
  * 'value' => '',
  * 'id' => '',
  * 'legend' => '',
  * 'options' => array('value1' => 'text1', 'value2' => 'text2', 'value3' => 'text3'),
  * 'options' => array('text1', 'text2', 'text3'), //also acceptable (cannot do half this, half above syntax)
  * )
  *
  * @param array $args
  * @return str
  */
 public function radios(array $args)
 {
     $id = isset($args['id']) ? $args['id'] : $args['name'];
     $properties = $this->_getProperties($args);
     if (isset($properties['value'])) {
         $checked = $properties['value'];
         unset($properties['value']);
     }
     $properties['type'] = isset($args['type']) ? $args['type'] : 'radio';
     $useValues = key($args['options']) !== 0 || isset($args['useValue']) && $args['useValue'];
     foreach ($args['options'] as $value => $text) {
         if (!$useValues) {
             $value = $text;
         }
         $properties['id'] = $id . '_' . preg_replace('/\\W/', '', $value);
         $properties['value'] = $value;
         if (isset($checked) && (($properties['type'] == 'radio' || !is_array($checked)) && $value == $checked || $properties['type'] == 'checkbox' && is_array($checked) && in_array((string) $value, $checked))) {
             $properties['checked'] = 'checked';
             $rowClass = !isset($properties['class']) ? 'checked' : $properties['class'] . ' checked';
         }
         $labelFirst = isset($args['labelFirst']) ? $args['labelFirst'] : false;
         $labelArgs = array('label' => $text, 'id' => $properties['id'], 'labelFirst' => $labelFirst);
         $input = '<input ' . htmlHelper::formatProperties($properties) . ' />';
         $row = $this->_getLabel($labelArgs, $input);
         if (isset($rowClass)) {
             $row = '<span class="' . $rowClass . '">' . $row . '</span>';
         }
         $radios[] = $row;
         unset($properties['checked'], $rowClass);
     }
     $this->{$properties['type'] == 'radio' ? 'radios' : 'checkboxes'} = $radios;
     $break = !isset($args['optionBreak']) ? '<br />' : $args['optionBreak'];
     $addFieldset = isset($args['addFieldset']) ? $args['addFieldset'] : isset($args['label']) && $args['label'] || count($args['options']) > 1;
     if ($addFieldset) {
         $return = '<fieldset id="' . $id . '">';
         if (isset($args['label'])) {
             $return .= '<legend>' . get::htmlentities($args['label']) . '</legend>';
         }
         $return .= implode($break, $radios) . '</fieldset>';
     } else {
         $return = implode($break, $radios);
     }
     if (isset($_POST['errors']) && isset($_POST['errors'][$id])) {
         $return = $this->getErrorMessageContainer($id, $_POST['errors'][$id]) . $return;
     }
     return $return;
 }