Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Carbon\Carbon;
- use Nissi\Proxies\Text;
- use Nissi\Proxies\Format;
- use Nissi\Proxies\Number;
- use Nissi\Proxies\Geography;
- /*
- |--------------------------------------------------------------------------
- | App-specific Helpers
- |--------------------------------------------------------------------------
- */
- function site_name()
- {
- return Config::get('project.name');
- }
- function from_address()
- {
- return Config::get('project.from_address');
- }
- function from_name()
- {
- return Config::get('project.from_name');
- }
- function recipient_address()
- {
- return Config::get('project.recipient_address');
- }
- function recipient_name()
- {
- return Config::get('project.recipient_name');
- }
- function contact_phone($format = null)
- {
- return format_phone(Config::get('contact.phone'), $format);
- }
- function contact_fax()
- {
- return format_phone(Config::get('contact.fax'));
- }
- function contact_address()
- {
- return new \Nissi\ValueObjects\PostalAddress(Config::get('contact'));
- }
- function contact_email()
- {
- return \Html::mailto(Config::get('contact.email'));
- }
- function page_title($title = '', $separator = null, $position = 'left')
- {
- if ( ! $separator) {
- $separator = Config::get('project.title_separator', ' | ');
- }
- if ( ! $title) {
- $separator = '';
- }
- if ($position == 'right') {
- return site_name() . $separator . $title;
- }
- return $title . $separator . site_name();
- }
- /*
- |--------------------------------------------------------------------------
- | Debugging Helpers
- |--------------------------------------------------------------------------
- */
- /*
- * `print_r` inside a `pre` tag.
- */
- function pr($var, $exit = false)
- {
- echo '<pre>';
- print_r($var);
- echo '</pre>' . PHP_EOL;
- if ($exit) {
- exit;
- }
- }
- /*
- * `var_dump` inside a `pre` tag.
- */
- function vd($var, $exit = true)
- {
- echo '<pre>';
- var_dump($var);
- echo "</pre>\n";
- if ($exit) {
- exit;
- }
- }
- /*
- * Return HTML from a Markdown string using Parsedown library.
- */
- function parsedown($text)
- {
- return (new Parsedown)->text($text);
- }
- /*
- |--------------------------------------------------------------------------
- | URL Helpers
- |--------------------------------------------------------------------------
- */
- /*
- * Generate a mailto: link to the given address
- */
- function mailto($email, $title = null, $attributes = [])
- {
- return \Html::mailto($email, $title, $attributes);
- }
- /*
- |--------------------------------------------------------------------------
- | Date/Time Helpers
- |--------------------------------------------------------------------------
- */
- /*
- * Returns a Carbon instance (or formatted string) on success,
- * Returns null on failure (i.e. invalid date)
- */
- function make_date($datestring, $format = null)
- {
- try {
- // No date string provided. Return null.
- if (empty($datestring)) {
- return;
- }
- // Invalid date. Return null.
- if ( ! $dt = Carbon::parse($datestring)) {
- return;
- }
- // Date out of range. Return null.
- if ($dt < Carbon::minValue() || $dt > Carbon::maxValue()) {
- return;
- }
- // Return a formatted string.
- if ($format) {
- return $dt->format($format);
- }
- // Return Carbon instance
- return $dt;
- } catch (\Exception $e) {
- return;
- }
- }
- /*
- |--------------------------------------------------------------------------
- | Data Formatters
- |--------------------------------------------------------------------------
- */
- /*
- * Format a phone number according to preferences.
- */
- function format_phone($phone, $format = null, array $options = [])
- {
- return Format::phone($phone, $format, $options);
- }
- /*
- * Format a URL
- */
- function format_url($url, array $options = [])
- {
- return Format::url($url, $options);
- }
- /*
- * Format a number as currency: 1234.00
- */
- function currency($amount, $options = [])
- {
- return Number::currency($amount, $options);
- }
- /*
- * Truncate a string to a given length
- */
- function truncate($string, $length = 20, $options = [])
- {
- return Text::truncate($string, $length, $options);
- }
- /*
- * Make a "safe" slug by guarding against duplicates
- */
- function make_slug($string, $table = 'posts', $column = 'slug')
- {
- $slug = str_slug($string);
- $count = DB::table($table)->whereRaw("{$column} RLIKE '^{$slug}(-[0-9]+)?$'")->count();
- return $count ? sprintf('%s-%s', $slug, $count + 1) : $slug;
- }
- /*
- |--------------------------------------------------------------------------
- | Array Helpers
- |--------------------------------------------------------------------------
- */
- /*
- * Makes array keys the same as the values. Useful for select elements.
- */
- function array_reflect($arr = [])
- {
- return array_combine(array_values($arr), array_values($arr));
- }
- /*
- |--------------------------------------------------------------------------
- | Menu Helpers
- |--------------------------------------------------------------------------
- */
- /**
- * Determine if a given menu item is "active" based on URL.
- *
- * @return string
- */
- function set_active($pathName, $className = 'active')
- {
- return call_user_func_array('Request::is', (array) $pathName) ? $className : '';
- }
- /**
- * Determine if a given menu item is active based on named route.
- *
- * @return string
- */
- function set_active_route($routeName, $className = 'active')
- {
- if ( ! Request::route()) {
- return;
- }
- return (Request::route()->getName() == $routeName) ? $className : '';
- }
- /*
- |--------------------------------------------------------------------------
- | Form Helpers
- |--------------------------------------------------------------------------
- */
- /*
- * Array of US states in $abbrev => $name format.
- */
- function us_states()
- {
- return Geography::usStates();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement