helpers.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * Convert HTML characters to entities.
  4. *
  5. * The encoding specified in the application configuration file will be used.
  6. *
  7. * @param string $value
  8. * @return string
  9. */
  10. function e($value)
  11. {
  12. return Laravel\HTML::entities($value);
  13. }
  14. /**
  15. * Retrieve a language line.
  16. *
  17. * @param string $key
  18. * @param array $replacements
  19. * @param string $language
  20. * @return string
  21. */
  22. function __($key, $replacements = array(), $language = null)
  23. {
  24. return Laravel\Lang::line($key, $replacements, $language);
  25. }
  26. /**
  27. * Get an item from an array using "dot" notation.
  28. *
  29. * <code>
  30. * // Get the $array['user']['name'] value from the array
  31. * $name = array_get($array, 'user.name');
  32. *
  33. * // Return a default from if the specified item doesn't exist
  34. * $name = array_get($array, 'user.name', 'Taylor');
  35. * </code>
  36. *
  37. * @param array $array
  38. * @param string $key
  39. * @param mixed $default
  40. * @return mixed
  41. */
  42. function array_get($array, $key, $default = null)
  43. {
  44. if (is_null($key)) return $array;
  45. foreach (explode('.', $key) as $segment)
  46. {
  47. if ( ! is_array($array) or ! array_key_exists($segment, $array))
  48. {
  49. return value($default);
  50. }
  51. $array = $array[$segment];
  52. }
  53. return $array;
  54. }
  55. /**
  56. * Set an array item to a given value using "dot" notation.
  57. *
  58. * If no key is given to the method, the entire array will be replaced.
  59. *
  60. * <code>
  61. * // Set the $array['user']['name'] value on the array
  62. * array_set($array, 'user.name', 'Taylor');
  63. *
  64. * // Set the $array['user']['name']['first'] value on the array
  65. * array_set($array, 'user.name.first', 'Michael');
  66. * </code>
  67. *
  68. * @param array $array
  69. * @param string $key
  70. * @param mixed $value
  71. * @return void
  72. */
  73. function array_set(&$array, $key, $value)
  74. {
  75. if (is_null($key)) return $array = $value;
  76. $keys = explode('.', $key);
  77. // This loop allows us to dig down into the array to a dynamic depth by
  78. // setting the array value for each level that we dig into. Once there
  79. // is one key left, we can fall out of the loop and set the value as
  80. // we should be at the proper depth within the array.
  81. while (count($keys) > 1)
  82. {
  83. $key = array_shift($keys);
  84. // If the key doesn't exist at this depth, we will just create an
  85. // empty array to hold the next value, allowing us to create the
  86. // arrays necessary to hold the final value at the proper depth.
  87. if ( ! isset($array[$key]) or ! is_array($array[$key]))
  88. {
  89. $array[$key] = array();
  90. }
  91. $array =& $array[$key];
  92. }
  93. $array[array_shift($keys)] = $value;
  94. }
  95. /**
  96. * Remove an array item from a given array using "dot" notation.
  97. *
  98. * <code>
  99. * // Remove the $array['user']['name'] item from the array
  100. * array_forget($array, 'user.name');
  101. *
  102. * // Remove the $array['user']['name']['first'] item from the array
  103. * array_forget($array, 'user.name.first');
  104. * </code>
  105. *
  106. * @param array $array
  107. * @param string $key
  108. * @return void
  109. */
  110. function array_forget(&$array, $key)
  111. {
  112. $keys = explode('.', $key);
  113. // This loop functions very similarly to the loop in the "set" method.
  114. // We will iterate over the keys, setting the array value to the new
  115. // depth at each iteration. Once there is only one key left, we will
  116. // be at the proper depth in the array to "forget" the value.
  117. while (count($keys) > 1)
  118. {
  119. $key = array_shift($keys);
  120. // Since this method is supposed to remove a value from the array,
  121. // if a value higher up in the chain doesn't exist, there is no
  122. // need to keep digging into the array, since it is impossible
  123. // for the final value to even exist in the array.
  124. if ( ! isset($array[$key]) or ! is_array($array[$key]))
  125. {
  126. return;
  127. }
  128. $array =& $array[$key];
  129. }
  130. unset($array[array_shift($keys)]);
  131. }
  132. /**
  133. * Return the first element in an array which passes a given truth test.
  134. *
  135. * <code>
  136. * // Return the first array element that equals "Taylor"
  137. * $value = array_first($array, function($k, $v) {return $v == 'Taylor';});
  138. *
  139. * // Return a default value if no matching element is found
  140. * $value = array_first($array, function($k, $v) {return $v == 'Taylor'}, 'Default');
  141. * </code>
  142. *
  143. * @param array $array
  144. * @param Closure $callback
  145. * @param mixed $default
  146. * @return mixed
  147. */
  148. function array_first($array, $callback, $default = null)
  149. {
  150. foreach ($array as $key => $value)
  151. {
  152. if (call_user_func($callback, $key, $value)) return $value;
  153. }
  154. return value($default);
  155. }
  156. /**
  157. * Spin through the array, executing a callback with each key and element.
  158. *
  159. * @param array $array
  160. * @param mixed $callback
  161. * @return array
  162. */
  163. function array_spin($array, $callback)
  164. {
  165. return array_map($callback, array_keys($array), array_values($array));
  166. }
  167. /**
  168. * Return the first element of an array.
  169. *
  170. * This is simply a convenient wrapper around the "reset" method.
  171. *
  172. * @param array $array
  173. * @return mixed
  174. */
  175. function head($array)
  176. {
  177. return reset($array);
  178. }
  179. /**
  180. * Generate an application URL.
  181. *
  182. * <code>
  183. * // Create a URL to a location within the application
  184. * $url = path('user/profile');
  185. *
  186. * // Create a HTTPS URL to a location within the application
  187. * $url = path('user/profile', true);
  188. * </code>
  189. *
  190. * @param string $url
  191. * @param bool $https
  192. * @return string
  193. */
  194. function path($url = '', $https = false)
  195. {
  196. return Laravel\URL::to($url, $https);
  197. }
  198. /**
  199. * Generate an application URL to an asset.
  200. *
  201. * @param string $url
  202. * @param bool $https
  203. * @return string
  204. */
  205. function asset($url, $https = false)
  206. {
  207. return Laravel\URL::to_asset($url, $https);
  208. }
  209. /**
  210. * Generate a URL to a controller action.
  211. *
  212. * <code>
  213. * // Generate a URL to the "index" method of the "user" controller
  214. * $url = action('user@index');
  215. *
  216. * // Generate a URL to http://example.com/user/profile/taylor
  217. * $url = action('user@profile', array('taylor'));
  218. * </code>
  219. *
  220. * @param string $action
  221. * @param array $parameters
  222. * @param bool $https
  223. * @return string
  224. */
  225. function action($action, $parameters = array(), $https = false)
  226. {
  227. return Laravel\URL::to_action($action, $parameters, $https);
  228. }
  229. /**
  230. * Generate a URL from a route name.
  231. *
  232. * <code>
  233. * // Create a URL to the "profile" named route
  234. * $url = route('profile');
  235. *
  236. * // Create a URL to the "profile" named route with wildcard parameters
  237. * $url = route('profile', array($username));
  238. * </code>
  239. *
  240. * @param string $name
  241. * @param array $parameters
  242. * @param bool $https
  243. * @return string
  244. */
  245. function route($name, $parameters = array(), $https = false)
  246. {
  247. return Laravel\URL::to_route($name, $parameters, $https);
  248. }
  249. /**
  250. * Determine if a given string begins with a given value.
  251. *
  252. * @param string $haystack
  253. * @param string $needle
  254. * @return bool
  255. */
  256. function starts_with($haystack, $needle)
  257. {
  258. return strpos($haystack, $needle) === 0;
  259. }
  260. /**
  261. * Determine if a given string contains a given sub-string.
  262. *
  263. * @param string $haystack
  264. * @param string $needle
  265. * @return bool
  266. */
  267. function str_contains($haystack, $needle)
  268. {
  269. return strpos($haystack, $needle) !== false;
  270. }
  271. /**
  272. * Return the value of the given item.
  273. *
  274. * If the given item is a Closure the result of the Closure will be returned.
  275. *
  276. * @param mixed $value
  277. * @return mixed
  278. */
  279. function value($value)
  280. {
  281. return ($value instanceof Closure) ? call_user_func($value) : $value;
  282. }