html.php 10.5 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2 3 4 5

class HTML {

	/**
6 7 8 9 10 11
	 * The registered custom macros.
	 *
	 * @var array
	 */
	public static $macros = array();

resurtm committed
12 13 14 15
	/**
	 * Registers a custom macro.
	 *
	 * @param  string   $name
Sergii Grebeniuk committed
16
	 * @param  Closure  $macro
resurtm committed
17 18
	 * @return void
	 */
19 20 21 22 23 24
	public static function macro($name, $macro)
	{
		static::$macros[$name] = $macro;
	}

	/**
25 26
	 * Convert HTML characters to entities.
	 *
Taylor Otwell committed
27 28
	 * The encoding specified in the application configuration file will be used.
	 *
29 30 31
	 * @param  string  $value
	 * @return string
	 */
32
	public static function entities($value)
33
	{
34
		return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
35 36 37
	}

	/**
38 39 40 41 42 43 44 45 46 47 48
	 * Convert entities to HTML characters.
	 *
	 * @param  string  $value
	 * @return string
	 */
	public static function decode($value)
	{
		return html_entity_decode($value, ENT_QUOTES, Config::get('application.encoding'));
	}

	/**
49 50 51 52 53 54 55 56 57 58 59 60 61
	 * Convert HTML special characters.
	 *
	 * The encoding specified in the application configuration file will be used.
	 *
	 * @param  string  $value
	 * @return string
	 */
	public static function specialchars($value)
	{
		return htmlspecialchars($value, ENT_QUOTES, Config::get('application.encoding'), false);
	}

	/**
62 63 64 65 66 67 68 69 70
	 * Generate a link to a JavaScript file.
	 *
	 * <code>
	 *		// Generate a link to a JavaScript file
	 *		echo HTML::script('js/jquery.js');
	 *
	 *		// Generate a link to a JavaScript file and add some attributes
	 *		echo HTML::script('js/jquery.js', array('defer'));
	 * </code>
71 72
	 *
	 * @param  string  $url
73
	 * @param  array   $attributes
74 75
	 * @return string
	 */
76
	public static function script($url, $attributes = array())
77
	{
Taylor Otwell committed
78
		$url = URL::to_asset($url);
79

80
		return '<script src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
81 82 83
	}

	/**
84
	 * Generate a link to a CSS file.
85
	 *
86 87
	 * If no media type is selected, "all" will be used.
	 *
88 89 90 91 92 93 94 95
	 * <code>
	 *		// Generate a link to a CSS file
	 *		echo HTML::style('css/common.css');
	 *
	 *		// Generate a link to a CSS file and add some attributes
	 *		echo HTML::style('css/common.css', array('media' => 'print'));
	 * </code>
	 *
96
	 * @param  string  $url
97
	 * @param  array   $attributes
98 99
	 * @return string
	 */
100
	public static function style($url, $attributes = array())
101
	{
102
		$defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
103

104
		$attributes = $attributes + $defaults;
105

Taylor Otwell committed
106
		$url = URL::to_asset($url);
107 108

		return '<link href="'.$url.'"'.static::attributes($attributes).'>'.PHP_EOL;
109 110 111
	}

	/**
Taylor Otwell committed
112 113 114 115 116 117
	 * Generate a HTML span.
	 *
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
118
	public static function span($value, $attributes = array())
Taylor Otwell committed
119
	{
120
		return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
Taylor Otwell committed
121 122 123
	}

	/**
124 125
	 * Generate a HTML link.
	 *
126 127 128 129 130 131 132 133
	 * <code>
	 *		// Generate a link to a location within the application
	 *		echo HTML::link('user/profile', 'User Profile');
	 *
	 *		// Generate a link to a location outside of the application
	 *		echo HTML::link('http://google.com', 'Google');
	 * </code>
	 *
134 135 136 137 138 139
	 * @param  string  $url
	 * @param  string  $title
	 * @param  array   $attributes
	 * @param  bool    $https
	 * @return string
	 */
140
	public static function link($url, $title = null, $attributes = array(), $https = null)
141
	{
Taylor Otwell committed
142
		$url = URL::to($url, $https);
143

144 145
		if (is_null($title)) $title = $url;

146
		return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
147 148 149 150 151 152 153 154 155 156
	}

