Skip to content

Instantly share code, notes, and snippets.

@demonio
Last active July 23, 2016 20:38
Show Gist options
  • Save demonio/50e5db46e8e079ef9bd9 to your computer and use it in GitHub Desktop.
Save demonio/50e5db46e8e079ef9bd9 to your computer and use it in GitHub Desktop.
Librería que hace de navaja suiza con métodos estáticos de uso habitual en el desarrollo de APPs.
<?php
/**
* SAK, ABREVIATURA DE NAVAJA SUIZA, FUNCIONES PHP DE USO FRECUENTE
*/
class Sak
{
public static $css = array(); # Sak::$css[$media][]
public static $js = array(); # Sak::$js[$hook][]
public static $meta = array(); # Sak::$meta[]
static public function _js($fn, $value, $delay=0)
{
# ALGUNAS FUNCIONES JS TIENEN OTRO FORMATO
$a = array('location');
# FORMATO FN='VALUE';
if (in_array($fn, $a) )
{
$s = "$fn='$value';";
}
# FORMATO FN('VALUE');
else
{
$s = "$fn('$value');";
}
# RETARDO PARA LANZAR LA FUNCION EN SEGUNDOS
if ($delay)
{
$delay = $delay*1000;
$s = "setTimeout(\"$s\", $delay);";
}
return $s;
}
static public function _wrap($s, $tag, $opt='', $charset='UTF-8')
{
if ($tag == 'script') $opt .= ' type="text/javascript" charset="' . $charset . '"';
return "<$tag$opt>$s</$tag>";
}
static public function _location($url, $delay=0)
{
# SE COMPRUEBA SI SE ENVIARON CABECERAS. POR EJEMPLO COMO PASA CON AJAX
if ( headers_sent() )
{
# SI EL USUARIO TIENE JAVASCRIPT ACTIVADO SE USA LOCATION
echo self::_wrap(self::_js('location', $url, $delay), 'script');
# SI NO LO TIENE SE USA META
echo self::_wrap('<meta http-equiv="refresh" content="' . $delay . ';url=' . $url . '" />', 'noscript');
}
else
{
# SI NO SE ENVIARON CABECERAS, AQUI VA UNA
sleep($delay);
header('Location: ' . $url);
}
exit();
}
static public function _str($mix)
{
if ( is_array($mix) ) $s = self::_wrap(htmlentities(print_r($mix, 1), ENT_QUOTES), 'pre');
else $s = htmlentities($mix, ENT_QUOTES);
return $s;
}
static public function _die($mix)
{
die( self::_str($mix) );
}
static public function _flush()
{
ob_flush();
flush();
}
static public function _red($s)
{
return self::_wrap($s, 'span', ' style="color:red"');
}
/**
* C) CORTA CADENAS
*/
static function cut($beg='', $s='', $end='')
{
if ( ! $s ) return;
$a = explode($beg, $s);
if ( ! $end ) return $a[1];
$b = explode($end, $a[1]);
return $b[0];
}
/**
* D1) FORMATEA FECHAS
*/
static function rdate($s='now', $format='d-m-Y')
{
return date( $format, strtotime($s) );
}
static function edate($s='now', $format='d-m-Y') { echo self::rdate($s, $format); }
/**
* D2) CALCULA LA EDAD
*/
static function rage($s)
{
$birth = date( 'Y', strtotime($s) );
$now = date( 'Y', strtotime('now') );
$age = (int)($now-$birth);
return $age;
}
static function eage($s) { echo self::rage($s); }
/**
* IN1) INCLUYE CSS, JS Y META TAGS "<?=Sak::inc('js', 'foot')?>"
*/
public static function inc( $tag, $k='' )
{
$o = get_class_vars( __CLASS__ );
$a = ( empty( $o[$tag][$k] ) ) ? $o[$tag] : $o[$tag][$k];
$s = '';
foreach ( $a as $v )
{
if ( strstr( $v, '<!' ) ) $s .= $v;
else
{
if ( $tag == 'css' )
{
if ( ! self::is( $v, 'url' ) ) $v = PUBLIC_PATH . "css/$v.css";
$s .= "<link href=\"$v\" media=\"$k\" rel=\"stylesheet\" />";
}
else if ( $tag == 'js' )
{
if ( ! self::is( $v, 'url' ) ) $v = PUBLIC_PATH . "javascript/$v.js";
$s .= "<script src=\"$v\"></script>";
}
else if ( $tag == 'meta' ) $s .= $v;
}
}
echo $s;
}
/**
* IN2) INCLUYE JS, OTRA FORMA MAS SIMPLE PARA INCLUIR JS
*/
static function js($s)
{
if ( strstr($s, '?') )
{
$a = explode('?', $s, 2);
$s = $a[0] . '.js?' . $a[1];
}
else
{
$s = $s . '.js';
}
?>
<script src="/js/<?php echo $s; ?>"></script>
<?php
}
/**
* IS) COMPRUEBA SI UNA CADENA ES DEL TIPO ESPECIFICADO
*/
public static function is( $x, $k )
{
$a=array
(
'boolean'=>FILTER_VALIDATE_BOOLEAN,
'email'=>FILTER_VALIDATE_EMAIL,
'float'=>FILTER_VALIDATE_FLOAT,
'int'=>FILTER_VALIDATE_INT,
'ip'=>FILTER_VALIDATE_IP,
'regexp'=>FILTER_VALIDATE_REGEXP,
'url'=>FILTER_VALIDATE_URL,
);
return filter_var( $x, $a[$k] );
}
/**
* P) IMPRIME EN PANTALLA VARIABLES
*/
static function r($x='')
{
if ( is_bool($x) )
{
if ($x === TRUE) $x = 'TRUE';
else if ($x === FALSE) $x = 'FALSE';
}
return '<pre>' . print_r($x, 1) . '</pre>';
}
static function e($x='') { echo self::r($x); }
static function d($x='') { die( self::r($x) ); }
/**
* IMPRIME VARIABLES Y ENVIA LA SALIDA A LA PANTALLA DINAMICAMENTE
*/
static function flush($x='')
{
self::e($x);
ob_flush();
flush();
}
}
#function tab( $n ) { return str_pad( '', $n, "\t", STR_PAD_LEFT ); }
Sak::$css['screen'][] = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css';
Sak::$css['screen'][] = 'https://netdna.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css';
Sak::$js['head'][] = '<!--[if lt IE 9]>';
Sak::$js['head'][] = 'https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js';
Sak::$js['head'][] = 'https://oss.maxcdn.com/respond/1.4.2/respond.min.js';
Sak::$js['head'][] = '<![endif]-->';
Sak::$js['foot'][] = 'http://code.jquery.com/jquery-latest.min.js';
Sak::$js['foot'][] = 'https://netdna.bootstrapcdn.com/bootstrap/latest/js/bootstrap.min.js';
Sak::$meta[] = '<meta charset="utf-8">';
Sak::$meta[] = '<meta http-equiv="X-UA-Compatible" content="IE=edge">';
Sak::$meta[] = '<meta name="viewport" content="width=device-width, initial-scale=1">';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment