-
Notifications
You must be signed in to change notification settings - Fork 36
Get a list of all enumerator instances of the called enumeration type #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,21 @@ final private function __construct($value, $ordinal = null) | |
$this->value = $value; | ||
$this->ordinal = $ordinal; | ||
} | ||
|
||
/** | ||
* Returns a list of all enumeration instances of actual implementation | ||
* | ||
* @return Enum[] | ||
*/ | ||
public static function getAll() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final public static function getInstances()
{
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class()));
} Thoughts ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah! I'll mention here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like I have benchmarked 3 versions but my version is a little bit faster :) final public static function getEnumerators1()
{
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class())));
}
final public static function getEnumerators2()
{
$enumerators = array_keys(self::detectConstants(get_called_class()));
foreach ($enumerators as & $entry) {
$entry = self::getByName($entry);
}
return $enumerators;
}
final public static function getEnumerators3()
{
$enums = array();
foreach (array_keys(self::detectConstants(get_called_class())) as $v) {
$enums[] = self::getByName($v);
}
return $enums;
} class MyEnum extends \MabeEnum\Enum {
const A = 'a';
const B = 'b';
const C = 'c';
const D = 'd';
const E = 'e';
const F = 'f';
const G = 'g';
const H = 'h';
const I = 'i';
const J = 'j';
const K = 'k';
const L = 'l';
const M = 'm';
const N = 'n';
const O = 'o';
const P = 'p';
const Q = 'q';
const R = 'r';
const S = 's';
const T = 't';
const U = 'u';
const V = 'v';
const W = 'w';
const X = 'x';
const Y = 'y';
const Z = 'z';
}
MyEnum::getConstants();
echo 'getEnumerators1(): ';
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
MyEnum::getEnumerators1();
}
$end = microtime(true);
echo ($end - $start) . PHP_EOL;
echo 'getEnumerators2(): ';
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
MyEnum::getEnumerators2();
}
$end = microtime(true);
echo ($end - $start) . PHP_EOL;
echo 'getEnumerators3(): ';
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
MyEnum::getEnumerators3();
}
$end = microtime(true);
echo ($end - $start) . PHP_EOL;
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marc-mabe thx! |
||
{ | ||
$enums = array(); | ||
foreach (array_keys(self::getConstants()) as $v) { | ||
$enums[] = self::getByName($v); | ||
} | ||
|
||
return $enums; | ||
} | ||
/** | ||
* Get the current selected constant name | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returns a list of all enumerator instances of called enumeration type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah!