helpers.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. // To retrieve the array item using dot syntax, we'll iterate through
  46. // each segment in the key and look for that value. If it exists, we
  47. // will return it, otherwise we will set the depth of the array and
  48. // look for the next segment.
  49. foreach (explode('.', $key) as $segment)
  50. {
  51. if ( ! is_array($array) or ! array_key_exists($segment, $array))
  52. {
  53. return value($default);
  54. }
  55. $array = $array[$segment];
  56. }
  57. return $array;
  58. }
  59. /**
  60. * Set an array item to a given value using "dot" notation.
  61. *
  62. * If no key is given to the method, the entire array will be replaced.
  63. *
  64. * <code>
  65. * // Set the $array['user']['name'] value on the array
  66. * array_set($array, 'user.name', 'Taylor');
  67. *
  68. * // Set the $array['user']['name']['first'] value on the array
  69. * array_set($array, 'user.name.first', 'Michael');
  70. * </code>
  71. *
  72. * @param array $array
  73. * @param string $key
  74. * @param mixed $value
  75. * @return void
  76. */
  77. function array_set(&$array, $key, $value)
  78. {
  79. if (is_null($key)) return $array = $value;
  80. $keys = explode('.', $key);
  81. // This loop allows us to dig down into the array to a dynamic depth by
  82. // setting the array value for each level that we dig into. Once there
  83. // is one key left, we can fall out of the loop and set the value as
  84. // we should be at the proper depth.
  85. while (count($keys) > 1)
  86. {
  87. $key = array_shift($keys);
  88. // If the key doesn't exist at this depth, we will just create an
  89. // empty array to hold the next value, allowing us to create the
  90. // arrays to hold the final value.
  91. if ( ! isset($array[$key]) or ! is_array($array[$key]))
  92. {
  93. $array[$key] = array();
  94. }
  95. $array =& $array[$key];
  96. }
  97. $array[array_shift($keys)] = $value;
  98. }
  99. /**
  100. * Remove an array item from a given array using "dot" notation.
  101. *
  102. * <code>
  103. * // Remove the $array['user']['name'] item from the array
  104. * array_forget($array, 'user.name');
  105. *
  106. * // Remove the $array['user']['name']['first'] item from the array
  107. * array_forget($array, 'user.name.first');
  108. * </code>
  109. *
  110. * @param array $array
  111. * @param string $key
  112. * @return void
  113. */
  114. function array_forget(&$array, $key)
  115. {
  116. $keys = explode('.', $key);
  117. // This loop functions very similarly to the loop in the "set" method.
  118. // We will iterate over the keys, setting the array value to the new
  119. // depth at each iteration. Once there is only one key left, we will
  120. // be at the proper depth in the array.
  121. while (count($keys) > 1)
  122. {
  123. $key = array_shift($keys);
  124. // Since this method is supposed to remove a value from the array,
  125. // if a value higher up in the chain doesn't exist, there is no
  126. // need to keep digging into the array, since it is impossible
  127. // for the final value to even exist.
  128. if ( ! isset($array[$key]) or ! is_array($array[$key]))
  129. {
  130. return;
  131. }
  132. $array =& $array[$key];
  133. }
  134. unset($array[array_shift($keys)]);
  135. }
  136. /**
  137. * Return the first element in an array which passes a given truth test.
  138. *
  139. * <code>
  140. * // Return the first array element that equals "Taylor"
  141. * $value = array_first($array, function($k, $v) {return $v == 'Taylor';});
  142. *
  143. * // Return a default value if no matching element is found
  144. * $value = array_first($array, function($k, $v) {return $v == 'Taylor'}, 'Default');
  145. * </code>
  146. *
  147. * @param array $array
  148. * @param Closure $callback
  149. * @param mixed $default
  150. * @return mixed
  151. */
  152. function array_first($array, $callback, $default = null)
  153. {
  154. foreach ($array as $key => $value)
  155. {
  156. if (call_user_func($callback, $key, $value)) return $value;
  157. }
  158. return value($default);
  159. }
  160. /**
  161. * Recursively remove slashes from array keys and values.
  162. *
  163. * @param array $array
  164. * @return array
  165. */
  166. function array_strip_slashes($array)
  167. {
  168. $result = array();
  169. foreach($array as $key => $value)
  170. {
  171. $key = stripslashes($key);
  172. // If the value is an array, we will just recurse back into the
  173. // function to keep stripping the slashes out of the array,
  174. // otherwise we will set the stripped value.
  175. if (is_array($value))
  176. {
  177. $result[$key] = array_strip_slashes($value);
  178. }
  179. else
  180. {
  181. $result[$key] = stripslashes($value);
  182. }
  183. }
  184. return $result;
  185. }
  186. /**
  187. * Divide an array into two arrays. One with keys and the other with values.
  188. *
  189. * @param array $array
  190. * @return array
  191. */
  192. function array_divide($array)
  193. {
  194. return array(array_keys($array), array_values($array));
  195. }
  196. /**
  197. * Determine if "Magic Quotes" are enabled on the server.
  198. *
  199. * @return bool
  200. */
  201. function magic_quotes()
  202. {
  203. return function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc();
  204. }
  205. /**
  206. * Return the first element of an array.
  207. *
  208. * This is simply a convenient wrapper around the "reset" method.
  209. *
  210. * @param array $array
  211. * @return mixed
  212. */
  213. function head($array)
  214. {
  215. return reset($array);
  216. }
  217. /**
  218. * Generate an application URL.
  219. *
  220. * <code>
  221. * // Create a URL to a location within the application
  222. * $url = path('user/profile');
  223. *
  224. * // Create a HTTPS URL to a location within the application
  225. * $url = path('user/profile', true);
  226. * </code>
  227. *
  228. * @param string $url
  229. * @param bool $https
  230. * @return string
  231. */
  232. function url($url = '', $https = false)
  233. {
  234. return Laravel\URL::to($url, $https);
  235. }
  236. /**
  237. * Generate an application URL to an asset.
  238. *
  239. * @param string $url
  240. * @param bool $https
  241. * @return string
  242. */
  243. function asset($url, $https = false)
  244. {
  245. return Laravel\URL::to_asset($url, $https);
  246. }
  247. /**
  248. * Generate a URL to a controller action.
  249. *
  250. * <code>
  251. * // Generate a URL to the "index" method of the "user" controller
  252. * $url = action('user@index');
  253. *
  254. * // Generate a URL to http://example.com/user/profile/taylor
  255. * $url = action('user@profile', array('taylor'));
  256. * </code>
  257. *
  258. * @param string $action
  259. * @param array $parameters
  260. * @return string
  261. */
  262. function action($action, $parameters = array())
  263. {
  264. return Laravel\URL::to_action($action, $parameters);
  265. }
  266. /**
  267. * Generate a URL from a route name.
  268. *
  269. * <code>
  270. * // Create a URL to the "profile" named route
  271. * $url = route('profile');
  272. *
  273. * // Create a URL to the "profile" named route with wildcard parameters
  274. * $url = route('profile', array($username));
  275. * </code>
  276. *
  277. * @param string $name
  278. * @param array $parameters
  279. * @return string
  280. */
  281. function route($name, $parameters = array())
  282. {
  283. return Laravel\URL::to_route($name, $parameters);
  284. }
  285. /**
  286. * Determine if a given string begins with a given value.
  287. *
  288. * @param string $haystack
  289. * @param string $needle
  290. * @return bool
  291. */
  292. function starts_with($haystack, $needle)
  293. {
  294. return strpos($haystack, $needle) === 0;
  295. }
  296. /**
  297. * Determine if a given string ends with a given value.
  298. *
  299. * @param string $haystack
  300. * @param string $needle
  301. * @return bool
  302. */
  303. function ends_with($haystack, $needle)
  304. {
  305. return $needle == substr($haystack, strlen($haystack) - strlen($needle));
  306. }
  307. /**
  308. * Determine if a given string contains a given sub-string.
  309. *
  310. * @param string $haystack
  311. * @param string $needle
  312. * @return bool
  313. */
  314. function str_contains($haystack, $needle)
  315. {
  316. return strpos($haystack, $needle) !== false;
  317. }
  318. /**
  319. * Cap a string with a single instance of the given string.
  320. *
  321. * @param string $value
  322. * @param string $cap
  323. * @return string
  324. */
  325. function str_finish($value, $cap)
  326. {
  327. return rtrim($value, $cap).$cap;
  328. }
  329. /**
  330. * Get the root namespace of a given class.
  331. *
  332. * @param string $class
  333. * @param string $separator
  334. * @return string
  335. */
  336. function root_namespace($class, $separator = '\\')
  337. {
  338. if (str_contains($class, $separator))
  339. {
  340. return head(explode($separator, $class));
  341. }
  342. }
  343. /**
  344. * Get the "class basename" of a class or object.
  345. *
  346. * The basename is considered the name of the class minus all namespaces.
  347. *
  348. * @param object|string $class
  349. * @return string
  350. */
  351. function class_basename($class)
  352. {
  353. if (is_object($class)) $class = get_class($class);
  354. return basename(str_replace('\\', '/', $class));
  355. }
  356. /**
  357. * Return the value of the given item.
  358. *
  359. * If the given item is a Closure the result of the Closure will be returned.
  360. *
  361. * @param mixed $value
  362. * @return mixed
  363. */
  364. function value($value)
  365. {
  366. return ($value instanceof Closure) ? call_user_func($value) : $value;
  367. }
  368. /**
  369. * Short-cut for constructor method chaining.
  370. *
  371. * @param mixed $object
  372. * @return mixed
  373. */
  374. function with($object)
  375. {
  376. return $object;
  377. }
  378. /**
  379. * Determine if the current version of PHP is at least the supplied version.
  380. *
  381. * @param string $version
  382. * @return bool
  383. */
  384. function has_php($version)
  385. {
  386. return version_compare(PHP_VERSION, $version) >= 0;
  387. }
  388. /**
  389. * Get a view instance.
  390. *
  391. * @param string $view
  392. * @param array $data
  393. * @return View
  394. */
  395. function view($view, $data = array())
  396. {
  397. if (is_null($view)) return '';
  398. return Laravel\View::make($view, $data);
  399. }
  400. /**
  401. * Render the given view.
  402. *
  403. * @param string $view
  404. * @param array $data
  405. * @return string
  406. */
  407. function render($view, $data = array())
  408. {
  409. if (is_null($view)) return '';
  410. return Laravel\View::make($view, $data)->render();
  411. }
  412. /**
  413. * Get the rendered contents of a partial from a loop.
  414. *
  415. * @param string $view
  416. * @param array $data
  417. * @param string $iterator
  418. * @param string $empty
  419. * @return string
  420. */
  421. function render_each($partial, array $data, $iterator, $empty = 'raw|')
  422. {
  423. return Laravel\View::render_each($partial, $data, $iterator, $empty);
  424. }
  425. /**
  426. * Get the string contents of a section.
  427. *
  428. * @param string $section
  429. * @return string
  430. */
  431. function yield($section)
  432. {
  433. return Laravel\Section::yield($section);
  434. }