@@ -28,6 +28,7 @@ It's an abstract class that needs to be extended to use it.
28
28
29
29
## Basics
30
30
31
+ ``` php
31
32
use MabeEnum\Enum;
32
33
33
34
// define an own enumeration class
@@ -44,19 +45,19 @@ It's an abstract class that needs to be extended to use it.
44
45
const STR = 'string';
45
46
const FLOAT = 0.123;
46
47
}
47
-
48
+
48
49
// different ways to instantiate an enumerator
49
50
$status = UserStatus::get(UserStatus::ACTIVE);
50
51
$status = UserStatus::ACTIVE();
51
52
$status = UserStatus::getByName('ACTIVE');
52
53
$status = UserStatus::getByOrdinal(1);
53
-
54
+
54
55
// available methods to get the selected entry
55
56
$status->getValue(); // returns the selected constant value
56
57
$status->getName(); // returns the selected constant name
57
58
$status->getOrdinal(); // returns the ordinal number of the selected constant
58
59
(string) $status; // returns the selected constant name
59
-
60
+
60
61
// same enumerators (of the same enumeration class) holds the same instance
61
62
UserStatus::get(UserStatus::ACTIVE) === UserStatus::ACTIVE()
62
63
UserStatus::get(UserStatus::DELETED) != UserStatus::INACTIVE()
@@ -66,10 +67,11 @@ It's an abstract class that needs to be extended to use it.
66
67
UserStatus::ACTIVE()->is(UserStatus::ACTIVE()); // true
67
68
UserStatus::ACTIVE()->is(UserStatus::DELETED); // false
68
69
UserStatus::ACTIVE()->is(UserStatus::DELETED()); // false
69
-
70
+ ```
70
71
71
72
## Type-Hint
72
-
73
+
74
+ ``` php
73
75
use MabeEnum\Enum;
74
76
75
77
class User
@@ -90,23 +92,27 @@ It's an abstract class that needs to be extended to use it.
90
92
return $this->status;
91
93
}
92
94
}
95
+ ```
93
96
94
97
### Type-Hint issue
95
98
96
99
Because in normal OOP the above example allows ` UserStatus ` and types inherited from it.
97
100
98
101
Please think about the following example:
99
102
103
+ ``` php
100
104
class ExtendedUserStatus
101
105
{
102
106
const EXTENDED = 'extended';
103
107
}
104
108
105
109
$user->setStatus(ExtendedUserStatus::EXTENDED());
110
+ ```
106
111
107
112
Now the setter receives a status it doesn't know about but allows it.
108
113
If your ` User ` class doesn't allow it the following is the recommanded way:
109
114
115
+ ``` php
110
116
class User
111
117
{
112
118
// ...
@@ -116,6 +122,7 @@ If your `User` class doesn't allow it the following is the recommanded way:
116
122
}
117
123
// ...
118
124
}
125
+ ```
119
126
120
127
Now you are 100% sure to work with an exact instace of ` UserStatus ` .
121
128
@@ -128,6 +135,7 @@ An ```EnumMap``` maps enumerators of the same type to data assigned to.
128
135
129
136
Internally the ``` EnumMap ``` is based of ``` SplObjectStorage ``` .
130
137
138
+ ``` php
131
139
use MabeEnum\EnumMap;
132
140
133
141
// create a new EnumMap
@@ -148,7 +156,7 @@ Internally the ```EnumMap``` is based of ```SplObjectStorage```.
148
156
// define key and value used for iteration
149
157
$enumSet->setFlags(EnumMap::KEY_AS_NAME | EnumMap::CURRENT_AS_DATA);
150
158
var_dump(iterator_to_array($enumSet)); // array('ACTIVE' => 'aktiv');
151
-
159
+ ```
152
160
153
161
## EnumSet
154
162
@@ -160,6 +168,7 @@ The maximun number of enumerators are limited by the size of an integer.
160
168
161
169
Enumerators will be ordered by the ordinal number.
162
170
171
+ ``` php
163
172
use MabeEnum\EnumSet;
164
173
165
174
// create a new EnumSet
@@ -176,7 +185,7 @@ Enumerators will be ordered by the ordinal number.
176
185
177
186
// iterate
178
187
var_dump(iterator_to_array($enumSet)); // array(0 => UserStatus{$value=1});
179
-
188
+ ```
180
189
181
190
# Why not ``` SplEnum ```
182
191
0 commit comments