PHP Class kartik\helpers\Enum

Since: 1.0
Author: Kartik Visweswaran ([email protected])
Inheritance: extends yii\helpers\Inflector
Mostra file Open project: kartik-v/yii2-helpers Class Usage Examples

Public Properties

Property Type Description
$intervals time interval values in seconds

Public Methods

Method Description
array2table ( array $array, boolean $transpose = false, boolean $recursive = false, boolean $typeHint = true, array $tableOptions = ['class' => 'table table-bordered table-striped'], array $keyOptions = [], array $valueOptions = ['style' => 'cursor: default; border-bottom: 1px #aaa dashed;'], string $null = '<span class="not-set">(not set)</span>' ) : string | boolean Convert a PHP array to HTML table.
boolList ( string $false = null, string $true = null ) : array Generates a boolean list.
dateList ( integer $from = 1, integer $to = 31, integer $interval = 1, boolean $intervalFromZero = true, boolean $showLast = true ) : array Generate a date picker array list for Gregorian Calendar.
dayList ( boolean $abbr = false, integer $start = 1, string $case = null ) : array Generate a day array list for Gregorian calendar
days ( ) : array Generate list of days.
formatBytes ( double $bytes, integer $precision = 2 ) : string Format and convert "bytes" to its optimal higher metric unit.
getBrowser ( boolean $common = false, array $browsers = [], string $agent = null ) : array Gets basic browser information
getType ( string $var ) : string Parses and returns a variable type
inArray ( string $needle, array $haystack ) : boolean Check if a value exists in the array. This method is faster in performance than the built in PHP in_array method.
initI18N ( ) Initialize translations
isEmpty ( mixed $var ) : boolean Check if a variable is empty or not set.
monthList ( boolean $abbr = false, integer $start = 1, string $case = null ) : array Generate a month array list for Gregorian calendar
months ( ) : array Generate list of months.
numToWords ( double $num ) : string Number to words conversion. Returns the number converted as an anglicized string.
ones ( ) : array Generate list of ones.
properize ( string $string ) : string Properize a string for possessive punctuation.
tens ( ) : array Generate list of tens.
timeElapsed ( string $fromTime = null, boolean $human = true, string $toTime = null, string $append = null ) : string Get time elapsed (Facebook Style)
timeInterval ( integer $interval, string $append = null, boolean $human = true ) : string Get time interval (Facebook Style) in a human readable format. An output could look like 14 hours ago for human friendly conversion, or 14 hours 7 minutes 55 seconds ago as a raw conversion.
timeList ( string $unit, integer $interval = 1, integer $from, integer $to = null, boolean $padZero = true ) : array Generate a time picker array list.
triplets ( ) : array Generate list of thousand multiples.
userIP ( boolean $filterLocal = true ) : string Gets the user's IP address
yearList ( integer $from, integer $to = null, boolean $keys = false, boolean $desc = true ) : array Generates a list of years.

Protected Methods

Method Description
convertTri ( double $num, integer $tri ) : string Recursive function used in number to words conversion. Converts three digits per pass.
genCalList ( string $unit = 'day', boolean $abbr = false, integer $start = 1, string $case = null ) : array Generate a month or day array list for Gregorian calendar
getBrowserVer ( string $agent, string $code ) : float Returns browser version
time2String ( integer $time, array $intervals ) : string Get elapsed time converted to string

Method Details

array2table() public static method

Example: ~~~ $data = [ ['id' => 1, 'name' => 'John', 'birthday' => '01-Jul-1976', 'commission'=>'4,500.50', 'active' => true], [2, 'Scott', '26-Feb-1980', '1,300.40', true], [3, 'Mary', '1990-02-10', null, false], [4, 'Lisa', '17-Dec-1982', '-900.34', true], ]; echo Enum::array2table($data); ~~~
public static array2table ( array $array, boolean $transpose = false, boolean $recursive = false, boolean $typeHint = true, array $tableOptions = ['class' => 'table table-bordered table-striped'], array $keyOptions = [], array $valueOptions = ['style' => 'cursor: default; border-bottom: 1px #aaa dashed;'], string $null = '<span class="not-set">(not set)</span>' ) : string | boolean
$array array the associative array to be converted
$transpose boolean whether to show keys as rows instead of columns. This parameter should be used only for a single dimensional associative array. If used for a multidimensional array, the sub array will be imploded as text.
$recursive boolean whether to recursively generate tables for multi-dimensional arrays
$typeHint boolean whether to show the data type as a hint
$tableOptions array the HTML attributes for the table
$keyOptions array the HTML attributes for the array key
$valueOptions array the HTML attributes for the array value
$null string the content to display for blank cells
return string | boolean

