Last active
August 29, 2015 14:24
-
-
Save atorscho/25e9560310f969e781c2 to your computer and use it in GitHub Desktop.
A helper class for Laravel projects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Helpers\MiscHelper; | |
use Illuminate\Pagination\Paginator; | |
if (!function_exists('to_objects')) { | |
/** | |
* Convert the array into an instance of stdClass. | |
* | |
* @param array $array | |
* | |
* @return stdClass | |
*/ | |
function to_objects(array $array) | |
{ | |
return MiscHelper::toObjects($array); | |
} | |
} | |
if (!function_exists('index')) { | |
/** | |
* Return an incremented index. | |
* | |
* @return int | |
*/ | |
function index() | |
{ | |
return MiscHelper::index(); | |
} | |
} | |
if (!function_exists('counter')) { | |
/** | |
* Index counter for pagination. | |
* | |
* @param int $perPage | |
* | |
* @return int | |
*/ | |
function counter($perPage) | |
{ | |
return MiscHelper::counter($perPage); | |
} | |
} | |
if (!function_exists('exec_time')) { | |
/** | |
* Calculate page load time. | |
* | |
* @return mixed | |
*/ | |
function exec_time() | |
{ | |
return MiscHelper::execTime(); | |
} | |
} | |
if (!function_exists('get_date_format')) { | |
/** | |
* Return a formatted date string. | |
* | |
* @param object|string $date | |
* @param string $format | |
* | |
* @return bool|string | |
*/ | |
function get_date_format($date, $format = 'M d Y') | |
{ | |
return MiscHelper::dateFormat($date, $format); | |
} | |
} | |
if (!function_exists('get_datetime_format')) { | |
/** | |
* Return a formatted datetime string. | |
* | |
* @param object|string $dateTime | |
* @param string $format | |
* | |
* @return bool|string | |
*/ | |
function get_datetime_format($dateTime, $format = 'M d Y, H:i') | |
{ | |
return MiscHelper::dateTimeFormat($dateTime, $format); | |
} | |
} | |
if (!function_exists('paginate')) { | |
/** | |
* Use a custom pagination style. | |
* | |
* @param Paginator $model | |
* | |
* @return string | |
*/ | |
function paginate($model) | |
{ | |
return MiscHelper::paginate($model); | |
} | |
} | |
if (!function_exists('submit_form')) { | |
/** | |
* Submit a form using the form controls: save & new, save & close, apply. | |
* | |
* @param array $redirects | |
* | |
* @return mixed | |
*/ | |
function submit_form(array $redirects) | |
{ | |
return MiscHelper::submitForm($redirects); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Helpers; | |
use App\Http\SemanticUIPagination; | |
use Carbon\Carbon; | |
use Illuminate\Pagination\Paginator; | |
use Request; | |
class MiscHelper | |
{ | |
/** | |
* Convert the array into an instance of stdClass. | |
* | |
* @param array $array | |
* | |
* @return \stdClass | |
*/ | |
public static function toObjects(array $array) | |
{ | |
$object = new \stdClass; | |
foreach ($array as $key => $value) { | |
$object->$key = $value; | |
if (is_array($value)) { | |
$object->$key = static::toObjects($value); | |
} | |
} | |
return $object; | |
} | |
/** | |
* Return an incremented index. | |
* | |
* @return int | |
*/ | |
public static function index() | |
{ | |
static $tab = 0; | |
$tab++; | |
return $tab; | |
} | |
/** | |
* Index counter for pagination. | |
* | |
* @param int $perPage | |
* | |
* @return int | |
*/ | |
public static function counter($perPage) | |
{ | |
return (int) ( $perPage * ( ( Request::get('page') ?: 1 ) - 1 ) ) + 1; | |
} | |
/** | |
* Calculate page load time. | |
* | |
* @return mixed | |
*/ | |
public static function execTime() | |
{ | |
return round(microtime(true) - LARAVEL_START, 2); | |
} | |
/** | |
* Return a formatted date string. | |
* | |
* @param object|string $date | |
* @param string $format | |
* | |
* @return bool|string | |
*/ | |
public static function dateFormat($date, $format = 'M d Y') | |
{ | |
if ($date instanceof Carbon) { | |
return $date->format($format); | |
} | |
return date($format, strtotime((string) $date)); | |
} | |
/** | |
* Return a formatted datetime string. | |
* | |
* @param object|string $dateTime | |
* @param string $format | |
* | |
* @return bool|string | |
*/ | |
public static function dateTimeFormat($dateTime, $format = 'M d Y, H:i') | |
{ | |
return static::dateFormat($dateTime, $format); | |
} | |
/** | |
* Use a custom pagination style. | |
* | |
* @param Paginator $model | |
* | |
* @return string | |
*/ | |
public static function paginate($model) | |
{ | |
return with(new SemanticUIPagination($model))->render(); | |
} | |
/** | |
* Submit a form using the form controls: save & new, save & close, apply. | |
* | |
* @param array $redirects | |
* | |
* @return mixed | |
*/ | |
public static function submitForm(array $redirects) | |
{ | |
switch (Request::input('submit')) { | |
case 'save_close': | |
default: | |
Flash::success('saveClose'); | |
return redirect($redirects[0]); | |
case 'save_new': | |
Flash::success('saveNew'); | |
return redirect($redirects[1]); | |
case 'apply': | |
Flash::success('changesApplied'); | |
return redirect($redirects[2]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment