/**
  * Method to test pivotBySort().
  *
  * @param array $data
  * @param array $expected
  *
  * @return void
  *
  * @dataProvider pivotSortDataProvider
  * @covers       \Windwalker\Helper\ArrayHelper::pivotBySort
  */
 public function testPivotBySort($data, $expected)
 {
     $this->assertEquals($expected, ArrayHelper::pivotBySort($data));
 }
Exemplo n.º 2
0
 /**
  * Convert a element data to JForm XML field string.
  *
  * @param    string $field_type Type of this field.
  * @param    array  $attrs      Field's attributes.
  * @param    array  $option     Some option.
  *
  * @return  string  Field XML element.
  */
 public function buildElement($field_type = 'text', $attrs = null, $option = array())
 {
     $node_name = !empty($option['node_name']) ? $option['node_name'] : 'field';
     $field_type = $field_type ? $field_type : 'text';
     $input = $this->container->get('input');
     $attrs = $this->convertJsonToArray($attrs);
     if (!is_array($attrs)) {
         $attrs = $input->getVar('attrs', array());
     }
     $this->event->trigger('onCCKEngineBeforeBuildElement', array(\JArrayHelper::getValue($option, 'context'), &$field_type, &$attrs, $option));
     if (!is_array($attrs)) {
         return '<' . $node_name . '/>';
     }
     // Rebuild options
     // ================================================================
     if (isset($attrs['options'])) {
         $attrs['options'] = ArrayHelper::pivotBySort($attrs['options']);
     }
     $element = '';
     $options = array();
     // Build Field Attrs
     // ================================================================
     // set type in attrs
     $attrs['type'] = $field_type;
     // set default
     if (is_array(\JArrayHelper::getValue($attrs, 'default'))) {
         $attrs['default'] = implode(',', $attrs['default']);
     }
     // start buliding attrs and options
     foreach ($attrs as $key => $attr) {
         if ($key == 'options' && is_array($attr)) {
             // Bulid options
             foreach ($attr as $key => $opt) {
                 if (!trim($opt['value'])) {
                     continue;
                 }
                 $value = addslashes(trim($opt['value']));
                 $text = htmlspecialchars(addslashes(trim($opt['text'])));
                 $options[] = "\t<option value=\"{$value}\">{$text}</option>\n";
             }
         } else {
             // Build attributes
             if (!trim($attr)) {
                 continue;
             }
             $attr = trim($attr);
             $attr = htmlspecialchars(addslashes($attr));
             $element .= "\t{$key}=\"{$attr}\"\n";
         }
     }
     // Build Element
     // ================================================================
     if (count($options) < 0) {
         $element = "<{$node_name}\n{$element}/>";
     } else {
         $options = implode('', $options);
         $element = "<{$node_name}\n{$element}>\n{$options}</{$node_name}>";
     }
     $this->event->trigger('onCCKEngineAfterBuildElement', array(\JArrayHelper::getValue($option, 'context'), &$field_type, &$element, $option));
     return $element;
 }