<?php
class HTMLUtils
{
/**
* Creates the HTML for a table
*
* @param array $rows
* @param array|null $headers If specified, used as table headers
* @param array $attributes
* @return string
*/
public static function table(array $rows, array $headers = NULL, array $attributes = array())
{
$cells = array();
$body = '';
$head = '';
if ($headers !== NULL && count($headers) > 0)
{
$head = '<thead><tr><th>'.implode('</th><th>', $headers).'</th></tr></thead>';
}
foreach ($rows as $row)
{
if (count($row) > 0)
{
$cells[] = '<td>'.implode('</td><td>', $row).'</td>';
}
}
if (count($cells) > 0)
{
$body = '<tr>'.implode('</tr><tr>', $cells).'</tr>';
}
return '<table '.self::_attributes($attributes).'>'.$head.'<tbody>'.$body.'</tbody>'.'</table>';
}
/**
* Creates the HTML for a select field
*
* @param $name
* @param array $options
* @param mixed $selected The value of the selected option, if any
* @param array $attributes
* @return string
*/
public static function select($name, array $options, $selected = NULL, array $attributes = array())
{
$_options = array();
foreach ($options as $option => $value)
{
$_attr = array('value' => $value);
if ($value == $selected)
{
$_attr['selected'] = 'selected';
}
$_options[] = '<option '.self::_attributes($_attr).'>'.$option.'</option>';
}
return '<select '.self::_attributes(array(
'name' => $name
) + $attributes).'>'.implode($_options).'</select>';
}
/**
* Creates the HTML for an ordered list
*
* Reason for separation of the different kinds of lists, are foremost that the generic "list" is a reserved keyword,
* and can therefore not be used (unless in a namespace). It does however provide a much cleaner API this way.
*
* @param array $items
* @param array $attributes
* @return string
*/
public static function ol(array $items, array $attributes = array())
{
return '<ol '.self::_attributes($attributes).'>'.self::_list($items).'</ol>';
}
/**
* Creates the HTML for an unordered list
*
* Reason for separation of the different kinds of lists, are foremost that the generic "list" is a reserved keyword,
* and can therefore not be used (unless in a namespace). It does however provide a much cleaner API this way.
*
* @param array $items
* @param array $attributes
* @return string
*/
public static function ul(array $items, array $attributes = array())
{
return '<ul '.self::_attributes($attributes).'>'.self::_list($items).'</ul>';
}
/**
* Creates the HTML for an anchor (link)
*
* @param string $url
* @param null|string $label The label for this anchor, if null it will default to $url
* @param array $attributes
* @return string
*/
public static function anchor($url, $label = NULL, array $attributes = array())
{
return '<a '.self::_attributes(array(
'href' => $url
) + $attributes).'>'.($label ? $label : $url).'</a>';
}
/**
* Creates the HTML for list items
*
* @param array $items
* @return string
*/
protected static function _list(array $items)
{
return count($items) > 0 ? '<li>'.implode('</li><li>', $items).'</li>' : '';
}
/**
* Generic way of creating properties for the HTML
*
* @param array $attributes The properties should be set up as array(property => value)
* @return string
*/
protected static function _attributes(array $attributes)
{
$_attr = array();
foreach ($attributes as $attribute => $value)
{
$_attr[] = $attribute.'="'.$value.'"';
}
return implode(' ', $_attr);
}
}