Skip to content

Instantly share code, notes, and snippets.

@michallohnisky
Created September 18, 2015 05:43
Show Gist options
  • Save michallohnisky/0d07cb4b0e5597b49d78 to your computer and use it in GitHub Desktop.
Save michallohnisky/0d07cb4b0e5597b49d78 to your computer and use it in GitHub Desktop.
PHP funkce
<?php
namespace App;
use Nette\Forms\Controls;
class Utils {
/**
* Naformatuje cislo jako menu.
* @param integer
* @param ActiveRow
* @param bool Zda se ma pridat symbol meny (napr.: 'Kc', '$', '€', ...)
* @return string
*/
public static function priceFormat($price, $currency, $withSymbol = TRUE) {
if ($currency === FALSE)
return '';
if (empty($price))
$price = 0;
$nulls = 2;
if (floor($price) == floor($price * 100) / 100)
$nulls = $currency->nulls;
$price = static::numberFormat($price, $nulls, $currency->dSep, $currency->tSep);
if ($withSymbol) {
if ($currency->position == 'after')
$price = $price . $currency->sSep . $currency->symbol;
else
$price = $currency->symbol . $currency->sSep . $price;
}
return $price;
}
/**
* Funguje jako number_format, ale místo -0.00 zobrazí 0.00.
* @param float $number
* @param integer $decimals
* @param string $dec_point
* @param string $thousands_sep
* @return string
*/
public static function numberFormat($number, $decimals = 0, $dec_point = '.', $thousands_sep = ',') {
if (round($number, $decimals) == 0)
$number = 0;
return number_format($number, $decimals, $dec_point, $thousands_sep);
}
/**
* Funguje jako array_unshift, ale umí zadat i klíč.
* @param array $arr
* @param mixed $key
* @param mixed $val
*/
public static function array_unshift_assoc(&$arr, $key, $val) {
if (is_null($arr))
return FALSE;
$arr = array_reverse($arr, true);
$arr[$key] = $val;
$arr = array_reverse($arr, true);
return count($arr);
}
/**
* Převede rodné číslo na DateTime
* @param string $nin
* @return \Nette\Utils\DateTime
*/
public static function ninToDate($nin) {
// "be liberal in what you receive"
if (!preg_match('#^\s*(\d\d)(\d\d)(\d\d)[ /]*(\d\d\d)(\d?)\s*$#', $nin, $matches)) {
return NULL;
}
list(, $year, $month, $day, $ext, $c) = $matches;
$year += $year < 54 ? 2000 : 1900;
// k měsíci může být připočteno 20, 50 nebo 70
if ($month > 70 && $year > 2003) $month -= 70;
elseif ($month > 50) $month -= 50;
elseif ($month > 20 && $year > 2003) $month -= 20;
$date = (new \Nette\Utils\DateTime('midnight'))->setDate($year, $month, $day);
return $date ? $date : NULL;
}
/**
* Funguje podobně jako tempnam. Vytvoří složku s unikátním názvem.
* @param string $dir
* @param string $prefix
* @return string|FALSE
*/
public static function tempnamDir($dir, $prefix) {
$tempfile = tempnam($dir, $prefix);
if (file_exists($tempfile))
unlink($tempfile);
mkdir($tempfile);
return $tempfile;
}
/**
* Remove dir recursive
*/
public static function rmdir_r($dirName) {
if (is_dir($dirName)) {
foreach (glob($dirName . '/*') as $file) {
if (is_dir($file))
self::rmdir_r($file);
else
unlink($file);
}
rmdir($dirName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment