html.php 6.29 KB
Newer Older
1 2 3 4 5
<?php namespace System;

class HTML {

	/**
6 7 8 9 10 11 12
	 * Convert HTML characters to entities.
	 *
	 * @param  string  $value
	 * @return string
	 */
	public static function entities($value)
	{
13
		return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
14 15 16
	}

	/**
17 18 19 20 21 22 23
	 * Generate a JavaScript reference.
	 *
	 * @param  string  $url
	 * @return string
	 */
	public static function script($url)
	{
24
		return '<script type="text/javascript" src="'.static::entities(URL::to_asset($url)).'"></script>'.PHP_EOL;
25 26 27 28 29 30 31 32 33 34
	}

	/**
	 * Generate a CSS reference.
	 *
	 * @param  string  $url
	 * @return string
	 */
	public static function style($url, $media = 'all')
	{
35
		return '<link href="'.static::entities(URL::to_asset($url)).'" rel="stylesheet" type="text/css" media="'.$media.'">'.PHP_EOL;
36 37 38 39 40 41 42 43 44
	}

	/**
	 * Generate a HTML link.
	 *
	 * @param  string  $url
	 * @param  string  $title
	 * @param  array   $attributes
	 * @param  bool    $https
45
	 * @param  bool    $asset
46 47
	 * @return string
	 */
48
	public static function link($url, $title, $attributes = array(), $https = false, $asset = false)
49
	{
50
		return '<a href="'.static::entities(URL::to($url, $https, $asset)).'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
51 52 53 54 55 56 57 58 59 60
	}

	/**
	 * Generate a HTTPS HTML link.
	 *
	 * @param  string  $url
	 * @param  string  $title
	 * @param  array   $attributes
	 * @return string
	 */
61
	public static function link_to_secure($url, $title, $attributes = array())
62 63 64 65 66
	{
		return static::link($url, $title, $attributes, true);
	}

	/**
67 68 69 70 71 72 73
	 * Generate an HTML link to an asset.
	 *
	 * @param  string  $url
	 * @param  string  $title
	 * @param  array   $attributes
	 * @return string
	 */
74
	public static function link_to_asset($url, $title, $attributes = array())
75 76 77 78 79
	{
		return static::link($url, $title, $attributes, false, true);
	}

	/**
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	 * Generate an HTML link to a route.
	 *
	 * @param  string  $name
	 * @param  string  $title
	 * @param  array   $parameters
	 * @param  array   $attributes
	 * @return string
	 */
	public static function link_to_route($name, $title, $parameters = array(), $attributes = array(), $https = false)
	{
		return static::link(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
	 */
	public static function link_to_secure_route($name, $title, $parameters = array(), $attributes = array())
	{
		return static::link_to_route($name, $title, $parameters, $attributes, true);
	}

	/**
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	 * Generate an HTML mailto link.
	 *
	 * @param  string  $email
	 * @param  string  $title
	 * @param  array   $attributes
	 * @return string
	 */
	public static function mailto($email, $title = null, $attributes = array())
	{
		$email = static::email($email);

		if (is_null($title))
		{
			$title = $email;
		}

124
		return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	}

	/**
	 * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
	 *
	 * @param  string  $email
	 * @return string
	 */
	public static function email($email)
	{
		return str_replace('@', '&#64;', static::obfuscate($email));
	}

	/**
	 * Generate an HTML image.
	 *
	 * @param  string  $url
	 * @param  string  $alt
	 * @param  array   $attributes
	 * @return string
	 */
	public static function image($url, $alt = '', $attributes = array())
	{
148
		$attributes['alt'] = static::entities($alt);
149

150
		return '<img src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>';
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	}

	/**
	 * Generate an ordered list.
	 *
	 * @param  array   $list
	 * @param  array   $attributes
	 * @return string
	 */
	public static function ol($list, $attributes = array())
	{
		return static::list_elements('ol', $list, $attributes);
	}

	/**
	 * Generate an un-ordered list.
	 *
	 * @param  array   $list
	 * @param  array   $attributes
	 * @return string
	 */
	public static function ul($list, $attributes = array())
	{
		return static::list_elements('ul', $list, $attributes);
	}

	/**
	 * Generate an ordered or un-ordered list.
	 *
	 * @param  string  $type
	 * @param  array   $list
	 * @param  array   $attributes
	 * @return string
	 */
	private static function list_elements($type, $list, $attributes)
	{
		$html = '';

		foreach ($list as $key => $value)
		{
191
			$html .= '<li>'.static::entities($value).'</li>';
192 193
		}

194
		return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
195 196 197 198 199 200 201 202 203 204 205 206 207 208
	}

	/**
	 * Build a list of HTML attributes.
	 *
	 * @param  array   $attributes
	 * @return string
	 */		
	public static function attributes($attributes)
	{
		$html = array();

		foreach ($attributes as $key => $value)
		{
209 210
			// Assume numeric-keyed attributes to have the same key and value.
			// Example: required="required", autofocus="autofocus", etc.
211 212 213 214 215
			if (is_numeric($key))
			{
				$key = $value;
			}

216
			if ( ! is_null($value))
217
			{
218
				$html[] = $key.'="'.static::entities($value).'"';
219 220 221
			}
		}

222
		return (count($html) > 0) ? ' '.implode(' ', $html) : '';
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	}

	/**
	 * Obfuscate a string to prevent spam-bots from sniffing it.
	 *
	 * @param  string  $value
	 * @return string
	 */
	public static function obfuscate($value)
	{
		$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;
	}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	/**
	 * Magic Method for handling dynamic static methods.
	 */
	public static function __callStatic($method, $parameters)
	{
		// Handle the dynamic creation of links to secure routes.
		if (strpos($method, 'link_to_secure_') === 0)
		{
			array_unshift($parameters, substr($method, 15));
			return forward_static_call_array('HTML::link_to_secure_route', $parameters);
		}

		// Handle the dynamic creation of links to routes.
		if (strpos($method, 'link_to_') === 0)
		{
			array_unshift($parameters, substr($method, 8));
			return forward_static_call_array('HTML::link_to_route', $parameters);
		}
276 277

		throw new \Exception("Static method [$method] is not defined on the HTML class.");
278 279
	}

280
}