helpers.php 8.4 KB

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