	/**
	 * Generate a HTTPS HTML link.
	 *
	 * @param  string  $url
	 * @param  string  $title
	 * @param  array   $attributes
	 * @return string
	 */
157
	public static function link_to_secure($url, $title = null, $attributes = array())
158
	{
159
		return static::link($url, $title, $attributes, true);
160 161 162
	}

	/**
163 164
	 * Generate an HTML link to an asset.
	 *
165 166
	 * The application index page will not be added to asset links.
	 *
167 168 169
	 * @param  string  $url
	 * @param  string  $title
	 * @param  array   $attributes
170
	 * @param  bool    $https
171 172
	 * @return string
	 */
173
	public static function link_to_asset($url, $title = null, $attributes = array(), $https = null)
174
	{
Taylor Otwell committed
175
		$url = URL::to_asset($url, $https);
176 177

		return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
178 179 180 181 182 183 184 185 186 187
	}

	/**
	 * Generate an HTTPS HTML link to an asset.
	 *
	 * @param  string  $url
	 * @param  string  $title
	 * @param  array   $attributes
	 * @return string
	 */
188
	public static function link_to_secure_asset($url, $title = null, $attributes = array())
189
	{
190
		return static::link_to_asset($url, $title, $attributes, true);
191 192 193
	}

	/**
194 195
	 * Generate an HTML link to a route.
	 *
Taylor Otwell committed
196 197
	 * An array of parameters may be specified to fill in URI segment wildcards.
	 *
198 199 200 201 202 203 204 205
	 * <code>
	 *		// Generate a link to the "profile" named route
	 *		echo HTML::link_to_route('profile', 'Profile');
	 *
	 *		// Generate a link to the "profile" route and add some parameters
	 *		echo HTML::link_to_route('profile', 'Profile', array('taylor'));
	 * </code>
	 *
206 207 208 209 210 211
	 * @param  string  $name
	 * @param  string  $title
	 * @param  array   $parameters
	 * @param  array   $attributes
	 * @return string
	 */
212
	public static function link_to_route($name, $title = null, $parameters = array(), $attributes = array())
213
	{
214
		return static::link(URL::to_route($name, $parameters), $title, $attributes);
215 216 217
	}

	/**
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	 * Generate an HTML link to a controller action.
	 *
	 * An array of parameters may be specified to fill in URI segment wildcards.
	 *
	 * <code>
	 *		// Generate a link to the "home@index" action
	 *		echo HTML::link_to_action('home@index', 'Home');
	 *
	 *		// Generate a link to the "user@profile" route and add some parameters
	 *		echo HTML::link_to_action('user@profile', 'Profile', array('taylor'));
	 * </code>
	 *
	 * @param  string  $action
	 * @param  string  $title
	 * @param  array   $parameters
	 * @param  array   $attributes
	 * @return string
	 */
236
	public static function link_to_action($action, $title = null, $parameters = array(), $attributes = array())
237 238 239 240 241
	{
		return static::link(URL::to_action($action, $parameters), $title, $attributes);
	}

	/**
242 243
	 * Generate an HTML mailto link.
	 *
Taylor Otwell committed
244 245
	 * The E-Mail address will be obfuscated to protect it from spam bots.
	 *
246 247 248 249 250
	 * @param  string  $email
	 * @param  string  $title
	 * @param  array   $attributes
	 * @return string
	 */
251
	public static function mailto($email, $title = null, $attributes = array())
252
	{
253
		$email = static::email($email);
254

255
		if (is_null($title)) $title = $email;
256

257 258
		$email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;

259
		return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
260 261 262 263 264 265 266 267
	}

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

	/**
274 275
	 * Generate an HTML image element.
	 *
276 277 278 279 280
	 * @param  string  $url
	 * @param  string  $alt
	 * @param  array   $attributes
	 * @return string
	 */
281
	public static function image($url, $alt = '', $attributes = array())
282
	{
283
		$attributes['alt'] = $alt;
284

Taylor Otwell committed
285
		return '<img src="'.URL::to_asset($url).'"'.static::attributes($attributes).'>';
286 287 288
	}

