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

class HTML {

	/**
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
	 * The registered custom macros.
	 *
	 * @var array
	 */
	public static $macros = array();

    /**
     * Registers a custom macro.
     *
     * @param  string   $name
     * @param  Closure  $input
     * @return void
     */
	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, $attributes = array(), $https = null)
141
	{
Taylor Otwell committed
142
		$url = URL::to($url, $https);
143

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

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

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

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

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

	/**
192 193
	 * Generate an HTML link to a route.
	 *
Taylor Otwell committed
194 195
	 * An array of parameters may be specified to fill in URI segment wildcards.
	 *
196 197 198 199 200 201 202 203
	 * <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>
	 *
204 205 206 207 208 209
	 * @param  string  $name
	 * @param  string  $title
	 * @param  array   $parameters
	 * @param  array   $attributes
	 * @return string
	 */
210
	public static function link_to_route($name, $title, $parameters = array(), $attributes = array())
211
	{
212
		return static::link(URL::to_route($name, $parameters), $title, $attributes);
213 214 215
	}

	/**
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	 * 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
	 */
	public static function link_to_action($action, $title, $parameters = array(), $attributes = array())
	{
		return static::link(URL::to_action($action, $parameters), $title, $attributes);
	}

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

253
		if (is_null($title)) $title = $email;
254

255 256
		$email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;

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

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

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

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

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

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

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

322 323
		if (count($list) == 0) return $html;

324 325
		foreach ($list as $key => $value)
		{
326 327 328
			// 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.
329 330
			if (is_array($value))
			{
331 332 333 334 335 336 337 338
				if (is_int($key))
				{
					$html .= static::listing($type, $value);
				}
				else
				{
					$html .= '<li>'.$key.static::listing($type, $value).'</li>';
				}
339 340 341 342 343
			}
			else
			{
				$html .= '<li>'.static::entities($value).'</li>';
			}
344 345
		}

346
		return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
347 348 349
	}

	/**
350 351
	 * Build a list of HTML attributes from an array.
	 *
352 353
	 * @param  array   $attributes
	 * @return string
354
	 */
355
	public static function attributes($attributes)
356 357 358
	{
		$html = array();

359
		foreach ((array) $attributes as $key => $value)
360
		{
361 362
			// For numeric keys, we will assume that the key and the value are the
			// same, as this will conver HTML attributes such as "required" that
363
			// may be specified as required="required", etc.
364
			if (is_numeric($key)) $key = $value;
365

366
			if ( ! is_null($value))
367
			{
368
				$html[] = $key.'="'.static::entities($value).'"';
369 370 371
			}
		}

372
		return (count($html) > 0) ? ' '.implode(' ', $html) : '';
373 374 375 376 377 378 379 380
	}

	/**
	 * Obfuscate a string to prevent spam-bots from sniffing it.
	 *
	 * @param  string  $value
	 * @return string
	 */
381
	protected static function obfuscate($value)
382 383 384 385 386
	{
		$safe = '';

		foreach (str_split($value) as $letter)
		{
387 388
			// To properly obfuscate the value, we will randomly convert each
			// letter to its entity or hexadecimal representation, keeping a
389
			// bot from sniffing the randomly obfuscated letters.
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
			switch (rand(1, 3))
			{
				case 1:
					$safe .= '&#'.ord($letter).';';
					break;

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

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

		return $safe;
	}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	/**
	 * 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);
	    }
	    
	    throw new \Exception("Method [$method] does not exist.");
	}

Phill Sparks committed
425
}