1: <?php
2:
3: /**
4: * Convert HTML characters to entities.
5: *
6: * The encoding specified in the application configuration file will be used.
7: *
8: * @param string $value
9: * @return string
10: */
11: function e($value)
12: {
13: return HTML::entities($value);
14: }
15:
16: /**
17: * Retrieve a language line.
18: *
19: * @param string $key
20: * @param array $replacements
21: * @param string $language
22: * @return string
23: */
24: function __($key, $replacements = array(), $language = null)
25: {
26: return Lang::line($key, $replacements, $language);
27: }
28:
29: /**
30: * Dump the given value and kill the script.
31: *
32: * @param mixed $value
33: * @return void
34: */
35: function dd($value)
36: {
37: echo "<pre>";
38: var_dump($value);
39: echo "</pre>";
40: die;
41: }
42:
43: /**
44: * Get an item from an array using "dot" notation.
45: *
46: * <code>
47: * // Get the $array['user']['name'] value from the array
48: * $name = array_get($array, 'user.name');
49: *
50: * // Return a default from if the specified item doesn't exist
51: * $name = array_get($array, 'user.name', 'Taylor');
52: * </code>
53: *
54: * @param array $array
55: * @param string $key
56: * @param mixed $default
57: * @return mixed
58: */
59: function array_get($array, $key, $default = null)
60: {
61: if (is_null($key)) return $array;
62:
63: // To retrieve the array item using dot syntax, we'll iterate through
64: // each segment in the key and look for that value. If it exists, we
65: // will return it, otherwise we will set the depth of the array and
66: // look for the next segment.
67: foreach (explode('.', $key) as $segment)
68: {
69: if ( ! is_array($array) or ! array_key_exists($segment, $array))
70: {
71: return value($default);
72: }
73:
74: $array = $array[$segment];
75: }
76:
77: return $array;
78: }
79:
80: /**
81: * Set an array item to a given value using "dot" notation.
82: *
83: * If no key is given to the method, the entire array will be replaced.
84: *
85: * <code>
86: * // Set the $array['user']['name'] value on the array
87: * array_set($array, 'user.name', 'Taylor');
88: *
89: * // Set the $array['user']['name']['first'] value on the array
90: * array_set($array, 'user.name.first', 'Michael');
91: * </code>
92: *
93: * @param array $array
94: * @param string $key
95: * @param mixed $value
96: * @return void
97: */
98: function array_set(&$array, $key, $value)
99: {
100: if (is_null($key)) return $array = $value;
101:
102: $keys = explode('.', $key);
103:
104: // This loop allows us to dig down into the array to a dynamic depth by
105: // setting the array value for each level that we dig into. Once there
106: // is one key left, we can fall out of the loop and set the value as
107: // we should be at the proper depth.
108: while (count($keys) > 1)
109: {
110: $key = array_shift($keys);
111:
112: // If the key doesn't exist at this depth, we will just create an
113: // empty array to hold the next value, allowing us to create the
114: // arrays to hold the final value.
115: if ( ! isset($array[$key]) or ! is_array($array[$key]))
116: {
117: $array[$key] = array();
118: }
119:
120: $array =& $array[$key];
121: }
122:
123: $array[array_shift($keys)] = $value;
124: }
125:
126: /**
127: * Remove an array item from a given array using "dot" notation.
128: *
129: * <code>
130: * // Remove the $array['user']['name'] item from the array
131: * array_forget($array, 'user.name');
132: *
133: * // Remove the $array['user']['name']['first'] item from the array
134: * array_forget($array, 'user.name.first');
135: * </code>
136: *
137: * @param array $array
138: * @param string $key
139: * @return void
140: */
141: function array_forget(&$array, $key)
142: {
143: $keys = explode('.', $key);
144:
145: // This loop functions very similarly to the loop in the "set" method.
146: // We will iterate over the keys, setting the array value to the new
147: // depth at each iteration. Once there is only one key left, we will
148: // be at the proper depth in the array.
149: while (count($keys) > 1)
150: {
151: $key = array_shift($keys);
152:
153: // Since this method is supposed to remove a value from the array,
154: // if a value higher up in the chain doesn't exist, there is no
155: // need to keep digging into the array, since it is impossible
156: // for the final value to even exist.
157: if ( ! isset($array[$key]) or ! is_array($array[$key]))
158: {
159: return;
160: }
161:
162: $array =& $array[$key];
163: }
164:
165: unset($array[array_shift($keys)]);
166: }
167:
168: /**
169: * Return the first element in an array which passes a given truth test.
170: *
171: * <code>
172: * // Return the first array element that equals "Taylor"
173: * $value = array_first($array, function($k, $v) {return $v == 'Taylor';});
174: *
175: * // Return a default value if no matching element is found
176: * $value = array_first($array, function($k, $v) {return $v == 'Taylor'}, 'Default');
177: * </code>
178: *
179: * @param array $array
180: * @param Closure $callback
181: * @param mixed $default
182: * @return mixed
183: */
184: function array_first($array, $callback, $default = null)
185: {
186: foreach ($array as $key => $value)
187: {
188: if (call_user_func($callback, $key, $value)) return $value;
189: }
190:
191: return value($default);
192: }
193:
194: /**
195: * Recursively remove slashes from array keys and values.
196: *
197: * @param array $array
198: * @return array
199: */
200: function array_strip_slashes($array)
201: {
202: $result = array();
203:
204: foreach($array as $key => $value)
205: {
206: $key = stripslashes($key);
207:
208: // If the value is an array, we will just recurse back into the
209: // function to keep stripping the slashes out of the array,
210: // otherwise we will set the stripped value.
211: if (is_array($value))
212: {
213: $result[$key] = array_strip_slashes($value);
214: }
215: else
216: {
217: $result[$key] = stripslashes($value);
218: }
219: }
220:
221: return $result;
222: }
223:
224: /**
225: * Divide an array into two arrays. One with keys and the other with values.
226: *
227: * @param array $array
228: * @return array
229: */
230: function array_divide($array)
231: {
232: return array(array_keys($array), array_values($array));
233: }
234:
235: /**
236: * Pluck an array of values from an array.
237: *
238: * @param array $array
239: * @param string $key
240: * @return array
241: */
242: function array_pluck($array, $key)
243: {
244: return array_map(function($v) use ($key)
245: {
246: return is_object($v) ? $v->$key : $v[$key];
247:
248: }, $array);
249: }
250:
251: /**
252: * Get a subset of the items from the given array.
253: *
254: * @param array $array
255: * @param array $keys
256: * @return array
257: */
258: function array_only($array, $keys)
259: {
260: return array_intersect_key( $array, array_flip((array) $keys) );
261: }
262:
263: /**
264: * Get all of the given array except for a specified array of items.
265: *
266: * @param array $array
267: * @param array $keys
268: * @return array
269: */
270: function array_except($array, $keys)
271: {
272: return array_diff_key( $array, array_flip((array) $keys) );
273: }
274:
275: /**
276: * Transform Eloquent models to a JSON object.
277: *
278: * @param Eloquent|array $models
279: * @return object
280: */
281: function eloquent_to_json($models)
282: {
283: if ($models instanceof Laravel\Database\Eloquent\Model)
284: {
285: return json_encode($models->to_array());
286: }
287:
288: return json_encode(array_map(function($m) { return $m->to_array(); }, $models));
289: }
290:
291: /**
292: * Determine if "Magic Quotes" are enabled on the server.
293: *
294: * @return bool
295: */
296: function magic_quotes()
297: {
298: return function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc();
299: }
300:
301: /**
302: * Return the first element of an array.
303: *
304: * This is simply a convenient wrapper around the "reset" method.
305: *
306: * @param array $array
307: * @return mixed
308: */
309: function head($array)
310: {
311: return reset($array);
312: }
313:
314: /**
315: * Generate an application URL.
316: *
317: * <code>
318: * // Create a URL to a location within the application
319: * $url = url('user/profile');
320: *
321: * // Create a HTTPS URL to a location within the application
322: * $url = url('user/profile', true);
323: * </code>
324: *
325: * @param string $url
326: * @param bool $https
327: * @return string
328: */
329: function url($url = '', $https = null)
330: {
331: return URL::to($url, $https);
332: }
333:
334: /**
335: * Generate an application URL to an asset.
336: *
337: * @param string $url
338: * @param bool $https
339: * @return string
340: */
341: function asset($url, $https = null)
342: {
343: return URL::to_asset($url, $https);
344: }
345:
346: /**
347: * Generate a URL to a controller action.
348: *
349: * <code>
350: * // Generate a URL to the "index" method of the "user" controller
351: * $url = action('user@index');
352: *
353: * // Generate a URL to http://example.com/user/profile/taylor
354: * $url = action('user@profile', array('taylor'));
355: * </code>
356: *
357: * @param string $action
358: * @param array $parameters
359: * @return string
360: */
361: function action($action, $parameters = array())
362: {
363: return URL::to_action($action, $parameters);
364: }
365:
366: /**
367: * Generate a URL from a route name.
368: *
369: * <code>
370: * // Create a URL to the "profile" named route
371: * $url = route('profile');
372: *
373: * // Create a URL to the "profile" named route with wildcard parameters
374: * $url = route('profile', array($username));
375: * </code>
376: *
377: * @param string $name
378: * @param array $parameters
379: * @return string
380: */
381: function route($name, $parameters = array())
382: {
383: return URL::to_route($name, $parameters);
384: }
385:
386: /**
387: * Determine if a given string begins with a given value.
388: *
389: * @param string $haystack
390: * @param string $needle
391: * @return bool
392: */
393: function starts_with($haystack, $needle)
394: {
395: return strpos($haystack, $needle) === 0;
396: }
397:
398: /**
399: * Determine if a given string ends with a given value.
400: *
401: * @param string $haystack
402: * @param string $needle
403: * @return bool
404: */
405: function ends_with($haystack, $needle)
406: {
407: return $needle == substr($haystack, strlen($haystack) - strlen($needle));
408: }
409:
410: /**
411: * Determine if a given string contains a given sub-string.
412: *
413: * @param string $haystack
414: * @param string|array $needle
415: * @return bool
416: */
417: function str_contains($haystack, $needle)
418: {
419: foreach ((array) $needle as $n)
420: {
421: if (strpos($haystack, $n) !== false) return true;
422: }
423:
424: return false;
425: }
426:
427: /**
428: * Cap a string with a single instance of the given string.
429: *
430: * @param string $value
431: * @param string $cap
432: * @return string
433: */
434: function str_finish($value, $cap)
435: {
436: return rtrim($value, $cap).$cap;
437: }
438:
439: /**
440: * Determine if the given object has a toString method.
441: *
442: * @param object $value
443: * @return bool
444: */
445: function str_object($value)
446: {
447: return is_object($value) and method_exists($value, '__toString');
448: }
449:
450: /**
451: * Get the root namespace of a given class.
452: *
453: * @param string $class
454: * @param string $separator
455: * @return string
456: */
457: function root_namespace($class, $separator = '\\')
458: {
459: if (str_contains($class, $separator))
460: {
461: return head(explode($separator, $class));
462: }
463: }
464:
465: /**
466: * Get the "class basename" of a class or object.
467: *
468: * The basename is considered to be the name of the class minus all namespaces.
469: *
470: * @param object|string $class
471: * @return string
472: */
473: function class_basename($class)
474: {
475: if (is_object($class)) $class = get_class($class);
476:
477: return basename(str_replace('\\', '/', $class));
478: }
479:
480: /**
481: * Return the value of the given item.
482: *
483: * If the given item is a Closure the result of the Closure will be returned.
484: *
485: * @param mixed $value
486: * @return mixed
487: */
488: function value($value)
489: {
490: return (is_callable($value) and ! is_string($value)) ? call_user_func($value) : $value;
491: }
492:
493: /**
494: * Short-cut for constructor method chaining.
495: *
496: * @param mixed $object
497: * @return mixed
498: */
499: function with($object)
500: {
501: return $object;
502: }
503:
504: /**
505: * Determine if the current version of PHP is at least the supplied version.
506: *
507: * @param string $version
508: * @return bool
509: */
510: function has_php($version)
511: {
512: return version_compare(PHP_VERSION, $version) >= 0;
513: }
514:
515: /**
516: * Get a view instance.
517: *
518: * @param string $view
519: * @param array $data
520: * @return View
521: */
522: function view($view, $data = array())
523: {
524: if (is_null($view)) return '';
525:
526: return View::make($view, $data);
527: }
528:
529: /**
530: * Render the given view.
531: *
532: * @param string $view
533: * @param array $data
534: * @return string
535: */
536: function render($view, $data = array())
537: {
538: if (is_null($view)) return '';
539:
540: return View::make($view, $data)->render();
541: }
542:
543: /**
544: * Get the rendered contents of a partial from a loop.
545: *
546: * @param string $partial
547: * @param array $data
548: * @param string $iterator
549: * @param string $empty
550: * @return string
551: */
552: function render_each($partial, array $data, $iterator, $empty = 'raw|')
553: {
554: return View::render_each($partial, $data, $iterator, $empty);
555: }
556:
557: /**
558: * Get the string contents of a section.
559: *
560: * @param string $section
561: * @return string
562: */
563: function yield($section)
564: {
565: return Section::yield($section);
566: }
567:
568: /**
569: * Get a CLI option from the argv $_SERVER variable.
570: *
571: * @param string $option
572: * @param mixed $default
573: * @return string
574: */
575: function get_cli_option($option, $default = null)
576: {
577: foreach (Laravel\Request::foundation()->server->get('argv') as $argument)
578: {
579: if (starts_with($argument, "--{$option}="))
580: {
581: return substr($argument, strlen($option) + 3);
582: }
583: }
584:
585: return value($default);
586: }
587:
588: /**
589: * Calculate the human-readable file size (with proper units).
590: *
591: * @param int $size
592: * @return string
593: */
594: function get_file_size($size)
595: {
596: $units = array('Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB');
597: return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2).' '.$units[$i];
598: }