boolList() public static method

Example: ~~~ print_r(Enum::boolList()); print_r(Enum::boolList('Active', 'Inactive')); ~~~
public static boolList ( string $false = null, string $true = null ) : array
$false string the label for the false value
$true string the label for the true value
return array

convertTri() protected static method

Recursive function used in number to words conversion. Converts three digits per pass.
protected static convertTri ( double $num, integer $tri ) : string
$num double the source number
$tri integer the three digits converted per pass.
return string

dateList() public static method

Example: ~~~ print_r(Enum::dateList(1, 28, 7)); print_r(Enum::dateList(1, 10)); ~~~
public static dateList ( integer $from = 1, integer $to = 31, integer $interval = 1, boolean $intervalFromZero = true, boolean $showLast = true ) : array
$from integer the start day, defaults to 1
$to integer the end day, defaults to 31
$interval integer the date interval, defaults to 1.
$intervalFromZero boolean whether to start incrementing intervals from zero if $from = 1.
$showLast boolean whether to show the last date (set in $to) even if it does not match interval.
return array

dayList() public static method

Generate a day array list for Gregorian calendar
public static dayList ( boolean $abbr = false, integer $start = 1, string $case = null ) : array
$abbr boolean whether to return abbreviated day
$start integer the first day to set. Defaults to `1` for `Sunday`.
$case string whether 'upper', lower', or null. If null, then the initcap case will be used.
return array list of days

days() public static method

Example: ~~~ $days = Enum::days(); echo $days[1]; // returns: Sunday echo $days[2]; // returns: Monday ~~~
public static days ( ) : array
return array

formatBytes() public static method

Example: ~~~ echo Enum::formatBytes(120.32); // returns: 1.17 KB echo Enum::formatBytes(28434322.25); // returns: 27.12 MB echo Enum::formatBytes(17328347842.25, 3); // returns: 16.138 GB ~~~
public static formatBytes ( double $bytes, integer $precision = 2 ) : string
$bytes double number of bytes
$precision integer the number of decimal places to round off
return string

genCalList() protected static method

Generate a month or day array list for Gregorian calendar
protected static genCalList ( string $unit = 'day', boolean $abbr = false, integer $start = 1, string $case = null ) : array
$unit string whether 'day' or 'month'
$abbr boolean whether to return abbreviated day or month
$start integer the first day or month to set. Defaults to `1`.
$case string whether 'upper', lower', or null. If null, then the initcap case will be used.
return array list of days or months

getBrowser() public static method

Example: ~~~ echo Enum::array2table(Enum::getBrowser(), true); ~~~
public static getBrowser ( boolean $common = false, array $browsers = [], string $agent = null ) : array
$common boolean show common browsers only
$browsers array the list of browsers
$agent string user agent
return array the browser information

getBrowserVer() protected static method

Returns browser version
protected static getBrowserVer ( string $agent, string $code ) : float
$agent string the user agent string
$code string the browser string
return float

getType() public static method

