Files
spa-api/laravel/html.php

345 lines
7.7 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
class HTML {
2011-08-26 21:42:04 -05:00
/**
* The encoding being used by the application.
*
* @var string
*/
private $encoding;
/**
* The URL generator instance.
*
* @var URL
*/
private $url;
/**
* Create a new HTML writer instance.
*
* @param string $encoding
* @return void
*/
public function __construct(URL $url, $encoding)
{
$this->url = $url;
$this->encoding = $encoding;
}
/**
* Convert HTML characters to entities.
*
2011-08-23 21:04:40 -05:00
* The encoding specified in the application configuration file will be used.
*
* @param string $value
* @return string
*/
2011-08-26 21:42:04 -05:00
public function entities($value)
{
2011-08-26 21:42:04 -05:00
return htmlentities($value, ENT_QUOTES, $this->encoding, false);
}
2011-06-08 23:45:08 -05:00
/**
* Generate a JavaScript reference.
*
* @param string $url
2011-07-29 23:17:57 -05:00
* @param array $attributes
2011-06-08 23:45:08 -05:00
* @return string
*/
2011-08-26 21:42:04 -05:00
public function script($url, $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-08-26 21:42:04 -05:00
$url = $this->entities($this->url->to_asset($url));
2011-08-26 21:42:04 -05:00
return '<script type="text/javascript" src="'.$url.'"'.$this->attributes($attributes).'></script>'.PHP_EOL;
2011-06-08 23:45:08 -05:00
}
/**
* Generate a CSS reference.
*
* @param string $url
2011-07-29 23:17:57 -05:00
* @param array $attributes
2011-06-08 23:45:08 -05:00
* @return string
*/
2011-08-26 21:42:04 -05:00
public function style($url, $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-08-08 09:07:32 -05:00
if ( ! array_key_exists('media', $attributes)) $attributes['media'] = 'all';
2011-08-23 21:04:40 -05:00
$attributes = array_merge($attributes, array('rel' => 'stylesheet', 'type' => 'text/css'));
2011-07-29 23:17:57 -05:00
2011-08-26 21:42:04 -05:00
return '<link href="'.$this->entities($this->url->to_asset($url)).'"'.$this->attributes($attributes).'>'.PHP_EOL;
2011-06-08 23:45:08 -05:00
}
2011-07-25 08:37:01 -07:00
/**
* Generate a HTML span.
*
* @param string $value
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function span($value, $attributes = array())
2011-07-25 08:37:01 -07:00
{
2011-08-26 21:42:04 -05:00
return '<span'.$this->attributes($attributes).'>'.$this->entities($value).'</span>';
2011-07-25 08:37:01 -07:00
}
2011-06-08 23:45:08 -05:00
/**
* Generate a HTML link.
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $https
* @param bool $asset
2011-06-08 23:45:08 -05:00
* @return string
*/
2011-08-26 21:42:04 -05:00
public function link($url, $title, $attributes = array(), $https = false, $asset = false)
2011-06-08 23:45:08 -05:00
{
2011-08-26 21:42:04 -05:00
$url = $this->entities($this->url->to($url, $https, $asset));
2011-08-26 21:42:04 -05:00
return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
2011-06-08 23:45:08 -05:00
}
/**
* Generate a HTTPS HTML link.
*
* @param string $url
* @param string $title
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function link_to_secure($url, $title, $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-08-26 21:42:04 -05:00
return $this->link($url, $title, $attributes, true);
2011-06-08 23:45:08 -05:00
}
/**
* Generate an HTML link to an asset.
*
* @param string $url
* @param string $title
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function link_to_asset($url, $title, $attributes = array(), $https = false)
{
2011-08-26 21:42:04 -05:00
return $this->link($url, $title, $attributes, $https, true);
}
/**
* Generate an HTTPS HTML link to an asset.
*
* @param string $url
* @param string $title
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function link_to_secure_asset($url, $title, $attributes = array())
{
2011-08-26 21:42:04 -05:00
return $this->link_to_asset($url, $title, $attributes, true);
}
/**
* Generate an HTML link to a route.
*
2011-08-23 21:04:40 -05:00
* An array of parameters may be specified to fill in URI segment wildcards.
*
* @param string $name
* @param string $title
* @param array $parameters
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function link_to_route($name, $title, $parameters = array(), $attributes = array(), $https = false)
{
2011-08-26 21:42:04 -05:00
return $this->link($this->url->to_route($name, $parameters, $https), $title, $attributes);
}
/**
* Generate an HTTPS HTML link to a route.
*
* @param string $name
* @param string $title
* @param array $parameters
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function link_to_secure_route($name, $title, $parameters = array(), $attributes = array())
{
2011-08-26 21:42:04 -05:00
return $this->link_to_route($name, $title, $parameters, $attributes, true);
}
2011-06-08 23:45:08 -05:00
/**
* Generate an HTML mailto link.
*
2011-08-23 21:04:40 -05:00
* The E-Mail address will be obfuscated to protect it from spam bots.
*
2011-06-08 23:45:08 -05:00
* @param string $email
* @param string $title
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function mailto($email, $title = null, $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-08-26 21:42:04 -05:00
$email = $this->email($email);
2011-06-08 23:45:08 -05:00
2011-08-08 09:07:32 -05:00
if (is_null($title)) $title = $email;
2011-06-08 23:45:08 -05:00
$email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;
2011-08-26 21:42:04 -05:00
return '<a href="'.$email.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
2011-06-08 23:45:08 -05:00
}
/**
* Obfuscate an e-mail address to prevent spam-bots from sniffing it.
*
* @param string $email
* @return string
*/
2011-08-26 21:42:04 -05:00
public function email($email)
2011-06-08 23:45:08 -05:00
{
2011-08-26 21:42:04 -05:00
return str_replace('@', '&#64;', $this->obfuscate($email));
2011-06-08 23:45:08 -05:00
}
/**
* Generate an HTML image.
*
* @param string $url
* @param string $alt
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function image($url, $alt = '', $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-08-26 21:42:04 -05:00
$attributes['alt'] = $this->entities($alt);
2011-07-05 06:58:27 -07:00
2011-08-26 21:42:04 -05:00
return '<img src="'.$this->entities($this->url->to_asset($url)).'"'.$this->attributes($attributes).'>';
2011-06-08 23:45:08 -05:00
}
/**
* Generate an ordered list.
*
* @param array $list
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function ol($list, $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-08-26 21:42:04 -05:00
return $this->list_elements('ol', $list, $attributes);
2011-06-08 23:45:08 -05:00
}
/**
* Generate an un-ordered list.
*
* @param array $list
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function ul($list, $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-08-26 21:42:04 -05:00
return $this->list_elements('ul', $list, $attributes);
2011-06-08 23:45:08 -05:00
}
/**
* Generate an ordered or un-ordered list.
*
* @param string $type
* @param array $list
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
private function list_elements($type, $list, $attributes = array())
2011-06-08 23:45:08 -05:00
{
$html = '';
foreach ($list as $key => $value)
{
2011-08-26 21:42:04 -05:00
$html .= (is_array($value)) ? $this->list_elements($type, $value) : '<li>'.$this->entities($value).'</li>';
2011-06-08 23:45:08 -05:00
}
2011-08-26 21:42:04 -05:00
return '<'.$type.$this->attributes($attributes).'>'.$html.'</'.$type.'>';
2011-06-08 23:45:08 -05:00
}
/**
* Build a list of HTML attributes.
*
* @param array $attributes
* @return string
*/
2011-08-26 21:42:04 -05:00
public function attributes($attributes)
2011-06-08 23:45:08 -05:00
{
$html = array();
foreach ($attributes as $key => $value)
{
2011-07-06 14:29:48 -07:00
// Assume numeric-keyed attributes to have the same key and value.
// Example: required="required", autofocus="autofocus", etc.
2011-08-08 09:07:32 -05:00
if (is_numeric($key)) $key = $value;
if ( ! is_null($value))
2011-06-08 23:45:08 -05:00
{
2011-08-26 21:42:04 -05:00
$html[] = $key.'="'.$this->entities($value).'"';
2011-06-08 23:45:08 -05:00
}
}
return (count($html) > 0) ? ' '.implode(' ', $html) : '';
2011-06-08 23:45:08 -05:00
}
/**
* Obfuscate a string to prevent spam-bots from sniffing it.
*
* @param string $value
* @return string
*/
2011-08-26 21:42:04 -05:00
public function obfuscate($value)
2011-06-08 23:45:08 -05:00
{
$safe = '';
foreach (str_split($value) as $letter)
{
switch (rand(1, 3))
{
// Convert the letter to its entity representation.
case 1:
$safe .= '&#'.ord($letter).';';
break;
// Convert the letter to a Hex character code.
case 2:
$safe .= '&#x'.dechex(ord($letter)).';';
break;
// No encoding.
case 3:
$safe .= $letter;
}
}
return $safe;
}
/**
* Magic Method for handling dynamic static methods.
2011-08-23 21:04:40 -05:00
*
* This method primarily handles dynamic calls to create links to named routes.
*/
2011-08-26 21:42:04 -05:00
public function __call($method, $parameters)
{
if (strpos($method, 'link_to_secure_') === 0)
{
array_unshift($parameters, substr($method, 15));
2011-08-08 09:07:32 -05:00
2011-08-26 21:42:04 -05:00
return call_user_func_array(array($this, 'link_to_secure_route'), $parameters);
}
if (strpos($method, 'link_to_') === 0)
{
array_unshift($parameters, substr($method, 8));
2011-08-08 09:07:32 -05:00
2011-08-26 21:42:04 -05:00
return call_user_func_array(array($this, 'link_to_route'), $parameters);
}
2011-08-26 21:42:04 -05:00
throw new \Exception("Method [$method] is not defined on the HTML class.");
}
2011-06-08 23:45:08 -05:00
}