Skip to content

Instantly share code, notes, and snippets.

@JonMadVal
Created January 12, 2018 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonMadVal/1041df628608ae8c49b409796cacf86e to your computer and use it in GitHub Desktop.
Save JonMadVal/1041df628608ae8c49b409796cacf86e to your computer and use it in GitHub Desktop.
My personal "Laravel Helper" file.
<?php
// Constantes de ayuda
const __punto__ = '.';
const __coma__ = ',';
const __guion__ = '-';
const __espacio__ = ' ';
if (!function_exists('user')) {
/**
* Retorna el usuario autenticado en el sistema.
* @return [type] [description]
*/
function user()
{
return Auth::user();
}
}
if (!function_exists('str_title')) {
/**
* Retorna un string con el formato UCFIRST
* @param string $name
* @return string
*/
function str_title($string)
{
return trim(mb_convert_case(strtolower($string), MB_CASE_TITLE));
}
}
if (!function_exists('str_tel')) {
/**
* Retorna un string con formato telefonico.
* @param string $telefono
* @return string
*/
function str_tel($telefono)
{
// Eliminando cualquier caracter que no sea un numero
$telefono = preg_replace('/[^0-9]/', null, $telefono);
if (is_numeric($telefono) and equal($telefono, 11)) {
return implode(__guion__, str_split($telefono, 4));
}
return false;
}
}
if (!function_exists('str_money')) {
/**
* Retorna un string con formato monetario.
* @param [type] $integer [description]
* @return [type] [description]
*/
function str_money($integer)
{
return is_numeric($integer) ? number_format($integer, 2, __coma__, __punto__) : false;
}
}
if (!function_exists('str_bank')) {
/**
* Retorna un string con formato de cuenta bancaria.
* @param [type] $cuenta [description]
* @return [type] [description]
*/
function str_bank($cuenta)
{
return strlen($cuenta) == 20 ? implode(__guion__, str_split($cuenta, 4)) : false;
}
}
if (!function_exists('carbon')) {
/**
* Retorna un objeto Carbon\Carbon
* @param $time [<description>]
* @return [type] [description]
*/
function carbon($time = null)
{
return new Carbon\Carbon(is_null($time) ?: $time, 'America/Caracas');
}
}
if (!function_exists('gmbn')) {
/**
* Retorna el mes del año.
* @param [type] $integer [description]
* @return [type] [description]
*/
function gmbn($integer, $string = false)
{
$months = array('Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
if ($string) {
return $months;
}
return array_key_exists(($integer - 1), $months) ? $months[(int) $integer - 1] : false;
}
}
if (!function_exists('str_date')) {
/**
* Retorna una fecha formateada en un formato textual.
* @param [type] $time [description]
* @return [type] [description]
*/
function str_date($time)
{
$time = carbon($time)->format('d-m-Y');
list($dia, $mes, $year) = explode(__guion__, $time);
return $dia . ' de ' . gmbn($mes) . ' del ' . $year;
}
}
if (!function_exists('zerofill')) {
/**
* Retorna un numero zerofill.
* @param [type] $integer [description]
* @return [type] [description]
*/
function zerofill($integer)
{
return str_pad($integer, 12, 0, STR_PAD_LEFT);
}
}
if (!function_exists('equal')) {
/**
* Comprueba la igualdad en dos o mas valores.
* @param [type] $params [description]
* @return [type] [description]
*/
function equal(...$params)
{
$counter = 0;
if (count($params) < 2) {
return false;
}
foreach ($params as $key => $prev) {
foreach ($params as $index => $next) {
if ($prev === $next) {
$counter++;
break;
} else {
$counter--;
}
}
}
return $counter === count($params);
}
}
if (!function_exists('active')) {
/**
* Verifica si la ruta enviada esta activa.
* @param [type] $url [description]
* @param array $params [description]
* @return [type] [description]
*/
function active($url)
{
return equal(Request::url(), url($url)) ? 'active' : false;
}
}
if (!function_exists('collapse')) {
/**
* Verifica si la ruta enviada esta activa y pertenece a una colleccion.
* @param array $urls [description]
* @return [type] [description]
*/
function collapse(array $urls)
{
foreach ($urls as $url) {
if (active($url)) {
return 'show';
}
}
}
}
if (!function_exists('contain')) {
/**
* Verficia si un array u objeto contiene datos
* @param array $data [description]
* @return [type] [description]
*/
function contain($data)
{
return count($data) > 0;
}
}
if (!function_exists('check')) {
/**
* Verifica la valides de una transacción
* @param boolean $query La transacción realizada
* @param boolean $model El modelo utilizado
* @param string $identifier El identificador de la transacción
* @return array
*/
function check($query = false, $model = false, $identifier = "id")
{
// Verifica si el modelo es false
if (!$model) {
// El modelo toma el valor del retorno
// de la transaccion realizada.
// Esto es util para transacciones cuyos
// retornos son de tipo object.
$model = $query;
}
// Para las transacciones de cuyos retornos son de tipo boolean es indispensable que
// se reciba el modelo de la transaccion como parametro obligatorio.
return $query ? array('active' => $model->$identifier, 'success' => true) : array('danger' => true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment