From 8f431ef8af6c83aa9ebdcd8a2dd6252759127de1 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Thu, 21 Jul 2011 14:14:40 +0200 Subject: (Almost) finished DB-Models / abstraction layer. --- ratatoeskr/sys/utils.php | 164 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 ratatoeskr/sys/utils.php (limited to 'ratatoeskr/sys/utils.php') diff --git a/ratatoeskr/sys/utils.php b/ratatoeskr/sys/utils.php new file mode 100644 index 0000000..de673aa --- /dev/null +++ b/ratatoeskr/sys/utils.php @@ -0,0 +1,164 @@ + 0) + { + for($i = 0; $i < count($arrays); ++$i) + { + $val = array_shift($arrays[$i]); + if($val === NULL) + continue; + array_push($rv, $val); + } + } + return $rv; + break; + } +} + +/* + * Function: array_filter_empty + * + * Filters all empty elements out of an array. + * + * Parameters: + * + * $input - The input array + * + * Returns: + * + * The $input without its empty elements. + */ +function array_filter_empty($input) +{ + return array_filter($input, function($x){return !empty($x);}); +} + +/* + * Function: array_filter_keys + * + * Like PHPs `array_filter`, but callback will get the key, not the value of the array element. + */ +function array_filter_keys($input, $callback) +{ + if(!is_array($input)) + throw new InvalidArgumentException("Argument 1 must be an array"); + if(empty($input)) + return array(); + $delete_keys = array_filter(array_keys($input), function ($x) use ($callback) { return !$callback($x);}); + foreach($delete_keys as $key) + unset($input[$key]); + return $input; +} + +/* + * Function: ucount + * + * Count elements of an array matching unser-defined rules. + * + * Parameters: + * $array - The input array. + * $callback - A callback function. It will be called with the current value as the only parameter. The value is counted, if callback returns TRUE. + * + * Returns: + * + * Number of elements where $callback returned TRUE. + */ +function ucount($array, $callback) +{ + return count(array_filter($array, $callback)); +} + +/* + * Function: vcount + * + * Counts how often $value appears in $array. + * + * Parameters: + * + * $array - + * $value - + * + * Returns: + * + * How often $value appears in $array. + */ +function vcount($array, $value) +{ + return ucount($array, function($x){return $x===$value;}); +} + +/* + * Function: self_url + * + * Gets current URL. + * + * From: http://dev.kanngard.net/Permalinks/ID_20050507183447.html + */ +function self_url() { + $s = empty($_SERVER["HTTPS"]) ? '' + : ($_SERVER["HTTPS"] == "on") ? "s" + : ""; + $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; + $port = ($_SERVER["SERVER_PORT"] == "80") ? "" + : (":".$_SERVER["SERVER_PORT"]); + return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; +} +function strleft($s1, $s2) { + return substr($s1, 0, strpos($s1, $s2)); +} + +/* + * Constant: SITE_BASE_PATH + * The Base path of this ratatoeskr site. + */ +define(SITE_BASE_PATH, dirname(dirname(dirname(__FILE__)))); +?> -- cgit v1.2.3-54-g00ecf