Krajee

Enum Helper

Thankful to Krajee! BUY A COFFEEor to get more out of us.
This helper class extends the Yii Inflector Helper with more utility functions for Yii 2 developers.

Tip

Not seeing the updated content on this page! Hard refresh your browser to clean cache for this page (e.g. SHIFT-F5 on Windows Chrome)

Check if a variable is empty or not set.
The $var parameter can be an array or string.
/**
 * @param reference $var variable to perform the check
 */
if (Enum::isEmpty($var)) {
    // do actions
}

Check if a value exists in the array. This method is faster in performance than the PHP built in in_array method. This method accepts the following parameters:

  • $needle: mixed, the value to search

  • $haystack: array, the array to scan

apple exists
/**
 * @param mixed $needle the value to search
 * @param array $haystack the array to scan
 */
if (Enum::inArray('apple', ['apple', 'mango', 'banana'])) {
    echo 'apple exists';
}
Properize a string for possessive punctuation.
Chris'
David's
/**
 * @param string $string the input string
 */
echo Enum::properize("Chris");
echo Enum::properize("David");
Get time elapsed between two times - typically until now. This is by default the human friendly format like on Facebook. Alternatively, one can choose to convert the entire time span to words accurately.
Human Friendly: 13 years ago
Raw Conversion: 13 years 2 months 3 weeks 3 days 27 minutes 27 seconds ago
/**
 * @param string $fromTime start date time 
 * @param boolean $human if true returns an approximate human friendly output
 *     - If set to false it will attempt an exact conversion of time intervals.
 * @param string $toTime end date time - defaults to current system time if not provided
 * @param string $append the string to append for the converted elapsed time - defaults to ' ago'
 */
echo 'Human Friendly: ' . Enum::timeElapsed("2011-08-30 03:11:39");
echo 'Raw Conversion: ' . Enum::timeElapsed("2011-08-30 03:11:39", false);
Format and convert number of Bytes to its optimal higher metric unit
Size (KB): 1.17 KB
Size (MB): 27.12 MB
Size (GB): 16.138 GB
/**
 * @param double $bytes number of bytes
 * @param integer $precision the number of decimal places to round off
 */
echo '<b>Size (KB):</b> ' . Enum::formatBytes(120.32);
echo '<b>Size (MB):</b> ' . Enum::formatBytes(28434322.25);
echo '<b>Size (GB):</b> ' . Enum::formatBytes(17328347842.25, 3);
Convert an integer number to words and returns an anglicized string.
23,289,438 = twenty three million two hundred eighty nine thousand four hundred thirty eight
380,004 = three hundred eighty thousand four
/**
 * @param double $num source number to be converted
 */
echo '<b>23,289,438 = </b> ' . Enum::numToWords(23289438);
echo '<b>380,004 = </b> ' . Enum::numToWords(380004);
Generates an array of years between from and to years. Years till current system date will be generated if to year is not specified. This is very useful for generating dropdown lists in forms or creating filter inputs in Yii's GridView.
Array
(
    [0] => 2024
    [1] => 2023
    [2] => 2022
    [3] => 2021
    [4] => 2020
    [5] => 2019
    [6] => 2018
    [7] => 2017
    [8] => 2016
    [9] => 2015
    [10] => 2014
    [11] => 2013
    [12] => 2012
    [13] => 2011
    [14] => 2010
    [15] => 2009
    [16] => 2008
    [17] => 2007
    [18] => 2006
    [19] => 2005
    [20] => 2004
    [21] => 2003
    [22] => 2002
)
Array
(
    [1990] => 1990
    [1991] => 1991
    [1992] => 1992
    [1993] => 1993
    [1994] => 1994
    [1995] => 1995
    [1996] => 1996
    [1997] => 1997
    [1998] => 1998
    [1999] => 1999
    [2000] => 2000
    [2001] => 2001
)
/**
 * @param integer $from the start year
 * @param integer $to the end year
 * @param boolean $keys whether to set the array keys same as the values (defaults to false)
 * @param boolean $desc whether to sort the years descending (defaults to true)
 */
print_r(Enum::yearList(2002));
print_r(Enum::yearList(1990, 2001, true, false));
Generates an array of months based on Gregorian calendar from a starting month number you specify. You can choose to build an abbreviated month list and also choose the case of the month. This is very useful for generating dropdown lists in forms or creating filter inputs in Yii's GridView.
Array
(
    [1] => January
    [2] => February
    [3] => March
    [4] => April
    [5] => May
    [6] => June
    [7] => July
    [8] => August
    [9] => September
    [10] => October
    [11] => November
    [12] => December
)
Array
(
    [1] => APR
    [2] => MAY
    [3] => JUN
    [4] => JUL
    [5] => AUG
    [6] => SEP
    [7] => OCT
    [8] => NOV
    [9] => DEC
    [10] => JAN
    [11] => FEB
    [12] => MAR
)
/**
 * @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.
 */
print_r(Enum::monthList());
print_r(Enum::monthList(true, 4, 'upper'));
Generates an array of days based on Gregorian calendar from a starting day number you specify. You can choose to build an abbreviated day list and also choose the case of the day. This is very useful for generating dropdown lists in forms or creating filter inputs in Yii's GridView.
Array
(
    [1] => Sunday
    [2] => Monday
    [3] => Tuesday
    [4] => Wednesday
    [5] => Thursday
    [6] => Friday
    [7] => Saturday
)
Array
(
    [1] => MON
    [2] => TUE
    [3] => WED
    [4] => THU
    [5] => FRI
    [6] => SAT
    [7] => SUN
)
/**
 * @param boolean $abbr whether to return abbreviated month
 * @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.
 */
