@@ -23,223 +23,135 @@ It's an abstract class that needs to be extended to use it.
23
23
> particular concrete representation in the computer's memory; compilers and
24
24
> interpreters can represent them arbitrarily.
25
25
26
-
27
- # Why not ``` SplEnum ```
28
-
29
- * It's not build-in PHP and requires pecl extension
30
- * SplEnum is too much magic under the hod
31
-
32
-
33
- # API
34
-
35
- abstract class MabeEnum\Enum
36
- {
37
- /**
38
- * Constructor
39
- * @param scalar $value The enum value to select
40
- * @throws InvalidArgumentException On unknwon value
41
- */
42
- public function __construct($value);
43
-
44
- /**
45
- * Returns an assoc array of defined constant names and the values
46
- * @return array
47
- */
48
- final public function getConstants();
49
-
50
- /**
51
- * Get the selected value
52
- * @return scalar
53
- */
54
- final public function getValue();
55
-
56
- /**
57
- * Get the constant name of the selected value
58
- * @return string
59
- */
60
- final public function getName();
61
-
62
- /**
63
- * Get the ordinal number of the selected value
64
- * @return int
65
- */
66
- final public function getOrdinal();
67
-
68
- /**
69
- * Get the selected value as string
70
- * (This will be called automatically on converting into a string)
71
- * @return string
72
- */
73
- final public function __toString();
74
-
75
- /**
76
- * Instantiate a new enum were the selected value
77
- * is the constant name of the called method name
78
- * (This will be called automatically on calling static method)
79
- * NOTE: THIS WILL WORK FOR PHP >= 5.3 ONLY
80
- * @param string $name The name of the constant to instantiate
81
- * @param array $args This should be an empty array (no arguments)
82
- * @throws BadMethodCallException If the called method hasn't the same name as a constant
83
- * @return MabeEnum\Enum The instantiated enum
84
- */
85
- final public static __callStatic($name, array $args);
86
- }
87
-
88
-
89
26
# Usage
90
27
91
- ## The way of class constants
28
+ ## Basics
92
29
93
- class User
30
+ use MabeEnum\Enum;
31
+
32
+ // define an own enumeration class
33
+ class UserStatus extends Enum
94
34
{
95
35
const INACTIVE = 0;
96
36
const ACTIVE = 1;
97
37
const DELETED = 2;
98
-
99
- protected $status = 0;
100
-
101
- public function setStatus($status)
102
- {
103
- $intStatus = (int)$status;
104
- if (!in_array($intStatus, array(self::INACTIVE, self::ACTIVE, self::DELETED))) {
105
- throw new InvalidArgumentException("Invalid status {$status}");
106
- }
107
- $this->status = $intStatus;
108
- }
109
-
110
- public function getStatus()
111
- {
112
- return $this->status;
113
- }
114
38
}
39
+
40
+ // different ways to instantiate an enumeration
41
+ $status = UserStatus::get(UserStatus::ACTIVE);
42
+ $status = UserStatus::ACTIVE();
43
+ $status = UserStatus::getByName('ACTIVE');
44
+ $status = UserStatus::getByOrdinal(1);
45
+
46
+ // available methods to get the selected entry
47
+ $status->getValue(); // returns the selected constant value
48
+ $status->getName(); // returns the selected constant name
49
+ $status->getOrdinal(); // returns the ordinal number of the selected constant
50
+ (string) $status; // returns the selected constant name
51
+
52
+ // The same enumerations of the same class holds the same instance
53
+ UserStatus::get(UserStatus::ACTIVE) === UserStatus::ACTIVE()
54
+ UserStatus::get(UserStatus::DELETED) != UserStatus::INACTIVE()
115
55
116
- $user = new User();
117
- echo 'Default user status: ' . $user->getStatus() . PHP_EOL;
118
- $user->setStatus(User::ACTIVE);
119
- echo 'Changed user status: ' . $user->getStatus() . PHP_EOL;
120
-
121
- ** PRINTS**
122
-
123
- Default user status: 0
124
- Changed user status: 1
125
-
126
- * Requires validation on every use
127
- * Hard to extend the list of possible values
128
- * Hard to get a human readable name of a value
129
-
130
- ## The way of php-enum:
131
56
132
- use MabeEnum\Enum;
57
+ ## Type-Hint
133
58
134
- class UserStatusEnum extends Enum
135
- {
136
- const INACTIVE = 0;
137
- const ACTIVE = 1;
138
- const DELETED = 2;
139
- }
59
+ use MabeEnum\Enum;
60
+ use UserStatus;
140
61
141
62
class User
142
63
{
143
64
protected $status;
144
65
145
- public function setStatus(UserStatusEnum $status)
66
+ public function setStatus(UserStatus $status)
146
67
{
147
68
$this->status = $status;
148
69
}
149
70
150
71
public function getStatus()
151
72
{
152
73
if (!$this->status) {
153
- // init default status
154
- $this->status = UserStatusEnum::INACTIVE( );
74
+ // initialize a default value
75
+ $this->status = UserStatus::get(UserStatus::INACTIVE );
155
76
}
156
77
return $this->status;
157
78
}
158
79
}
159
-
160
- $user = new User();
161
- echo 'Default user status: ' . $user->getStatus() . '(' . $user->getStatus()->getValue() . ')' . PHP_EOL;
162
- $user->setStatus(UserStatusEnum::ACTIVE());
163
- echo 'Changed user status: ' . $user->getStatus() . '(' . $user->getStatus()->getValue() . ')' . PHP_EOL;
164
80
165
- ** PRINTS **
81
+ ## EnumMap
166
82
167
- Default user status: INACTIVE (0)
168
- Changed user status: ACTIVE (1)
83
+ An ``` EnumMap ``` maps enumeration instances of exactly one type to data assigned to.
84
+ Internally the ``` EnumMap ``` is based of ``` SplObjectStorage ``` .
169
85
170
- * Validation will be already done on basic class ``` MabeEnum\Enum ```
171
- * Using type-hint makes arguments save
172
- * Human readable name of a value is simple accessable
86
+ use MabeEnum\EnumMap;
87
+ use UserStatus;
173
88
89
+ // create a new EnumMap
90
+ $enumMap = new EnumMap('UserStatus');
174
91
175
- # Install
92
+ // attach entries (by value of by instance)
93
+ $enumMap->attach(UserStatus::INACTIVE, 'inaktiv');
94
+ $enumMap->attach(UserStatus::ACTIVE(), 'aktiv');
95
+ $enumMap->attach(UserStatus::DELETED(), 'gelöscht');
96
+
97
+ // detach entries (by value or by instance)
98
+ $enumMap->detach(UserStatus::INACTIVE);
99
+ $enumMap->detach(UserStatus::DELETED());
100
+
101
+ // iterate
102
+ var_dump(iterator_to_array($enumSet)); // array(0 => UserStatus{$value=1});
176
103
177
- ## Composer
104
+ // it's possible to define the key and the value used for iteration
105
+ $enumSet->setFlags(EnumSet::KEY_AS_NAME | EnumSet::CURRENT_AS_DATA);
106
+ var_dump(iterator_to_array($enumSet)); // array('ACTIVE' => 'aktiv');
178
107
179
- Add ``` marc-mabe/php-enum ``` to the project's composer.json dependencies and run
180
- ``` php composer.phar install ```
181
108
182
- ## GIT
109
+ ## EnumSet
183
110
184
- ``` git clone git://github.com/marc-mabe/php-enum.git ```
111
+ An ``` EnumSet ``` groups enumeration instances of exactly one type together.
112
+ Internally it's based of a list (array) of ordinal values.
185
113
186
- ## ZIP / TAR
114
+ use MabeEnum\EnumSet;
115
+ use UserStatus;
187
116
188
- Download the last version from [ Github ] ( https://github.com/marc-mabe/php-enum/tags )
189
- and extract it.
117
+ // create a new EnumSet
118
+ $enumSet = new EnumSet('UserStatus');
190
119
120
+ // attach entries (by value of by instance)
121
+ $enumSet->attach(UserStatus::INACTIVE);
122
+ $enumSet->attach(UserStatus::ACTIVE());
123
+ $enumSet->attach(UserStatus::DELETED());
124
+
125
+ // detach entries (by value or by instance)
126
+ $enumSet->detach(UserStatus::INACTIVE);
127
+ $enumSet->detach(UserStatus::DELETED());
128
+
129
+ // iterate
130
+ var_dump(iterator_to_array($enumSet)); // array(0 => UserStatus{$value=1});
191
131
192
- # Examples
132
+ # Why not ``` SplEnum ```
193
133
194
- ## Define a default value:
134
+ * ``` SplEnum ``` is not build-in into PHP and requires pecl extension installed.
135
+ * Instances of the same value of an ``` SplEnum ``` are not the same instances.
136
+ * ``` SplEnum ``` doesn't have an implementaiton for ``` EnumMap ``` and ``` EnumSet ``` .
195
137
196
- This example defines the constant ``` ONE ``` with value ``` 1 ``` as default
197
- value.
198
138
199
- use MabeEnum\Enum;
200
-
201
- class MyEnumWithDefaultValue extends Enum
202
- {
203
- const ONE = 1;
204
- const TWO = 2;
205
-
206
- public function __construct($value = self::ONE)
207
- {
208
- parent::__construct($value);
209
- }
210
- }
139
+ # Install
211
140
212
- ## Inheritance
141
+ ## Composer
213
142
214
- It's also possible to extend other enumerations.
143
+ Add ``` marc-mabe/php-enum ``` to the project's composer.json dependencies and run
144
+ ``` php composer.phar install ```
215
145
216
- use MabeEnum\Enum;
217
-
218
- class MyEnum extends Enum
219
- {
220
- const ONE = 1;
221
- const TWO = 2;
222
- }
223
-
224
- class EnumInheritance extends MyEnum
225
- {
226
- const INHERITANCE = 'Inheritance';
227
- }
146
+ ## GIT
228
147
229
- ## Simplified instantiation
148
+ ``` git clone git://github.com/marc-mabe/php-enum.git ```
230
149
231
- It's possible to call one of the defined constants like a method
232
- and you will get the instantiated enum as a result.
150
+ ## ZIP / TAR
151
+
152
+ Download the last version from [ Github] ( https://github.com/marc-mabe/php-enum/tags )
153
+ and extract it.
233
154
234
- use MabeEnum\Enum;
235
-
236
- class MyEnum extends Enum
237
- {
238
- const ONE = 1;
239
- const TWO = 2;
240
- }
241
-
242
- $enum = MyEnum::ONE();
243
155
244
156
# New BSD License
245
157
0 commit comments