Commits

WaveHack  committed aa77358

Initial import

  • Participants

Comments (0)

Files changed (2)

+WCH_General
+===========
+*WaveHack's CodeIgniter Helper - General functions*
+
+Collection of handy PHP functions I've gathered and built over the years.
+
+Meant for use with EllisLab's CodeIgniter PHP framework, but will also work without CodeIgniter as a file include.
+
+Installation
+------------
+Copy file 'wch_general_helper.php' to:
+    ``application/helpers/wch_general_helper.php``
+And load the helper with:
+    ``$this->load->helper('wch_general');``
+
+Legal
+-----
+WCH_General is licensed under the `DBAD Public License <http://www.dbad-license.org/>`_.

File wch_general_helper.php

+<?php
+/**
+ * WaveHack's CodeIgniter Helper - General functions
+ *
+ * Collection of handy PHP functions I've gathered and built over the years.
+ *
+ * Meant for use with EllisLab's CodeIgniter PHP framework, but will also work
+ * without CodeIgniter as a file include.
+ *
+ * @author  WaveHack <email@wavehack.net>
+ * @link    https://bitbucket.org/WaveHack/wch_general
+ * @license http://www.dbad-license.org/ DBAD Public License
+ * @package WHCI\Helpers\General
+ */
+
+if (!function_exists('n_var')) {
+	/**
+	 * Ternary not-equal-to wrapper function.
+	 *
+	 * Returns $var if $var is not equal to $value or $return otherwise. The
+	 * checks are explicitly done with the '!==' comparison operator (not '!=').
+	 *
+	 * @param  mixed $var    Variable to check.
+	 * @param  mixed $value  Value to check.
+	 * @param  mixed $return Return value if $var is not equal to $value.
+	 * @return mixed         $var if $var equals to $value, $return otherwise.
+	 */
+	function n_var($var, $value, $return = null)
+	{
+		return (($var !== $value) ? $var : $return);
+	}
+}
+
+if (!function_exists('nempty_var')) {
+	/**
+	 * Ternary 'empty'-wrapper function.
+	 *
+	 * Returns $var if $var is not empty or $return otherwise.
+	 *
+	 * @param  mixed $var    Variable to check.
+	 * @param  mixed $return Return value if $var is empty.
+	 * @return mixed         $var if $var is not empty, $return otherwise.
+	 */
+	function nempty_var($var, $return = null)
+	{
+		return n_var($var, '', $return);
+	}
+}
+
+if (!function_exists('nfalse_var')) {
+	/**
+	 * Ternary '!== false'-wrapper function.
+	 *
+	 * Returns $var if $var is not boolean false or $return otherwise.
+	 *
+	 * @param  mixed $var    Variable to check.
+	 * @param  mixed $return Return value if $var is false.
+	 * @return mixed         $var if $var is not false, $return otherwise.
+	 */
+	function nfalse_var($var, $return = null)
+	{
+		return n_var($var, false, $return);
+	}
+}
+
+if (!function_exists('nnull_var')) {
+	/**
+	 * Ternary '!== null'-wrapper function.
+	 *
+	 * Returns $var if $var is not equal to NULL or $return otherwise.
+	 *
+	 * @param  mixed $var    Variable to check.
+	 * @param  mixed $return Return value if $var is null.
+	 * @return mixed         $var is $var is not null, $return otherwise.
+	 */
+	function nnull_var($var, $return = 'NULL')
+	{
+		return n_var($var, null, $return);
+	}
+}
+
+if (!function_exists('isset_var')) {
+	/**
+	 * Wrapper for 'nnull_var'.
+	 *
+	 * Returns $var is $var is set (i.e. not null) or $return otherwise.
+	 *
+	 * Because isset() is a special PHP language function, either the error
+	 * suppression operator (@) must be used on either the function call to
+	 * isset_var or on the parameter which may not exist, or error_reporting be
+	 * set to not induce notices. Recommended is to use the @ operator on the
+	 * variable (parameter).
+	 *
+	 * Because you're really passing a null value in the $var parameter when
+	 * you call this function with a non-existant parameter, this is only really
+	 * a wrapper for nnull_var(). But this function is more semantically correct
+	 * when checking to see if variables exist or not.
+	 *
+	 * @param  mixed $var    Variable to check.
+	 * @param  mixed $return Return value if $var is null (not set).
+	 * @return mixed         $var if $var is not null, $return otherwise.
+	 */
+	function isset_var($var, $return = null)
+	{
+		return nnull_var($var, $return);
+	}
+}
+
+if (!function_exists('array_extend')) {
+	/**
+	 * Recursive function for extending associative arrays.
+	 *
+	 * This function works like array_merge_recursive, but overwrites array
+	 * values instead of creating a new array at the value spot containing the
+	 * conflicting values.
+	 *
+	 * This function is mainly for extending configuration arrays, but can be
+	 * used for other purposes as well where you need to merge two or more
+	 * arrays whilst preserving key=>value pairs.
+	 *
+	 * @param  array $array1 Initial array to extend.
+	 * @param  array ...     Variable list of arrays to extend.
+	 * @return array         Returns the resulting array.
+	 */
+	function array_extend(array $array1)
+	{
+		$arrays = func_get_args();
+
+		// Remove $array1, since we want to loop through the other arrays only
+		array_shift($arrays);
+
+		foreach ($arrays as $array) {
+			if (!is_array($array)) {
+				continue;
+			}
+
+			foreach ($array as $k => $v) {
+				$array1[$k] = (
+					(is_array($v) && isset($array1[$k]))
+						? array_extend($array1[$k], $v)
+						: $v
+				);
+			}
+		}
+
+		return $array1;
+	}
+}
+
+if (!function_exists('number_format_rtrim')) {
+	/**
+	 * Wrapper for number_format() which rtrims zeroes.
+	 *
+	 * Removes zeroes from the right until a non-zero digit is found. If all the
+	 * decimals are zero (i.e. integer value), it also removes the decimal
+	 * point.
+	 *
+	 * @param  float  $number        The number being formatted.
+	 * @param  int    $decimals      Sets the number of decimal points.
+	 * @param  string $dec_point     Sets the separator for the decimal point.
+	 * @param  string $thousands_sep Sets the thousands separator.
+	 * @return string                A formatted version of $number, rtrimmed of any zeroes.
+	 */
+	function number_format_rtrim($number, $decimals, $dec_point = '.', $thousands_sep = ',')
+	{
+		return rtrim(rtrim(number_format($number, $decimals, $dec_point, $thousands_sep), '0'), $dec_point);
+	}
+}