print_r(Enum::dayList());
print_r(Enum::dayList(true, 2, 'upper'));
Generates a date picker list (just the numeric days) for the Gregorian Calendar between from and to days. This is very useful for generating dropdown lists in forms or creating filter inputs in Yii's GridView.
Array
(
    [0] => 1
    [1] => 7
    [2] => 14
    [3] => 21
    [4] => 28
    [5] => 30
)
Array
(
    [0] => 1
    [1] => 5
    [2] => 10
    [3] => 15
    [4] => 20
    [5] => 25
    [6] => 30
)
/**
 * @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.
 */
print_r(Enum::dateList(1, 28, 7));
print_r(Enum::dateList(1, 10));
Generates a time picker array. A time unit is to be passed and can be hour, min, sec, or ms. This is very useful for generating dropdown lists in forms or creating filter inputs in Yii's GridView.
00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
00, 05, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55
/**
 * @param string $unit the time unit ('hour', 'min', 'sec', 'ms')
 * @param integer $interval the interval between each time unit list entry. Defaults to 1.
 * @param integer $from the starting time. Defaults to 0.
 * @param integer $to the ending time. Defaults to 23 for hour, 59 for min & sec, and 999 for ms.
 * @param boolean $padZero whether to left pad zero (0) in the time value displayed. Defaults to true
 */
echo implode(', ', Enum::timeList('hour'));
echo implode(', ', Enum::timeList('sec', 5));
Generates a boolean array. You can pass the true label (default Yes) and false label (default No). This is very useful for generating dropdown lists in forms or creating filter inputs in Yii's GridView.
Array
(
    [0] => No
    [1] => Yes
)
Array
(
    [0] => Active
    [1] => Inactive
)
/**
 * @param string $true the label for the true value
 * @param string $false the label for the false value
 */
print_r(Enum::boolList());
print_r(Enum::boolList('Active', 'Inactive'));
Fetches a PHP variable type by parsing the variable logically based on its content. It differs from the inbuilt PHP gettype() function in the sense, that it parses strings and returns the type based on its content. The function returns one of the following types:
  • array
  • object
  • resource
  • NULL
  • boolean
  • float
  • integer
  • datetime
  • string
  • unknown
id: integer
name: string
date: datetime
amount: float
relations: array
$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>";
}
Converts an associative array to a HTML Table. If an invalid array is passed, this will return false. The following parameters are supported.
  • array: array the associative array to be converted.

  • transpose: boolean whether to show keys as rows instead of columns. Defaults to false. 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. Check an example of usage in the getBrowser function below.

  • recursive: boolean whether to recursively generate tables for multi-dimensional arrays. Defaults to false

  • typeHint: boolean whether to show the data type as a hint. Defaults to true.

  • null: string the content to display for blank cells. Defaults to <span span class="not-set">NULL</span>.

  • tableOptions: array the HTML attributes for the table. Defaults to ['class' => 'table table-bordered table-striped']

  • keyOptions: array the HTML attributes for the array key. Defaults to []

  • valueOptions: array the HTML attributes for the array value. Defaults to ['style' => 'cursor: default; border-bottom: 1px #aaa dashed;']

idnamebirthdaycommissionactive
1John01-Jul-19764,500.50true
2Scott26-Feb-19801,300.40true
3Mary1990-02-10(not set)false
4Lisa17-Dec-1982-900.34true
$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);
Gets the user's IP address. You can choose to filter local or LAN IP addresses.
IP Address (Filtered)
18.220.206.141
IP Address (Raw)
18.220.206.141
/**
 * @param boolean $filterLocal whether to filter local & LAN IP
 */
echo Enum::userIP();
echo Enum::userIP(false);
A quick way to fetch the basic browser information based on user agent. The following parameters are supported.
  • common: boolean whether to validate the most common browsers only. Defaults to false. For quicker performance or for just checking the most commonly used browsers, set this to true.

  • browsers: array the function validates most of the browsers - but if you choose, you can pass this list as key value pairs. The array key is the browser code (in lower case) as detected in the user agent string. The array value is the description/full name for the browser.

The function returns an array containing the following information about the browser:
  • agent: string the complete user agent string.

  • code: string the browser code in lower case.

  • name: string the full name of the browser.

  • version: float the detected browser version (only the major and minor versions are shown).

  • platform: string the platform on which the browser is running.

Note

This is just a quick way of getting basic browser, version, and platform information (approximately). It depends on the user agent setting and should not be used for secure scenarios (because the user agent can be manipulated).

Your Browser Details

agentMozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
codeother
nameOther
version?
platformUnknown
echo '<p class="lead">Your Browser Details</p>';
echo Enum::array2table(Enum::getBrowser(), true);

Note

You can now visit the Krajee Webtips Q & A forum for searching OR asking questions OR helping programmers with answers on these extensions and plugins. For asking a question click here. Select the appropriate question category (i.e. Krajee Plugins) and choose this current page plugin in the question related to field.

The comments and discussion section below are intended for generic discussions or feedback for this plugin. Developers may not be able to search or lookup here specific questions or tips on usage for this plugin.

 
visitors to Krajee Yii2 Demos since 22-May-2017