Example: ~~~ $data = [ 'id' => 1, 'name' => 'Smith', 'date' => '2014/01/22', 'amount' => '4,323.23', 'relations' => ['spouse', 'children'] ]; foreach ($data as $k=>$v) { echo "$k: " . Enum::getType($v) . "
"; } ~~~
public static getType ( string $var ) : string
$var string the variable to be parsed
return string

inArray() public static method

Example: ~~~ if (Enum::inArray('apple', ['apple', 'mango', 'banana'])) { echo 'apple exists'; } ~~~
public static inArray ( string $needle, array $haystack ) : boolean
$needle string the value to search
$haystack array the array to scan
return boolean

initI18N() public static method

Initialize translations
public static initI18N ( )

isEmpty() public static method

Example: ~~~ if (Enum::isEmpty([]) { echo 'Not empty'; } ~~~
public static isEmpty ( mixed $var ) : boolean
$var mixed variable to perform the check
return boolean

monthList() public static method

Example: ~~~ print_r(Enum::monthList()); print_r(Enum::monthList(true, 4, 'upper')); ~~~
public static monthList ( boolean $abbr = false, integer $start = 1, string $case = null ) : array
$abbr boolean whether to return abbreviated month
$start integer the first month to set. Defaults to `1` for `January`.
$case string whether 'upper', lower', or null. If null, then the initcap case will be used.
return array list of months

months() public static method

Example: ~~~ $months = Enum::months(); echo $months[1]; // returns: January echo $months[2]; // returns: February ~~~
public static months ( ) : array
return array

numToWords() public static method

Example: ~~~ echo Enum::numToWords(21909); returns: twenty one thousand nine hundred and nine ~~~
public static numToWords ( double $num ) : string
$num double the source number
return string

ones() public static method

Example: ~~~ $ones = Enum::ones(); echo $ones[5]; // returns: five echo $ones[9]; // returns: nine echo $ones[0]; // returns: '' (empty string) ~~~
public static ones ( ) : array
return array

properize() public static method

Example: ~~~ echo Enum::properize("Chris"); //returns Chris' echo Enum::properize("David"); //returns David's ~~~
public static properize ( string $string ) : string
$string string input string
return string

tens() public static method

Example: ~~~ $tens = Enum::tens(); echo $tens[2]; // returns: twenty echo $tens[9]; // returns: ninety echo $tens[0]; // returns: '' (empty string) echo $tens[1]; // returns: '' (empty string) ~~~
public static tens ( ) : array
return array

time2String() protected static method

Example Output: 1 year 5 months 3 days ago
protected static time2String ( integer $time, array $intervals ) : string
$time integer elapsed number of seconds
$intervals array configuration of time intervals in seconds
return string

timeElapsed() public static method

Example: ~~~ echo Enum::timeElapsed('21-Jan-2016'); ~~~
public static timeElapsed ( string $fromTime = null, boolean $human = true, string $toTime = null, string $append = null ) : string
$fromTime string start date time
$human boolean if true returns an approximate human friendly output. If set to `false`, will attempt an exact conversion of time intervals.
$toTime string end date time (defaults to current system time)
$append string the string to append for the converted elapsed time. Defaults to ' ago'. Example Output(s): 10 hours ago
return string

timeInterval() public static method

Example: ~~~ echo 'Human Friendly: ' . Enum::timeElapsed("2011-08-30 03:11:39"); echo 'Raw Conversion: ' . Enum::timeElapsed("2011-08-30 03:11:39", false); ~~~
public static timeInterval ( integer $interval, string $append = null, boolean $human = true ) : string
$interval integer time interval in seconds
$append string the string to append for the converted elapsed time. Defaults to `' ago'`.
$human boolean if true returns an approximate human friendly output. If set to `false`, will attempt an exact conversion of time intervals.
return string

timeList() public static method

Example: ~~~ echo implode(', ', Enum::timeList('hour')); echo implode(', ', Enum::timeList('sec', 5)); ~~~
public static timeList ( string $unit, integer $interval = 1, integer $from, integer $to = null, boolean $padZero = true ) : array
$unit string the time unit ('hour', 'min', 'sec', 'ms')
$interval integer the time interval.
$from integer the time from (defaults to 23 for hour
$to integer the time to (defaults to 1).
$padZero boolean whether to pad zeros to the left of each time unit value.
return array

triplets() public static method

Example: ~~~ $triplets = Enum::days(); echo $triplets[1]; // returns: thousand echo $triplets[2]; // returns: billion echo $triplets[0]; // returns: '' (empty string) ~~~
public static triplets ( ) : array
return array

userIP() public static method

Example: ~~~ echo Enum::userIP(); echo Enum::userIP(false); ~~~
public static userIP ( boolean $filterLocal = true ) : string
$filterLocal boolean whether to filter local & LAN IP (defaults to true)
return string

yearList() public static method

Example: ~~~ $years = Enum::yearList(2000, 2013); // years from 2000 to 2013 echo $years[0]; // returns 2013 echo $years[12]; // returns 2000 $years = Enum::yearList(2000, 2013, false, false); // years from 2000 to 2013 echo $years[0]; // returns 2000 echo $years[12]; // returns 2013 ~~~
public static yearList ( integer $from, integer $to = null, boolean $keys = false, boolean $desc = true ) : array
$from integer the start year
$to integer the end year
$keys boolean whether to set the array keys same as the values (defaults to false)
$desc boolean whether to sort the years descending (defaults to true)
return array

Property Details

$intervals public_oe static_oe property

time interval values in seconds
public static $intervals