	/**
289 290
	 * Generate an ordered list of items.
	 *
291 292 293 294
	 * @param  array   $list
	 * @param  array   $attributes
	 * @return string
	 */
295
	public static function ol($list, $attributes = array())
296
	{
297
		return static::listing('ol', $list, $attributes);
298 299 300
	}

	/**
301 302
	 * Generate an un-ordered list of items.
	 *
303 304 305 306
	 * @param  array   $list
	 * @param  array   $attributes
	 * @return string
	 */
307
	public static function ul($list, $attributes = array())
308
	{
309
		return static::listing('ul', $list, $attributes);
310 311 312 313 314 315 316 317 318 319
	}

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

324 325
		if (count($list) == 0) return $html;

326 327
		foreach ($list as $key => $value)
		{
328 329 330
			// If the value is an array, we will recurse the function so that we can
			// produce a nested list within the list being built. Of course, nested
			// lists may exist within nested lists, etc.
331 332
			if (is_array($value))
			{
333 334 335 336 337 338 339 340
				if (is_int($key))
				{
					$html .= static::listing($type, $value);
				}
				else
				{
					$html .= '<li>'.$key.static::listing($type, $value).'</li>';
				}
341 342 343 344 345
			}
			else
			{
				$html .= '<li>'.static::entities($value).'</li>';
			}
346 347
		}

348
		return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
349
	}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	
	/**
	 * Generate a definition list.
	 *
	 * @param  array   $list
	 * @param  array   $attributes
	 * @return string
	 */
	public static function dl($list, $attributes = array())
	{
		$html = '';

		if (count($list) == 0) return $html;
		
		foreach ($list as $term => $description)
		{
			$html .= '<dt>'.static::entities($term).'</dt>';
			$html .= '<dd>'.static::entities($description).'</dd>';
		}
		
370
		return '<dl'.static::attributes($attributes).'>'.$html.'</dl>';
371
	}
372 373

	/**
374 375
	 * Build a list of HTML attributes from an array.
	 *
376 377
	 * @param  array   $attributes
	 * @return string
378
	 */
379
	public static function attributes($attributes)
380 381 382
	{
		$html = array();

383
		foreach ((array) $attributes as $key => $value)
384
		{
385
			// For numeric keys, we will assume that the key and the value are the
Pascal Borreli committed
386
			// same, as this will convert HTML attributes such as "required" that
387
			// may be specified as required="required", etc.
388
			if (is_numeric($key)) $key = $value;
389

390
			if ( ! is_null($value))
391
			{
392
				$html[] = $key.'="'.static::entities($value).'"';
393 394 395
			}
		}

396
		return (count($html) > 0) ? ' '.implode(' ', $html) : '';
397 398 399 400 401 402 403 404
	}

	/**
	 * Obfuscate a string to prevent spam-bots from sniffing it.
	 *
	 * @param  string  $value
	 * @return string
	 */
405
	protected static function obfuscate($value)
406 407 408 409 410
	{
		$safe = '';

		foreach (str_split($value) as $letter)
		{
411 412
			// To properly obfuscate the value, we will randomly convert each
			// letter to its entity or hexadecimal representation, keeping a
413
			// bot from sniffing the randomly obfuscated letters.
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
			switch (rand(1, 3))
			{
				case 1:
					$safe .= '&#'.ord($letter).';';
					break;

				case 2:
					$safe .= '&#x'.dechex(ord($letter)).';';
					break;

				case 3:
					$safe .= $letter;
			}
		}

		return $safe;
	}

432 433 434 435 436 437 438 439 440 441 442 443 444
	/**
	 * Dynamically handle calls to custom macros.
	 *
	 * @param  string  $method
	 * @param  array   $parameters
	 * @return mixed
	 */
	public static function __callStatic($method, $parameters)
	{
	    if (isset(static::$macros[$method]))
	    {
	        return call_user_func_array(static::$macros[$method], $parameters);
	    }
445

446 447 448
	    throw new \Exception("Method [$method] does not exist.");
	}

Phill Sparks committed
449
}