Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. <?php
  2. use Carbon\Carbon;
  3. use Nissi\Proxies\Text;
  4. use Nissi\Proxies\Format;
  5. use Nissi\Proxies\Number;
  6. use Nissi\Proxies\Geography;
  7.  
  8. /*
  9. |--------------------------------------------------------------------------
  10. | App-specific Helpers
  11. |--------------------------------------------------------------------------
  12. */
  13.  
  14. function site_name()
  15. {
  16. return Config::get('project.name');
  17. }
  18.  
  19. function from_address()
  20. {
  21. return Config::get('project.from_address');
  22. }
  23.  
  24. function from_name()
  25. {
  26. return Config::get('project.from_name');
  27. }
  28.  
  29. function recipient_address()
  30. {
  31. return Config::get('project.recipient_address');
  32. }
  33.  
  34. function recipient_name()
  35. {
  36. return Config::get('project.recipient_name');
  37. }
  38.  
  39. function contact_phone($format = null)
  40. {
  41. return format_phone(Config::get('contact.phone'), $format);
  42. }
  43.  
  44. function contact_fax()
  45. {
  46. return format_phone(Config::get('contact.fax'));
  47. }
  48.  
  49. function contact_address()
  50. {
  51. return new \Nissi\ValueObjects\PostalAddress(Config::get('contact'));
  52. }
  53.  
  54. function contact_email()
  55. {
  56. return \Html::mailto(Config::get('contact.email'));
  57. }
  58.  
  59. function page_title($title = '', $separator = null, $position = 'left')
  60. {
  61. if ( ! $separator) {
  62. $separator = Config::get('project.title_separator', ' | ');
  63. }
  64.  
  65. if ( ! $title) {
  66. $separator = '';
  67. }
  68.  
  69. if ($position == 'right') {
  70. return site_name() . $separator . $title;
  71. }
  72.  
  73. return $title . $separator . site_name();
  74. }
  75.  
  76. /*
  77. |--------------------------------------------------------------------------
  78. | Debugging Helpers
  79. |--------------------------------------------------------------------------
  80. */
  81.  
  82. /*
  83. * `print_r` inside a `pre` tag.
  84. */
  85. function pr($var, $exit = false)
  86. {
  87. echo '<pre>';
  88. print_r($var);
  89. echo '</pre>' . PHP_EOL;
  90.  
  91. if ($exit) {
  92. exit;
  93. }
  94. }
  95.  
  96. /*
  97. * `var_dump` inside a `pre` tag.
  98. */
  99. function vd($var, $exit = true)
  100. {
  101. echo '<pre>';
  102. var_dump($var);
  103. echo "</pre>\n";
  104.  
  105. if ($exit) {
  106. exit;
  107. }
  108. }
  109.  
  110. /*
  111. * Return HTML from a Markdown string using Parsedown library.
  112. */
  113. function parsedown($text)
  114. {
  115. return (new Parsedown)->text($text);
  116. }
  117.  
  118. /*
  119. |--------------------------------------------------------------------------
  120. | URL Helpers
  121. |--------------------------------------------------------------------------
  122. */
  123.  
  124. /*
  125. * Generate a mailto: link to the given address
  126. */
  127. function mailto($email, $title = null, $attributes = [])
  128. {
  129. return \Html::mailto($email, $title, $attributes);
  130. }
  131.  
  132. /*
  133. |--------------------------------------------------------------------------
  134. | Date/Time Helpers
  135. |--------------------------------------------------------------------------
  136. */
  137.  
  138. /*
  139. * Returns a Carbon instance (or formatted string) on success,
  140. * Returns null on failure (i.e. invalid date)
  141. */
  142. function make_date($datestring, $format = null)
  143. {
  144. try {
  145. // No date string provided. Return null.
  146. if (empty($datestring)) {
  147. return;
  148. }
  149.  
  150. // Invalid date. Return null.
  151. if ( ! $dt = Carbon::parse($datestring)) {
  152. return;
  153. }
  154.  
  155. // Date out of range. Return null.
  156. if ($dt < Carbon::minValue() || $dt > Carbon::maxValue()) {
  157. return;
  158. }
  159.  
  160. // Return a formatted string.
  161. if ($format) {
  162. return $dt->format($format);
  163. }
  164.  
  165. // Return Carbon instance
  166. return $dt;
  167. } catch (\Exception $e) {
  168. return;
  169. }
  170. }
  171.  
  172. /*
  173. |--------------------------------------------------------------------------
  174. | Data Formatters
  175. |--------------------------------------------------------------------------
  176. */
  177.  
  178. /*
  179. * Format a phone number according to preferences.
  180. */
  181. function format_phone($phone, $format = null, array $options = [])
  182. {
  183. return Format::phone($phone, $format, $options);
  184. }
  185.  
  186. /*
  187. * Format a URL
  188. */
  189. function format_url($url, array $options = [])
  190. {
  191. return Format::url($url, $options);
  192. }
  193.  
  194. /*
  195. * Format a number as currency: 1234.00
  196. */
  197. function currency($amount, $options = [])
  198. {
  199. return Number::currency($amount, $options);
  200. }
  201.  
  202. /*
  203. * Truncate a string to a given length
  204. */
  205. function truncate($string, $length = 20, $options = [])
  206. {
  207. return Text::truncate($string, $length, $options);
  208. }
  209.  
  210. /*
  211. * Make a "safe" slug by guarding against duplicates
  212. */
  213. function make_slug($string, $table = 'posts', $column = 'slug')
  214. {
  215. $slug = str_slug($string);
  216.  
  217. $count = DB::table($table)->whereRaw("{$column} RLIKE '^{$slug}(-[0-9]+)?$'")->count();
  218.  
  219. return $count ? sprintf('%s-%s', $slug, $count + 1) : $slug;
  220. }
  221.  
  222. /*
  223. |--------------------------------------------------------------------------
  224. | Array Helpers
  225. |--------------------------------------------------------------------------
  226. */
  227.  
  228. /*
  229. * Makes array keys the same as the values. Useful for select elements.
  230. */
  231. function array_reflect($arr = [])
  232. {
  233. return array_combine(array_values($arr), array_values($arr));
  234. }
  235.  
  236. /*
  237. |--------------------------------------------------------------------------
  238. | Menu Helpers
  239. |--------------------------------------------------------------------------
  240. */
  241.  
  242. /**
  243. * Determine if a given menu item is "active" based on URL.
  244. *
  245. * @return string
  246. */
  247. function set_active($pathName, $className = 'active')
  248. {
  249. return call_user_func_array('Request::is', (array) $pathName) ? $className : '';
  250. }
  251.  
  252. /**
  253. * Determine if a given menu item is active based on named route.
  254. *
  255. * @return string
  256. */
  257. function set_active_route($routeName, $className = 'active')
  258. {
  259. if ( ! Request::route()) {
  260. return;
  261. }
  262.  
  263. return (Request::route()->getName() == $routeName) ? $className : '';
  264. }
  265.  
  266. /*
  267. |--------------------------------------------------------------------------
  268. | Form Helpers
  269. |--------------------------------------------------------------------------
  270. */
  271.  
  272. /*
  273. * Array of US states in $abbrev => $name format.
  274. */
  275. function us_states()
  276. {
  277. return Geography::usStates();
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement