Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 335 lines (293 sloc) 8.152 kB
1e90e42 @taylorotwell first commit of 2.0
taylorotwell authored
1 <?php namespace Laravel;
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
2
3 class HTML {
4
5 /**
c05ccc5 @taylorotwell moved entities method to html class and added encoding configuration …
taylorotwell authored
6 * Convert HTML characters to entities.
7 *
df84e36 @taylorotwell refactoring.
taylorotwell authored
8 * The encoding specified in the application configuration file will be used.
9 *
c05ccc5 @taylorotwell moved entities method to html class and added encoding configuration …
taylorotwell authored
10 * @param string $value
11 * @return string
12 */
13 public static function entities($value)
14 {
b90c6dd @taylorotwell Fixing tabbing on HTML::entities method.
taylorotwell authored
15 return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
c05ccc5 @taylorotwell moved entities method to html class and added encoding configuration …
taylorotwell authored
16 }
17
18 /**
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
19 * Generate a JavaScript reference.
20 *
21 * @param string $url
dbdd45d @taylorotwell tweaked cache and session namespacing.
taylorotwell authored
22 * @param array $attributes
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
23 * @return string
24 */
dbdd45d @taylorotwell tweaked cache and session namespacing.
taylorotwell authored
25 public static function script($url, $attributes = array())
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
26 {
1e7850d @taylorotwell refactoring for dependency injection and testability.
taylorotwell authored
27 $url = static::entities(URL::to_asset($url));
82045e2 @taylorotwell more refactoring for dependency injection.
taylorotwell authored
28
1e7850d @taylorotwell refactoring for dependency injection and testability.
taylorotwell authored
29 return '<script type="text/javascript" src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
30 }
31
32 /**
33 * Generate a CSS reference.
34 *
35 * @param string $url
dbdd45d @taylorotwell tweaked cache and session namespacing.
taylorotwell authored
36 * @param array $attributes
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
37 * @return string
38 */
dbdd45d @taylorotwell tweaked cache and session namespacing.
taylorotwell authored
39 public static function style($url, $attributes = array())
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
40 {
5862faf @taylorotwell Refactoring the html class.
taylorotwell authored
41 if ( ! array_key_exists('media', $attributes)) $attributes['media'] = 'all';
42
df84e36 @taylorotwell refactoring.
taylorotwell authored
43 $attributes = array_merge($attributes, array('rel' => 'stylesheet', 'type' => 'text/css'));
dbdd45d @taylorotwell tweaked cache and session namespacing.
taylorotwell authored
44
1e7850d @taylorotwell refactoring for dependency injection and testability.
taylorotwell authored
45 return '<link href="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>'.PHP_EOL;
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
46 }
47
48 /**
7ca5a23 @taylorotwell Added HTML::span method.
taylorotwell authored
49 * Generate a HTML span.
50 *
51 * @param string $value
52 * @param array $attributes
53 * @return string
54 */
55 public static function span($value, $attributes = array())
56 {
57 return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
58 }
59
60 /**
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
61 * Generate a HTML link.
62 *
63 * @param string $url
64 * @param string $title
65 * @param array $attributes
66 * @param bool $https
bf44ce8 @taylorotwell added asset links and improved html class.
taylorotwell authored
67 * @param bool $asset
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
68 * @return string
69 */
bf44ce8 @taylorotwell added asset links and improved html class.
taylorotwell authored
70 public static function link($url, $title, $attributes = array(), $https = false, $asset = false)
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
71 {
1e7850d @taylorotwell refactoring for dependency injection and testability.
taylorotwell authored
72 $url = static::entities(URL::to($url, $https, $asset));
82045e2 @taylorotwell more refactoring for dependency injection.
taylorotwell authored
73
1e7850d @taylorotwell refactoring for dependency injection and testability.
taylorotwell authored
74 return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
75 }
76
77 /**
78 * Generate a HTTPS HTML link.
79 *
80 * @param string $url
81 * @param string $title
82 * @param array $attributes
83 * @return string
84 */
4669911 @taylorotwell fixed bug in html link methods.
taylorotwell authored
85 public static function link_to_secure($url, $title, $attributes = array())
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
86 {
87 return static::link($url, $title, $attributes, true);
88 }
89
90 /**
bf44ce8 @taylorotwell added asset links and improved html class.
taylorotwell authored
91 * Generate an HTML link to an asset.
92 *
93 * @param string $url
94 * @param string $title
95 * @param array $attributes
96 * @return string
97 */
64351b2 @taylorotwell Added HTML::link_to_secure_asset method.
taylorotwell authored
98 public static function link_to_asset($url, $title, $attributes = array(), $https = false)
bf44ce8 @taylorotwell added asset links and improved html class.
taylorotwell authored
99 {
64351b2 @taylorotwell Added HTML::link_to_secure_asset method.
taylorotwell authored
100 return static::link($url, $title, $attributes, $https, true);
101 }
102
103 /**
104 * Generate an HTTPS HTML link to an asset.
105 *
106 * @param string $url
107 * @param string $title
108 * @param array $attributes
109 * @return string
110 */
111 public static function link_to_secure_asset($url, $title, $attributes = array())
112 {
113 return static::link_to_asset($url, $title, $attributes, true);
bf44ce8 @taylorotwell added asset links and improved html class.
taylorotwell authored
114 }
115
116 /**
6af79d8 @taylorotwell Added ability to dynamically create links to named routes using the H…
taylorotwell authored
117 * Generate an HTML link to a route.
118 *
df84e36 @taylorotwell refactoring.
taylorotwell authored
119 * An array of parameters may be specified to fill in URI segment wildcards.
120 *
121 * <code>
122 * // Link to the "login" route
123 * echo HTML::link_to_route('login', 'Login');
124 *
125 * // Link to the "profile" route, which has a URI of "/profile/(:any)"
126 * echo HTML::link_to_route('profile', 'Profile', array('taylor'));
127 * </code>
128 *
6af79d8 @taylorotwell Added ability to dynamically create links to named routes using the H…
taylorotwell authored
129 * @param string $name
130 * @param string $title
131 * @param array $parameters
132 * @param array $attributes
133 * @return string
134 */
135 public static function link_to_route($name, $title, $parameters = array(), $attributes = array(), $https = false)
136 {
1e7850d @taylorotwell refactoring for dependency injection and testability.
taylorotwell authored
137 return static::link(URL::to_route($name, $parameters, $https), $title, $attributes);
6af79d8 @taylorotwell Added ability to dynamically create links to named routes using the H…
taylorotwell authored
138 }
139
140 /**
141 * Generate an HTTPS HTML link to a route.
142 *
143 * @param string $name
144 * @param string $title
145 * @param array $parameters
146 * @param array $attributes
147 * @return string
148 */
149 public static function link_to_secure_route($name, $title, $parameters = array(), $attributes = array())
150 {
151 return static::link_to_route($name, $title, $parameters, $attributes, true);
152 }
153
154 /**
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
155 * Generate an HTML mailto link.
156 *
df84e36 @taylorotwell refactoring.
taylorotwell authored
157 * The E-Mail address will be obfuscated to protect it from spam bots.
158 *
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
159 * @param string $email
160 * @param string $title
161 * @param array $attributes
162 * @return string
163 */
164 public static function mailto($email, $title = null, $attributes = array())
165 {
166 $email = static::email($email);
167
5862faf @taylorotwell Refactoring the html class.
taylorotwell authored
168 if (is_null($title)) $title = $email;
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
169
1e7850d @taylorotwell refactoring for dependency injection and testability.
taylorotwell authored
170 $email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;
171
172 return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
173 }
174
175 /**
176 * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
177 *
178 * @param string $email
179 * @return string
180 */
181 public static function email($email)
182 {
183 return str_replace('@', '&#64;', static::obfuscate($email));
184 }
185
186 /**
187 * Generate an HTML image.
188 *
189 * @param string $url
190 * @param string $alt
191 * @param array $attributes
192 * @return string
193 */
194 public static function image($url, $alt = '', $attributes = array())
195 {
c05ccc5 @taylorotwell moved entities method to html class and added encoding configuration …
taylorotwell authored
196 $attributes['alt'] = static::entities($alt);
64704cf @taylorotwell Added comments to HTML class.
taylorotwell authored
197
1e7850d @taylorotwell refactoring for dependency injection and testability.
taylorotwell authored
198 return '<img src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>';
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
199 }
200
201 /**
202 * Generate an ordered list.
203 *
204 * @param array $list
205 * @param array $attributes
206 * @return string
207 */
208 public static function ol($list, $attributes = array())
209 {
210 return static::list_elements('ol', $list, $attributes);
211 }
212
213 /**
214 * Generate an un-ordered list.
215 *
216 * @param array $list
217 * @param array $attributes
218 * @return string
219 */
220 public static function ul($list, $attributes = array())
221 {
222 return static::list_elements('ul', $list, $attributes);
223 }
224
225 /**
226 * Generate an ordered or un-ordered list.
227 *
228 * @param string $type
229 * @param array $list
230 * @param array $attributes
231 * @return string
232 */
e0a8d17 @taylorotwell Add support for nested ul and ol lists.
taylorotwell authored
233 private static function list_elements($type, $list, $attributes = array())
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
234 {
235 $html = '';
236
237 foreach ($list as $key => $value)
238 {
e0a8d17 @taylorotwell Add support for nested ul and ol lists.
taylorotwell authored
239 $html .= (is_array($value)) ? static::list_elements($type, $value) : '<li>'.static::entities($value).'</li>';
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
240 }
241
f7bb0c5 @taylorotwell trimmed comment bloat. returning boolean on eloquent save.
taylorotwell authored
242 return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
243 }
244
245 /**
246 * Build a list of HTML attributes.
247 *
248 * @param array $attributes
249 * @return string
250 */
251 public static function attributes($attributes)
252 {
253 $html = array();
254
255 foreach ($attributes as $key => $value)
256 {
2a1c01f @taylorotwell Cleaning up HTML class bloat.
taylorotwell authored
257 // Assume numeric-keyed attributes to have the same key and value.
258 // Example: required="required", autofocus="autofocus", etc.
5862faf @taylorotwell Refactoring the html class.
taylorotwell authored
259 if (is_numeric($key)) $key = $value;
cd33e81 @taylorotwell added several of the new html5 form elements. added support for attri…
taylorotwell authored
260
f7bb0c5 @taylorotwell trimmed comment bloat. returning boolean on eloquent save.
taylorotwell authored
261 if ( ! is_null($value))
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
262 {
c05ccc5 @taylorotwell moved entities method to html class and added encoding configuration …
taylorotwell authored
263 $html[] = $key.'="'.static::entities($value).'"';
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
264 }
265 }
266
bf44ce8 @taylorotwell added asset links and improved html class.
taylorotwell authored
267 return (count($html) > 0) ? ' '.implode(' ', $html) : '';
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
268 }
269
270 /**
271 * Obfuscate a string to prevent spam-bots from sniffing it.
272 *
273 * @param string $value
274 * @return string
275 */
276 public static function obfuscate($value)
277 {
278 $safe = '';
279
280 foreach (str_split($value) as $letter)
281 {
282 switch (rand(1, 3))
283 {
284 // Convert the letter to its entity representation.
285 case 1:
286 $safe .= '&#'.ord($letter).';';
287 break;
288
289 // Convert the letter to a Hex character code.
290 case 2:
291 $safe .= '&#x'.dechex(ord($letter)).';';
292 break;
293
294 // No encoding.
295 case 3:
296 $safe .= $letter;
297 }
298 }
299
300 return $safe;
301 }
302
6af79d8 @taylorotwell Added ability to dynamically create links to named routes using the H…
taylorotwell authored
303 /**
304 * Magic Method for handling dynamic static methods.
df84e36 @taylorotwell refactoring.
taylorotwell authored
305 *
306 * This method primarily handles dynamic calls to create links to named routes.
307 *
308 * <code>
309 * // Link to the "login" route
310 * echo HTML::link_to_login('Login');
311 *
312 * // Link to the "profile" route, which has a URI of "/profile/(:any)"
313 * echo HTML::link_to_profile('Profile', array('taylor'));
314 * </code>
6af79d8 @taylorotwell Added ability to dynamically create links to named routes using the H…
taylorotwell authored
315 */
316 public static function __callStatic($method, $parameters)
317 {
318 if (strpos($method, 'link_to_secure_') === 0)
319 {
320 array_unshift($parameters, substr($method, 15));
5862faf @taylorotwell Refactoring the html class.
taylorotwell authored
321
6af79d8 @taylorotwell Added ability to dynamically create links to named routes using the H…
taylorotwell authored
322 return forward_static_call_array('HTML::link_to_secure_route', $parameters);
323 }
324
325 if (strpos($method, 'link_to_') === 0)
326 {
327 array_unshift($parameters, substr($method, 8));
5862faf @taylorotwell Refactoring the html class.
taylorotwell authored
328
6af79d8 @taylorotwell Added ability to dynamically create links to named routes using the H…
taylorotwell authored
329 return forward_static_call_array('HTML::link_to_route', $parameters);
330 }
cd33e81 @taylorotwell added several of the new html5 form elements. added support for attri…
taylorotwell authored
331
332 throw new \Exception("Static method [$method] is not defined on the HTML class.");
6af79d8 @taylorotwell Added ability to dynamically create links to named routes using the H…
taylorotwell authored
333 }
334
a188d62 @taylorotwell initial commit of laravel!
taylorotwell authored
335 }
Something went wrong with that request. Please try again.