helpers.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. * Dump the given value and kill the script.
  28. *
  29. * @param mixed $value
  30. * @return void
  31. */
  32. function dd($value)
  33. {
  34. die(var_dump($value));
  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.
  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.
  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.
  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.
  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. * Recursively remove slashes from array keys and values.
  172. *
  173. * @param array $array
  174. * @return array
  175. */
  176. function array_strip_slashes($array)
  177. {
  178. $result = array();
  179. foreach($array as $key => $value)
  180. {
  181. $key = stripslashes($key);
  182. // If the value is an array, we will just recurse back into the
  183. // function to keep stripping the slashes out of the array,
  184. // otherwise we will set the stripped value.
  185. if (is_array($value))
  186. {
  187. $result[$key] = array_strip_slashes($value);
  188. }
  189. else
  190. {
  191. $result[$key] = stripslashes($value);
  192. }
  193. }
  194. return $result;
  195. }
  196. /**
  197. * Divide an array into two arrays. One with keys and the other with values.
  198. *
  199. * @param array $array
  200. * @return array
  201. */
  202. function array_divide($array)
  203. {
  204. return array(array_keys($array), array_values($array));
  205. }
  206. /**
  207. * Pluck an array of values from an array.
  208. *
  209. * @param array $array
  210. * @param string $key
  211. * @return array
  212. */
  213. function array_pluck($array, $key)
  214. {
  215. return array_map(function($v) use ($key)
  216. {
  217. return is_object($v) ? $v->$key : $v[$key];
  218. }, $array);
  219. }
  220. /**
  221. * Get a subset of the items from the given array.
  222. *
  223. * @param array $array
  224. * @param array $keys
  225. * @return array
  226. */
  227. function array_only($array, $keys)
  228. {
  229. return array_intersect_key( $array, array_flip((array) $keys) );
  230. }
  231. /**
  232. * Get all of the given array except for a specified array of items.
  233. *
  234. * @param array $array
  235. * @param array $keys
  236. * @return array
  237. */
  238. function array_except($array, $keys)
  239. {
  240. return array_diff_key( $array, array_flip((array) $keys) );
  241. }
  242. /**
  243. * Transform Eloquent models to a JSON object.
  244. *
  245. * @param Eloquent|array $models
  246. * @return object
  247. */
  248. function eloquent_to_json($models)
  249. {
  250. if ($models instanceof Laravel\Database\Eloquent\Model)
  251. {
  252. return json_encode($models->to_array());
  253. }
  254. return json_encode(array_map(function($m) { return $m->to_array(); }, $models));
  255. }
  256. /**
  257. * Determine if "Magic Quotes" are enabled on the server.
  258. *
  259. * @return bool
  260. */
  261. function magic_quotes()
  262. {
  263. return function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc();
  264. }
  265. /**
  266. * Return the first element of an array.
  267. *
  268. * This is simply a convenient wrapper around the "reset" method.
  269. *
  270. * @param array $array
  271. * @return mixed
  272. */
  273. function head($array)
  274. {
  275. return reset($array);
  276. }
  277. /**
  278. * Generate an application URL.
  279. *
  280. * <code>
  281. * // Create a URL to a location within the application
  282. * $url = path('user/profile');
  283. *
  284. * // Create a HTTPS URL to a location within the application
  285. * $url = path('user/profile', true);
  286. * </code>
  287. *
  288. * @param string $url
  289. * @param bool $https
  290. * @return string
  291. */
  292. function url($url = '', $https = false)
  293. {
  294. return Laravel\URL::to($url, $https);
  295. }
  296. /**
  297. * Generate an application URL to an asset.
  298. *
  299. * @param string $url
  300. * @param bool $https
  301. * @return string
  302. */
  303. function asset($url, $https = false)
  304. {
  305. return Laravel\URL::to_asset($url, $https);
  306. }
  307. /**
  308. * Generate a URL to a controller action.
  309. *
  310. * <code>
  311. * // Generate a URL to the "index" method of the "user" controller
  312. * $url = action('user@index');
  313. *
  314. * // Generate a URL to http://example.com/user/profile/taylor
  315. * $url = action('user@profile', array('taylor'));
  316. * </code>
  317. *
  318. * @param string $action
  319. * @param array $parameters
  320. * @return string
  321. */
  322. function action($action, $parameters = array())
  323. {
  324. return Laravel\URL::to_action($action, $parameters);
  325. }
  326. /**
  327. * Generate a URL from a route name.
  328. *
  329. * <code>
  330. * // Create a URL to the "profile" named route
  331. * $url = route('profile');
  332. *
  333. * // Create a URL to the "profile" named route with wildcard parameters
  334. * $url = route('profile', array($username));
  335. * </code>
  336. *
  337. * @param string $name
  338. * @param array $parameters
  339. * @return string
  340. */
  341. function route($name, $parameters = array())
  342. {
  343. return Laravel\URL::to_route($name, $parameters);
  344. }
  345. /**
  346. * Determine if a given string begins with a given value.
  347. *
  348. * @param string $haystack
  349. * @param string $needle
  350. * @return bool
  351. */
  352. function starts_with($haystack, $needle)
  353. {
  354. return strpos($haystack, $needle) === 0;
  355. }
  356. /**
  357. * Determine if a given string ends with a given value.
  358. *
  359. * @param string $haystack
  360. * @param string $needle
  361. * @return bool
  362. */
  363. function ends_with($haystack, $needle)
  364. {
  365. return $needle == substr($haystack, strlen($haystack) - strlen($needle));
  366. }
  367. /**
  368. * Determine if a given string contains a given sub-string.
  369. *
  370. * @param string $haystack
  371. * @param string|array $needle
  372. * @return bool
  373. */
  374. function str_contains($haystack, $needle)
  375. {
  376. foreach ((array) $needle as $n)
  377. {
  378. if (strpos($haystack, $n) !== false) return true;
  379. }
  380. return false;
  381. }
  382. /**
  383. * Cap a string with a single instance of the given string.
  384. *
  385. * @param string $value
  386. * @param string $cap
  387. * @return string
  388. */
  389. function str_finish($value, $cap)
  390. {
  391. return rtrim($value, $cap).$cap;
  392. }
  393. /**
  394. * Determine if the given object has a toString method.
  395. *
  396. * @param object $value
  397. * @return bool
  398. */
  399. function str_object($value)
  400. {
  401. return is_object($value) and method_exists($value, '__toString');
  402. }
  403. /**
  404. * Get the root namespace of a given class.
  405. *
  406. * @param string $class
  407. * @param string $separator
  408. * @return string
  409. */
  410. function root_namespace($class, $separator = '\\')
  411. {
  412. if (str_contains($class, $separator))
  413. {
  414. return head(explode($separator, $class));
  415. }
  416. }
  417. /**
  418. * Get the "class basename" of a class or object.
  419. *
  420. * The basename is considered the name of the class minus all namespaces.
  421. *
  422. * @param object|string $class
  423. * @return string
  424. */
  425. function class_basename($class)
  426. {
  427. if (is_object($class)) $class = get_class($class);
  428. return basename(str_replace('\\', '/', $class));
  429. }
  430. /**
  431. * Return the value of the given item.
  432. *
  433. * If the given item is a Closure the result of the Closure will be returned.
  434. *
  435. * @param mixed $value
  436. * @return mixed
  437. */
  438. function value($value)
  439. {
  440. return (is_callable($value) and ! is_string($value)) ? call_user_func($value) : $value;
  441. }
  442. /**
  443. * Short-cut for constructor method chaining.
  444. *
  445. * @param mixed $object
  446. * @return mixed
  447. */
  448. function with($object)
  449. {
  450. return $object;
  451. }
  452. /**
  453. * Determine if the current version of PHP is at least the supplied version.
  454. *
  455. * @param string $version
  456. * @return bool
  457. */
  458. function has_php($version)
  459. {
  460. return version_compare(PHP_VERSION, $version) >= 0;
  461. }
  462. /**
  463. * Get a view instance.
  464. *
  465. * @param string $view
  466. * @param array $data
  467. * @return View
  468. */
  469. function view($view, $data = array())
  470. {
  471. if (is_null($view)) return '';
  472. return Laravel\View::make($view, $data);
  473. }
  474. /**
  475. * Render the given view.
  476. *
  477. * @param string $view
  478. * @param array $data
  479. * @return string
  480. */
  481. function render($view, $data = array())
  482. {
  483. if (is_null($view)) return '';
  484. return Laravel\View::make($view, $data)->render();
  485. }
  486. /**
  487. * Get the rendered contents of a partial from a loop.
  488. *
  489. * @param string $view
  490. * @param array $data
  491. * @param string $iterator
  492. * @param string $empty
  493. * @return string
  494. */
  495. function render_each($partial, array $data, $iterator, $empty = 'raw|')
  496. {
  497. return Laravel\View::render_each($partial, $data, $iterator, $empty);
  498. }
  499. /**
  500. * Get the string contents of a section.
  501. *
  502. * @param string $section
  503. * @return string
  504. */
  505. function yield($section)
  506. {
  507. return Laravel\Section::yield($section);
  508. }
  509. /**
  510. * Get a CLI option from the argv $_SERVER variable.
  511. *
  512. * @param string $option
  513. * @param mixed $default
  514. * @return string
  515. */
  516. function get_cli_option($option, $default = null)
  517. {
  518. foreach (Laravel\Request::foundation()->server->get('argv') as $argument)
  519. {
  520. if (starts_with($argument, "--{$option}="))
  521. {
  522. return substr($argument, strlen($option) + 3);
  523. }
  524. }
  525. return value($default);
  526. }