@@ -38,9 +38,9 @@ use MabeEnum\Enum;
38
38
// define an own enumeration class
39
39
class UserStatus extends Enum
40
40
{
41
- const INACTIVE = 0 ;
42
- const ACTIVE = 1 ;
43
- const DELETED = 2 ;
41
+ const INACTIVE = 'i' ;
42
+ const ACTIVE = 'a' ;
43
+ const DELETED = 'd' ;
44
44
45
45
// all scalar datatypes are supported
46
46
const NIL = null;
@@ -72,17 +72,24 @@ class UserStatus extends Enum
72
72
// const CLASS = 'class';
73
73
}
74
74
75
- // different ways to instantiate an enumerator
76
- $status = UserStatus::get(UserStatus::ACTIVE);
77
- $status = UserStatus::ACTIVE();
78
- $status = UserStatus::byName('ACTIVE');
79
- $status = UserStatus::byOrdinal(1);
75
+ // ways to instantiate an enumerator
76
+ $status = UserStatus::get(UserStatus::ACTIVE); // by value or instance
77
+ $status = UserStatus::ACTIVE(); // by name as callable
78
+ $status = UserStatus::byValue('a'); // by value
79
+ $status = UserStatus::byName('ACTIVE'); // by name
80
+ $status = UserStatus::byOrdinal(1); // by ordinal number
80
81
81
- // available methods to get the selected entry
82
+ // basic methods of an instantiated enumerator
82
83
$status->getValue(); // returns the selected constant value
83
84
$status->getName(); // returns the selected constant name
84
85
$status->getOrdinal(); // returns the ordinal number of the selected constant
85
- (string) $status; // returns the selected constant name
86
+
87
+ // basic methods to list defined enumerators
88
+ UserStatus::getEnumerators() // returns a list of enumerator instances
89
+ UserStatus::getValues() // returns a list of enumerator values
90
+ UserStatus::getNames() // returns a list of enumerator names
91
+ UserStatus::getOrdinals() // returns a list of ordinal numbers
92
+ UserStatus::getConstants() // returns an associative array of enumerator names to enumerator values
86
93
87
94
// same enumerators (of the same enumeration class) holds the same instance
88
95
UserStatus::get(UserStatus::ACTIVE) === UserStatus::ACTIVE()
@@ -113,8 +120,8 @@ class User
113
120
public function getStatus()
114
121
{
115
122
if (!$this->status) {
116
- // initialize the default enumerator
117
- $this->status = UserStatus::get(UserStatus:: INACTIVE);
123
+ // initialize default
124
+ $this->status = UserStatus::INACTIVE( );
118
125
}
119
126
return $this->status;
120
127
}
0 commit comments