PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/helpers.php

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