1. <?php
  2.  
  3. class HTMLUtils
  4. {
  5.  
  6.     /**
  7.      * Creates the HTML for a table
  8.      *
  9.      * @param  array       $rows
  10.      * @param  array|null  $headers    If specified, used as table headers
  11.      * @param  array       $attributes
  12.      * @return string
  13.      */
  14.     public static function table(array $rows, array $headers = NULL, array $attributes = array())
  15.     {
  16.         $cells = array();
  17.         $body  = '';
  18.         $head  = '';
  19.  
  20.         if ($headers !== NULL && count($headers) > 0)
  21.         {
  22.             $head = '<thead><tr><th>'.implode('</th><th>', $headers).'</th></tr></thead>';
  23.         }
  24.  
  25.         foreach ($rows as $row)
  26.         {
  27.             if (count($row) > 0)
  28.             {
  29.                 $cells[] = '<td>'.implode('</td><td>', $row).'</td>';
  30.             }
  31.         }
  32.  
  33.         if (count($cells) > 0)
  34.         {
  35.             $body = '<tr>'.implode('</tr><tr>', $cells).'</tr>';
  36.         }
  37.  
  38.         return '<table '.self::_attributes($attributes).'>'.$head.'<tbody>'.$body.'</tbody>'.'</table>';
  39.     }
  40.  
  41.     /**
  42.      * Creates the HTML for a select field
  43.      *
  44.      * @param         $name
  45.      * @param  array  $options
  46.      * @param  mixed  $selected   The value of the selected option, if any
  47.      * @param  array  $attributes
  48.      * @return string
  49.      */
  50.     public static function select($name, array $options, $selected = NULL, array $attributes = array())
  51.     {
  52.         $_options = array();
  53.  
  54.         foreach ($options as $option => $value)
  55.         {
  56.             $_attr = array('value' => $value);
  57.  
  58.             if ($value == $selected)
  59.             {
  60.                 $_attr['selected'] = 'selected';
  61.             }
  62.  
  63.             $_options[] = '<option '.self::_attributes($_attr).'>'.$option.'</option>';
  64.         }
  65.  
  66.         return '<select '.self::_attributes(array(
  67.             'name' => $name
  68.         ) + $attributes).'>'.implode($_options).'</select>';
  69.     }
  70.  
  71.     /**
  72.      * Creates the HTML for an ordered list
  73.      *
  74.      * Reason for separation of the different kinds of lists, are foremost that the generic "list" is a reserved keyword,
  75.      * and can therefore not be used (unless in a namespace). It does however provide a much cleaner API this way.
  76.      *
  77.      * @param  array  $items
  78.      * @param  array  $attributes
  79.      * @return string
  80.      */
  81.     public static function ol(array $items, array $attributes = array())
  82.     {
  83.         return '<ol '.self::_attributes($attributes).'>'.self::_list($items).'</ol>';
  84.     }
  85.  
  86.     /**
  87.      * Creates the HTML for an unordered list
  88.      *
  89.      * Reason for separation of the different kinds of lists, are foremost that the generic "list" is a reserved keyword,
  90.      * and can therefore not be used (unless in a namespace). It does however provide a much cleaner API this way.
  91.      *
  92.      * @param  array  $items
  93.      * @param  array  $attributes
  94.      * @return string
  95.      */
  96.     public static function ul(array $items, array $attributes = array())
  97.     {
  98.         return '<ul '.self::_attributes($attributes).'>'.self::_list($items).'</ul>';
  99.     }
  100.  
  101.     /**
  102.      * Creates the HTML for an anchor (link)
  103.      *
  104.      * @param  string      $url
  105.      * @param  null|string $label      The label for this anchor, if null it will default to $url
  106.      * @param  array       $attributes
  107.      * @return string
  108.      */
  109.     public static function anchor($url, $label = NULL, array $attributes = array())
  110.     {
  111.         return '<a '.self::_attributes(array(
  112.             'href'   => $url
  113.         ) + $attributes).'>'.($label ? $label : $url).'</a>';
  114.     }
  115.  
  116.     /**
  117.      * Creates the HTML for list items
  118.      *
  119.      * @param  array  $items
  120.      * @return string
  121.      */
  122.     protected static function _list(array $items)
  123.     {
  124.         return count($items) > 0 ? '<li>'.implode('</li><li>', $items).'</li>' : '';
  125.     }
  126.  
  127.     /**
  128.      * Generic way of creating properties for the HTML
  129.      *
  130.      * @param  array  $attributes The properties should be set up as array(property => value)
  131.      * @return string
  132.      */
  133.     protected static function _attributes(array $attributes)
  134.     {
  135.         $_attr = array();
  136.  
  137.         foreach ($attributes as $attribute => $value)
  138.         {
  139.             $_attr[] = $attribute.'="'.$value.'"';
  140.         }
  141.  
  142.         return implode(' ', $_attr);
  143.     }
  144.  
  145. }