Skip to content

Commit

Permalink
Updates to release v1.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Dec 11, 2015
1 parent 0b0579b commit a6be6bc
Show file tree
Hide file tree
Showing 5 changed files with 529 additions and 604 deletions.
8 changes: 8 additions & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Change Log: `yii2-helpers`
==========================

## Version 1.3.4

**Date:** 12-Dec-2015

- (enh #27): Add Ukranian Translations.
- (enh #28): Add Italian Translations.
- (enh #29): Code style and formatting enhancements.

## Version 1.3.3

**Date:** 22-Oct-2015
Expand Down
135 changes: 71 additions & 64 deletions Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Inflector;

/**
* Collection of useful helper functions for Yii Applications
Expand All @@ -18,9 +19,11 @@
* @since 1.0
*
*/
class Enum extends \yii\helpers\Inflector
class Enum extends Inflector
{
/* time intervals in seconds */
/**
* @var array time intervals in seconds
*/
public static $intervals = [
'year' => 31556926,
'month' => 2629744,
Expand All @@ -34,18 +37,17 @@ class Enum extends \yii\helpers\Inflector
/**
* Check if a variable is empty or not set.
*
* @param reference $var variable to perform the check
* @param mixed $var variable to perform the check
*
* @return boolean
*/
public static function isEmpty(&$var)
public static function isEmpty($var)
{
return is_array($var) ? empty($var) : (!isset($var) || (strlen($var) == 0));
return !isset($var) ? true : (is_array($var) ? empty($var) : ($var === null || $var === ''));
}

/**
* Check if a value exists in the array. This method is faster
* in performance than the built in PHP in_array method.
* Check if a value exists in the array. This method is faster in performance than the built in PHP in_array method.
*
* @param string $needle the value to search
* @param array $haystack the array to scan
Expand All @@ -60,11 +62,16 @@ public static function inArray($needle, $haystack)

/**
* Properize a string for possessive punctuation.
* e.g.
* properize("Chris"); //returns Chris'
* properize("David"); //returns David's
*
* @param string $string input string
*
* Example:
* ~~~
* properize("Chris"); //returns Chris'
* properize("David"); //returns David's
* ~~~
*
* @return string
*/
public static function properize($string)
{
Expand All @@ -75,15 +82,15 @@ public static function properize($string)
/**
* Get time elapsed (Facebook Style)
*
* @param string $fromTime start date time
* @param bool $human if true returns an approximate human friendly output. If set to `false`, will attempt an
* exact conversion of time intervals.
* @param string $toTime end date time (defaults to current system time)
* @param string $append the string to append for the converted elapsed time. Defaults to ' ago'.
*
* Example Output(s):
* 10 hours ago
*
* @param string $fromTime start date time
* @param boolean $human if true returns an approximate human friendly output. If set to `false`,
* will attempt an exact conversion of time intervals.
* @param string $toTime end date time (defaults to current system time)
* @param string $append the string to append for the converted elapsed time. Defaults to ' ago'.
*
* @return string
*/
public static function timeElapsed($fromTime = null, $human = true, $toTime = null, $append = null)
Expand All @@ -98,16 +105,16 @@ public static function timeElapsed($fromTime = null, $human = true, $toTime = nu
/**
* Get time interval (Facebook Style)
*
* @param int $interval time interval in seconds
* @param string $append the string to append for the converted elapsed time. Defaults to ' ago'.
* @param bool $human if true returns an approximate human friendly output. If set to `false`, will attempt an
* exact conversion of time intervals.
*
* Example Output(s):
* 10 hours ago
*
* @param int $interval time interval in seconds
* @param string $append the string to append for the converted elapsed time. Defaults to ' ago'.
* @param boolean $human if true returns an approximate human friendly output. If set to `false`,
* will attempt an exact conversion of time intervals.
*
* @return string
*/
*/
public static function timeInterval($interval, $append = null, $human = true)
{
static::initI18N();
Expand All @@ -121,12 +128,13 @@ public static function timeInterval($interval, $append = null, $human = true)
if ($interval <= 0) {
$elapsed = Yii::t('kvenum', 'a moment ago');
} elseif ($interval < 60) {
$elapsed = Yii::t('kvenum', '{n, plural, one{one second} other{# seconds}}',
['n' => $interval]) . $append;
$elapsed = Yii::t('kvenum', '{n, plural, one{one second} other{# seconds}}', [
'n' => $interval
]) . $append;
} elseif ($interval >= 60 && $interval < $intervals['hour']) {
$interval = floor($interval / $intervals['minute']);
$elapsed = Yii::t('kvenum', '{n, plural, one{one minute} other{# minutes}}',
['n' => $interval]) . $append;
$elapsed = Yii::t('kvenum', '{n, plural, one{one minute} other{# minutes}}', ['n' => $interval]) .
$append;
} elseif ($interval >= $intervals['hour'] && $interval < $intervals['day']) {
$interval = floor($interval / $intervals['hour']);
$elapsed = Yii::t('kvenum', '{n, plural, one{one hour} other{# hours}}', ['n' => $interval]) . $append;
Expand All @@ -138,8 +146,8 @@ public static function timeInterval($interval, $append = null, $human = true)
$elapsed = Yii::t('kvenum', '{n, plural, one{one week} other{# weeks}}', ['n' => $interval]) . $append;
} elseif ($interval >= $intervals['month'] && $interval < $intervals['year']) {
$interval = floor($interval / $intervals['month']);
$elapsed = Yii::t('kvenum', '{n, plural, one{one month} other{# months}}',
['n' => $interval]) . $append;
$elapsed = Yii::t('kvenum', '{n, plural, one{one month} other{# months}}', ['n' => $interval]) .
$append;
} elseif ($interval >= $intervals['year']) {
$interval = floor($interval / $intervals['year']);
$elapsed = Yii::t('kvenum', '{n, plural, one{one year} other{# years}}', ['n' => $interval]) . $append;
Expand Down Expand Up @@ -172,17 +180,17 @@ public static function initI18N()
* Example Output:
* 1 year 5 months 3 days ago
*
* @param integer $timeline elapsed number of seconds
* @param array $intervals configuration of time intervals in seconds
* @param int $time elapsed number of seconds
* @param array $intervals configuration of time intervals in seconds
*
* @return string
*/
protected static function time2String($timeline, $intervals)
protected static function time2String($time, $intervals)
{
$output = '';
foreach ($intervals AS $name => $seconds) {
$num = floor($timeline / $seconds);
$timeline -= ($num * $seconds);
foreach ($intervals as $name => $seconds) {
$num = floor($time / $seconds);
$time -= ($num * $seconds);
if ($num > 0) {
$output .= $num . ' ' . $name . (($num > 1) ? 's' : '') . ' ';
}
Expand Down Expand Up @@ -296,11 +304,10 @@ public static function numToWords($num)
}

/**
* Recursive function used in number to words conversion.
* Converts three digits per pass.
* Recursive function used in number to words conversion. Converts three digits per pass.
*
* @param double $num the source number
* @param double $tri the three digits converted per pass.
* @param int $tri the three digits converted per pass.
*
* @return string
*/
Expand Down Expand Up @@ -329,8 +336,7 @@ protected static function convertTri($num, $tri)
$str .= $tens[(int)($y / 10)] . $ones[$y % 10];
}

// add triplet modifier only if there
// is some output to be modified...
// add triplet modifier only if there is some output to be modified...
if ($str != "") {
$str .= $triplets[$tri];
}
Expand Down Expand Up @@ -491,13 +497,13 @@ public static function yearList($from, $to = null, $keys = false, $desc = true)
/**
* Generate a month or day array list for Gregorian calendar
*
* @param string $unit whether 'day' or 'month'
* @param boolean $abbr whether to return abbreviated day or month
* @param boolean $start the first day or month to set. Defaults to `1`.
* @param string $case whether 'upper', lower', or null. If null, then
* the initcap case will be used.
* @param string $unit whether 'day' or 'month'
* @param bool $abbr whether to return abbreviated day or month
* @param int $start the first day or month to set. Defaults to `1`.
* @param string $case whether 'upper', lower', or null. If null, then the initcap case will be used.
*
* @return array list of days or months
* @throws InvalidConfigException
*/
protected static function genCalList($unit = 'day', $abbr = false, $start = 1, $case = null)
{
Expand Down Expand Up @@ -530,10 +536,9 @@ protected static function genCalList($unit = 'day', $abbr = false, $start = 1, $
/**
* Generate a month array list for Gregorian calendar
*
* @param boolean $abbr whether to return abbreviated month
* @param boolean $start the first month to set. Defaults to `1` for `January`.
* @param string $case whether 'upper', lower', or null. If null, then
* the initcap case will be used.
* @param bool $abbr whether to return abbreviated month
* @param int $start the first month to set. Defaults to `1` for `January`.
* @param string $case whether 'upper', lower', or null. If null, then the initcap case will be used.
*
* @return array list of months
*/
Expand All @@ -545,10 +550,9 @@ public static function monthList($abbr = false, $start = 1, $case = null)
/**
* Generate a day array list for Gregorian calendar
*
* @param boolean $abbr whether to return abbreviated day
* @param boolean $start the first day to set. Defaults to `1` for `Sunday`.
* @param string $case whether 'upper', lower', or null. If null, then
* the initcap case will be used.
* @param bool $abbr whether to return abbreviated day
* @param int $start the first day to set. Defaults to `1` for `Sunday`.
* @param string $case whether 'upper', lower', or null. If null, then the initcap case will be used.
*
* @return array list of days
*/
Expand All @@ -560,11 +564,11 @@ public static function dayList($abbr = false, $start = 1, $case = null)
/**
* Generate a date picker array list for Gregorian Calendar.
*
* @param integer $from the start day, defaults to 1
* @param integer $to the end day, defaults to 31
* @param integer $interval the date interval, defaults to 1.
* @param integer $intervalFromZero whether to start incrementing intervals from zero if $from = 1.
* @param integer $showLast whether to show the last date (set in $to) even if it does not match interval.
* @param int $from the start day, defaults to 1
* @param int $to the end day, defaults to 31
* @param int $interval the date interval, defaults to 1.
* @param bool $intervalFromZero whether to start incrementing intervals from zero if $from = 1.
* @param bool $showLast whether to show the last date (set in $to) even if it does not match interval.
*
* @return array
* @throws InvalidConfigException
Expand Down Expand Up @@ -734,7 +738,7 @@ public static function array2table(

if ($recursive === true && is_array($cell) && !empty($cell)) {
// Recursive mode
$table .= "\n" . array2table($cell, true, true) . "\n";
$table .= "\n" . static::array2table($cell, true, true) . "\n";
} else {
if (!is_null($cell) && is_bool($cell)) {
$val = $cell ? 'true' : 'false';
Expand Down Expand Up @@ -776,8 +780,8 @@ public static function getType($var)
return 'NULL';
} elseif (is_bool($var)) {
return 'boolean';
} elseif (is_float($var) || (is_numeric(str_replace(',', '', $var)) && strpos($var,
'.') > 0 && is_float((float)str_replace(',', '', $var)))
} elseif (is_float($var) || (is_numeric(str_replace(',', '', $var)) && strpos($var, '.') > 0 &&
is_float((float)str_replace(',', '', $var)))
) {
return 'float';
} elseif (is_int($var) || (is_numeric($var) && is_int((int)$var))) {
Expand Down Expand Up @@ -812,8 +816,11 @@ public static function userIP($filterLocal = true)
if (array_key_exists($key, $_SERVER) === true) {
foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) {
if ($filterLocal) {
$checkFilter = filter_var($ip, FILTER_VALIDATE_IP,
FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
$checkFilter = filter_var(
$ip,
FILTER_VALIDATE_IP,
FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
);
if ($checkFilter !== false) {
return $ip;
}
Expand Down Expand Up @@ -921,8 +928,8 @@ public static function getBrowser($common = false, $browsers = [], $agent = null
/**
* Returns browser version
*
* @param string $agent
* @param browser $code
* @param string $agent the user agent string
* @param string $code the browser string
*
* @return float
*/
Expand Down
Loading

0 comments on commit a6be6bc

Please sign in to comment.