Class kartik\helpers\Enum
Inheritance | kartik\helpers\Enum » yii\helpers\Inflector » yii\helpers\BaseInflector |
---|---|
Available since version | 1.0 |
Enum is a collection of useful helper methods that can be used across various PHP applications. It extends the yii\helpers\Inflector class, which pluralizes and singularizes English nouns. In addition, this extension provides methods to properize string for possessive punctuation, convert number to words, format bytes, human friendly time elapsed and other useful methods.
For example:
use kartik\helpers\Enum;
// examples of usage
echo Enum::properize('Chris');
echo Enum::timeElapsed('2011-08-30 03:11:39');
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$intervals | array | Time interval values in seconds | kartik\helpers\Enum |
$plurals | array | The rules for converting a word into its plural form. | yii\helpers\BaseInflector |
$singulars | array | The rules for converting a word into its singular form. | yii\helpers\BaseInflector |
$specials | array | The special rules for converting a word between its plural form and singular form. | yii\helpers\BaseInflector |
$transliteration | array | Fallback map for transliteration used by transliterate() when intl isn't available. | yii\helpers\BaseInflector |
$transliterator | mixed | Either a \Transliterator, or a string from which a \Transliterator can be built for transliteration. | yii\helpers\BaseInflector |
Public Methods
Method | Description | Defined By |
---|---|---|
array2table() | Convert a PHP array to HTML table. | kartik\helpers\Enum |
boolList() | Generates a boolean list. | kartik\helpers\Enum |
camel2id() | Converts a CamelCase name into an ID in lowercase. | yii\helpers\BaseInflector |
camel2words() | Converts a CamelCase name into space-separated words. | yii\helpers\BaseInflector |
camelize() | Returns given word as CamelCased. | yii\helpers\BaseInflector |
classify() | Converts a table name to its class name. | yii\helpers\BaseInflector |
dateList() | Generate a date picker array list for Gregorian Calendar. | kartik\helpers\Enum |
dayList() | Generate a day array list for Gregorian calendar | kartik\helpers\Enum |
days() | Generate list of days. | kartik\helpers\Enum |
formatBytes() | Format and convert "bytes" to its optimal higher metric unit. | kartik\helpers\Enum |
getBrowser() | Gets basic browser information | kartik\helpers\Enum |
getType() | Parses and returns a variable type | kartik\helpers\Enum |
humanize() | Returns a human-readable string from $word. | yii\helpers\BaseInflector |
id2camel() | Converts an ID into a CamelCase name. | yii\helpers\BaseInflector |
inArray() | Check if a value exists in the array. This method is faster in performance than the built in PHP in_array method. | kartik\helpers\Enum |
initI18N() | Initialize translations | kartik\helpers\Enum |
isEmpty() | Check if a variable is empty or not set. | kartik\helpers\Enum |
monthList() | Generate a month array list for Gregorian calendar | kartik\helpers\Enum |
months() | Generate list of months. | kartik\helpers\Enum |
numToWords() | Number to words conversion. Returns the number converted as an anglicized string. | kartik\helpers\Enum |
ones() | Generate list of ones. | kartik\helpers\Enum |
ordinalize() | Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd . | yii\helpers\BaseInflector |
pluralize() | Converts a word to its plural form. | yii\helpers\BaseInflector |
properize() | Properize a string for possessive punctuation. | kartik\helpers\Enum |
sentence() | Converts a list of words into a sentence. | yii\helpers\BaseInflector |
singularize() | Returns the singular of the $word. | yii\helpers\BaseInflector |
slug() | Returns a string with all spaces converted to given replacement, non word characters removed and the rest of characters transliterated. | yii\helpers\BaseInflector |
tableize() | Converts a class name to its table name (pluralized) naming conventions. | yii\helpers\BaseInflector |
tens() | Generate list of tens. | kartik\helpers\Enum |
timeElapsed() | Get time elapsed (Facebook Style) | kartik\helpers\Enum |
timeInterval() | 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. |
kartik\helpers\Enum |
timeList() | Generate a time picker array list. | kartik\helpers\Enum |
titleize() | Converts an underscored or CamelCase word into a English sentence. | yii\helpers\BaseInflector |
transliterate() | Returns transliterated version of a string. | yii\helpers\BaseInflector |
triplets() | Generate list of thousand multiples. | kartik\helpers\Enum |
underscore() | Converts any "CamelCased" into an "underscored_word". | yii\helpers\BaseInflector |
userIP() | Gets the user's IP address | kartik\helpers\Enum |
variablize() | Same as camelize but first char is in lowercase. | yii\helpers\BaseInflector |
yearList() | Generates a list of years. | kartik\helpers\Enum |
Protected Methods
Method | Description | Defined By |
---|---|---|
convertTri() | Recursive function used in number to words conversion. Converts three digits per pass. | kartik\helpers\Enum |
genCalList() | Generate a month or day array list for Gregorian calendar | kartik\helpers\Enum |
getBrowserVer() | Returns browser version | kartik\helpers\Enum |
hasIntl() | yii\helpers\BaseInflector | |
time2String() | Get elapsed time converted to string | kartik\helpers\Enum |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
TRANSLITERATE_LOOSE | 'Any-Latin; Latin-ASCII; [\u0080-\uffff] remove' | Shortcut for Any-Latin; Latin-ASCII; [\u0080-\uffff] remove transliteration rule.
The rule is loose,
letters will be transliterated with the characters of Basic Latin Unicode Block.
For example:
获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español? will be transliterated to
huo qu dao dochira Ukrainska: g,e, Srpska: d, n, d! Espanol? .
Used in transliterate().
For detailed information see unicode normalization forms |
yii\helpers\BaseInflector |
TRANSLITERATE_MEDIUM | 'Any-Latin; Latin-ASCII' | Shortcut for Any-Latin; Latin-ASCII transliteration rule.
The rule is medium, letters will be
transliterated to characters of Latin-1 (ISO 8859-1) ASCII table. For example:
获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español? will be transliterated to
huo qu dao dochira Ukrainsʹka: g,e, Srpska: d, n, d! ¿Espanol? .
Used in transliterate().
For detailed information see unicode normalization forms |
yii\helpers\BaseInflector |
TRANSLITERATE_STRICT | 'Any-Latin; NFKD' | Shortcut for Any-Latin; NFKD transliteration rule.
The rule is strict, letters will be transliterated with
the closest sound-representation chars. The result may contain any UTF-8 chars. For example:
获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español? will be transliterated to
huò qǔ dào dochira Ukraí̈nsʹka: g̀,ê, Srpska: đ, n̂, d̂! ¿Español? .
Used in transliterate().
For detailed information see unicode normalization forms |
yii\helpers\BaseInflector |
Property Details
Time interval values in seconds
'year' => 31556926, 'month' => 2629744, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'minute' => 60, 'second' => 1
]
Method Details
Convert a PHP array to HTML table.
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 string|boolean array2table ( $array, $transpose = false, $recursive = false, $typeHint = true, $tableOptions = [ 'class' => 'table table-bordered table-striped' ], $keyOptions = [], $valueOptions = [ 'style' => 'cursor: default; border-bottom: 1px #aaa dashed;' ], $null = '<span class="not-set">(not set)</span>' ) | ||
$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 |
Generates a boolean list.
Example:
print_r(Enum::boolList());
print_r(Enum::boolList('Active', 'Inactive'));
public static array boolList ( $false = null, $true = null ) | ||
$false | string | The label for the false value |
$true | string | The label for the true value |
Recursive function used in number to words conversion. Converts three digits per pass.
protected static string convertTri ( $num, $tri ) | ||
$num | double | The source number |
$tri | integer | The three digits converted per pass. |
Generate a date picker array list for Gregorian Calendar.
Example:
print_r(Enum::dateList(1, 28, 7));
print_r(Enum::dateList(1, 10));
public static array dateList ( $from = 1, $to = 31, $interval = 1, $intervalFromZero = true, $showLast = true ) | ||
$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. |
throws | yii\base\InvalidConfigException |
---|
Generate a day array list for Gregorian calendar
public static array dayList ( $abbr = false, $start = 1, $case = null ) | ||
$abbr | boolean | Whether to return abbreviated day |
$start | integer | The first day to set. Defaults to |
$case | string | Whether 'upper', lower', or null. If null, then the initcap case will be used. |
return | array | List of days |
---|---|---|
throws | yii\base\InvalidConfigException |
Generate list of days.
Example:
$days = Enum::days();
echo $days[1]; // returns: Sunday
echo $days[2]; // returns: Monday
public static array days ( ) |
Format and convert "bytes" to its optimal higher metric unit.
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 string formatBytes ( $bytes, $precision = 2 ) | ||
$bytes | double | Number of bytes |
$precision | integer | The number of decimal places to round off |
Generate a month or day array list for Gregorian calendar
protected static array genCalList ( $unit = 'day', $abbr = false, $start = 1, $case = null ) | ||
$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 |
$case | string | Whether 'upper', lower', or null. If null, then the initcap case will be used. |
return | array | List of days or months |
---|---|---|
throws | yii\base\InvalidConfigException |
Gets basic browser information
Example:
echo Enum::array2table(Enum::getBrowser(), true);
public static array getBrowser ( $common = false, $browsers = [], $agent = null ) | ||
$common | boolean | Show common browsers only |
$browsers | array | The list of browsers |
$agent | string | User agent |
return | array | The browser information |
---|
Returns browser version
protected static float getBrowserVer ( $agent, $code ) | ||
$agent | string | The user agent string |
$code | string | The browser string |
Parses and returns a variable type
Example:
$data = [
'id' => 1,
'name' => 'Smith',
'date' => '2014/01/22',
'amount' => '4,323.23',
'relations' => ['spouse', 'children']
];
foreach ($data as $k=>$v) {
echo "<b>$k</b>: " . Enum::getType($v) . "<br>";
}
public static string getType ( $var ) | ||
$var | string | The variable to be parsed |
Check if a value exists in the array. This method is faster in performance than the built in PHP in_array method.
Example:
if (Enum::inArray('apple', ['apple', 'mango', 'banana'])) {
echo 'apple exists';
}
public static boolean inArray ( $needle, $haystack ) | ||
$needle | string | The value to search |
$haystack | array | The array to scan |
Initialize translations
public static void initI18N ( ) |
Check if a variable is empty or not set.
Example:
if (Enum::isEmpty([]) {
echo 'Not empty';
}
public static boolean isEmpty ( $var ) | ||
$var | mixed | Variable to perform the check |
Generate a month array list for Gregorian calendar
Example:
print_r(Enum::monthList());
print_r(Enum::monthList(true, 4, 'upper'));
public static array monthList ( $abbr = false, $start = 1, $case = null ) | ||
$abbr | boolean | Whether to return abbreviated month |
$start | integer | The first month to set. Defaults to |
$case | string | Whether 'upper', lower', or null. If null, then the initcap case will be used. |
return | array | List of months |
---|---|---|
throws | yii\base\InvalidConfigException |
Generate list of months.
Example:
$months = Enum::months();
echo $months[1]; // returns: January
echo $months[2]; // returns: February
public static array months ( ) |
Number to words conversion. Returns the number converted as an anglicized string.
Example:
echo Enum::numToWords(21909);
// returns: twenty one thousand nine hundred and nine
public static string numToWords ( $num ) | ||
$num | double | The source number |
Generate list of ones.
Example:
$ones = Enum::ones();
echo $ones[5]; // returns: five
echo $ones[9]; // returns: nine
echo $ones[0]; // returns: '' (empty string)
public static array ones ( ) |
Properize a string for possessive punctuation.
Example:
echo Enum::properize("Chris"); //returns Chris'
echo Enum::properize("David"); //returns David's
public static string properize ( $string ) | ||
$string | string | Input string |
Generate list of tens.
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 array tens ( ) |
Get elapsed time converted to string
Example Output: 1 year 5 months 3 days ago
protected static string time2String ( $time, $intervals ) | ||
$time | integer | Elapsed number of seconds |
$intervals | array | Configuration of time intervals in seconds |
Get time elapsed (Facebook Style)
Example:
echo Enum::timeElapsed('21-Jan-2016');
Example Output:
10 hours ago
public static string timeElapsed ( $fromTime = null, $human = true, $toTime = null, $append = null ) | ||
$fromTime | string | Start date time |
$human | boolean | If true returns an approximate human friendly output. If set to
|
$toTime | string | End date time (defaults to current system time) |
$append | string | The string to append for the converted elapsed time. Defaults to ' ago'. |
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.
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 string timeInterval ( $interval, $append = null, $human = true ) | ||
$interval | integer | Time interval in seconds |
$append | string | The string to append for the converted elapsed time. Defaults to |
$human | boolean | If true returns an approximate human friendly output. If set to |
Generate a time picker array list.
Example:
echo implode(', ', Enum::timeList('hour'));
echo implode(', ', Enum::timeList('sec', 5));
public static array timeList ( $unit, $interval = 1, $from = 0, $to = null, $padZero = true ) | ||
$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. |
throws | yii\base\InvalidConfigException | if $unit passed is invalid |
---|
Generate list of thousand multiples.
Example:
$triplets = Enum::days();
echo $triplets[1]; // returns: thousand
echo $triplets[2]; // returns: billion
echo $triplets[0]; // returns: '' (empty string)
public static array triplets ( ) |
Gets the user's IP address
Example:
echo Enum::userIP();
echo Enum::userIP(false);
public static string userIP ( $filterLocal = true ) | ||
$filterLocal | boolean | Whether to filter local & LAN IP (defaults to true) |
Generates a list of years.
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 array yearList ( $from, $to = null, $keys = false, $desc = true ) | ||
$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) |
throws | yii\base\InvalidConfigException | if $to < $from |
---|