35
35
abstract class AbstractEnum
36
36
{
37
37
/**
38
- * @var string|int The value of the enum instance
38
+ * @var string The value of the enum instance
39
39
*/
40
- private $ value ;
40
+ private string $ value ;
41
41
42
42
/**
43
43
* @var string The name of the enum constant
44
44
*/
45
45
private string $ name ;
46
46
47
47
/**
48
- * @var array<string, array<string, string|int >> Cache for reflection data
48
+ * @var array<string, array<string, string>> Cache for reflection data
49
49
*/
50
50
private static array $ cache = [];
51
51
@@ -55,25 +55,27 @@ abstract class AbstractEnum
55
55
private static array $ instances = [];
56
56
57
57
/**
58
- * Constructor is private to ensure instances are created through static methods
58
+ * Constructor is private to ensure instances are created through static methods.
59
59
*
60
60
* @since n.e.x.t
61
- * @param string|int $value The enum value
62
- * @param string $name The constant name
61
+ *
62
+ * @param string $value The enum value.
63
+ * @param string $name The constant name.
63
64
*/
64
- final private function __construct ($ value , string $ name )
65
+ final private function __construct (string $ value , string $ name )
65
66
{
66
67
$ this ->value = $ value ;
67
68
$ this ->name = $ name ;
68
69
}
69
70
70
71
/**
71
- * Magic getter to provide read-only access to properties
72
+ * Provides read-only access to properties.
72
73
*
73
74
* @since n.e.x.t
74
- * @param string $property The property name
75
- * @return mixed
76
- * @throws BadMethodCallException If property doesn't exist
75
+ *
76
+ * @param string $property The property name.
77
+ * @return mixed The property value.
78
+ * @throws BadMethodCallException If property doesn't exist.
77
79
*/
78
80
final public function __get (string $ property )
79
81
{
@@ -87,12 +89,13 @@ final public function __get(string $property)
87
89
}
88
90
89
91
/**
90
- * Magic setter to prevent property modification
92
+ * Prevents property modification.
91
93
*
92
94
* @since n.e.x.t
93
- * @param string $property The property name
94
- * @param mixed $value The value to set
95
- * @throws BadMethodCallException Always, as enum properties are read-only
95
+ *
96
+ * @param string $property The property name.
97
+ * @param mixed $value The value to set.
98
+ * @throws BadMethodCallException Always, as enum properties are read-only.
96
99
*/
97
100
final public function __set (string $ property , $ value ): void
98
101
{
@@ -102,32 +105,34 @@ final public function __set(string $property, $value): void
102
105
}
103
106
104
107
/**
105
- * Create an enum instance from a value, throws exception if invalid
108
+ * Creates an enum instance from a value, throws exception if invalid.
106
109
*
107
110
* @since n.e.x.t
108
- * @param string|int $value The enum value
109
- * @return static
110
- * @throws InvalidArgumentException If the value is not valid
111
+ *
112
+ * @param string $value The enum value.
113
+ * @return static The enum instance.
114
+ * @throws InvalidArgumentException If the value is not valid.
111
115
*/
112
- final public static function from ($ value ): self
116
+ final public static function from (string $ value ): self
113
117
{
114
118
$ instance = self ::tryFrom ($ value );
115
119
if ($ instance === null ) {
116
120
throw new InvalidArgumentException (
117
- sprintf ('%s is not a valid backing value for enum %s ' , ( string ) $ value , static ::class)
121
+ sprintf ('%s is not a valid backing value for enum %s ' , $ value , static ::class)
118
122
);
119
123
}
120
124
return $ instance ;
121
125
}
122
126
123
127
/**
124
- * Try to create an enum instance from a value, returns null if invalid
128
+ * Tries to create an enum instance from a value, returns null if invalid.
125
129
*
126
130
* @since n.e.x.t
127
- * @param string|int $value The enum value
128
- * @return static|null
131
+ *
132
+ * @param string $value The enum value.
133
+ * @return static|null The enum instance or null.
129
134
*/
130
- final public static function tryFrom ($ value ): ?self
135
+ final public static function tryFrom (string $ value ): ?self
131
136
{
132
137
$ constants = self ::getConstants ();
133
138
foreach ($ constants as $ name => $ constantValue ) {
@@ -139,10 +144,11 @@ final public static function tryFrom($value): ?self
139
144
}
140
145
141
146
/**
142
- * Get all enum cases
147
+ * Gets all enum cases.
143
148
*
144
149
* @since n.e.x.t
145
- * @return static[]
150
+ *
151
+ * @return static[] Array of all enum instances.
146
152
*/
147
153
final public static function cases (): array
148
154
{
@@ -155,11 +161,12 @@ final public static function cases(): array
155
161
}
156
162
157
163
/**
158
- * Check if this enum has the same value as the given value
164
+ * Checks if this enum has the same value as the given value.
159
165
*
160
166
* @since n.e.x.t
161
- * @param string|int|self $other The value or enum to compare
162
- * @return bool
167
+ *
168
+ * @param string|self $other The value or enum to compare.
169
+ * @return bool True if values are equal.
163
170
*/
164
171
final public function equals ($ other ): bool
165
172
{
@@ -171,49 +178,53 @@ final public function equals($other): bool
171
178
}
172
179
173
180
/**
174
- * Check if this enum is the same instance type and value as another enum
181
+ * Checks if this enum is the same instance type and value as another enum.
175
182
*
176
183
* @since n.e.x.t
177
- * @param self $other The other enum to compare
178
- * @return bool
184
+ *
185
+ * @param self $other The other enum to compare.
186
+ * @return bool True if enums are identical.
179
187
*/
180
188
final public function is (self $ other ): bool
181
189
{
182
190
return $ this === $ other ; // Since we're using singletons, we can use identity comparison
183
191
}
184
192
185
193
/**
186
- * Get all valid values for this enum
194
+ * Gets all valid values for this enum.
187
195
*
188
196
* @since n.e.x.t
189
- * @return array<string, string|int>
197
+ *
198
+ * @return array<string, string> Map of constant names to values.
190
199
*/
191
200
final public static function getValues (): array
192
201
{
193
202
return self ::getConstants ();
194
203
}
195
204
196
205
/**
197
- * Check if a value is valid for this enum
206
+ * Checks if a value is valid for this enum.
198
207
*
199
208
* @since n.e.x.t
200
- * @param string|int $value The value to check
201
- * @return bool
209
+ *
210
+ * @param string $value The value to check.
211
+ * @return bool True if value is valid.
202
212
*/
203
- final public static function isValidValue ($ value ): bool
213
+ final public static function isValidValue (string $ value ): bool
204
214
{
205
215
return in_array ($ value , self ::getValues (), true );
206
216
}
207
217
208
218
/**
209
- * Get or create a singleton instance for the given value and name
219
+ * Gets or creates a singleton instance for the given value and name.
210
220
*
211
221
* @since n.e.x.t
212
- * @param string|int $value The enum value
213
- * @param string $name The constant name
214
- * @return static
222
+ *
223
+ * @param string $value The enum value.
224
+ * @param string $name The constant name.
225
+ * @return static The enum instance.
215
226
*/
216
- private static function getInstance ($ value , string $ name ): self
227
+ private static function getInstance (string $ value , string $ name ): self
217
228
{
218
229
$ className = static ::class;
219
230
@@ -231,11 +242,12 @@ private static function getInstance($value, string $name): self
231
242
}
232
243
233
244
/**
234
- * Get all constants for this enum class
245
+ * Gets all constants for this enum class.
235
246
*
236
247
* @since n.e.x.t
237
- * @return array<string, string|int>
238
- * @throws \RuntimeException If invalid constant found
248
+ *
249
+ * @return array<string, string> Map of constant names to values.
250
+ * @throws \RuntimeException If invalid constant found.
239
251
*/
240
252
final protected static function getConstants (): array
241
253
{
@@ -260,11 +272,11 @@ final protected static function getConstants(): array
260
272
}
261
273
262
274
// Check if value is valid type
263
- if (!is_string ($ value ) && ! is_int ( $ value ) ) {
275
+ if (!is_string ($ value )) {
264
276
throw new \RuntimeException (
265
277
sprintf (
266
278
'Invalid enum value type for constant %s::%s. ' .
267
- 'Only string and int values are allowed, %s given. ' ,
279
+ 'Only string values are allowed, %s given. ' ,
268
280
$ className ,
269
281
$ name ,
270
282
gettype ($ value )
@@ -282,13 +294,14 @@ final protected static function getConstants(): array
282
294
}
283
295
284
296
/**
285
- * Handle dynamic method calls for enum checking
297
+ * Handles dynamic method calls for enum checking.
286
298
*
287
299
* @since n.e.x.t
288
- * @param string $name The method name
289
- * @param array<mixed> $arguments The method arguments
290
- * @return bool
291
- * @throws BadMethodCallException If the method doesn't exist
300
+ *
301
+ * @param string $name The method name.
302
+ * @param array<mixed> $arguments The method arguments.
303
+ * @return bool True if the enum value matches.
304
+ * @throws BadMethodCallException If the method doesn't exist.
292
305
*/
293
306
final public function __call (string $ name , array $ arguments ): bool
294
307
{
@@ -308,13 +321,14 @@ final public function __call(string $name, array $arguments): bool
308
321
}
309
322
310
323
/**
311
- * Handle static method calls for enum creation
324
+ * Handles static method calls for enum creation.
312
325
*
313
326
* @since n.e.x.t
314
- * @param string $name The method name
315
- * @param array<mixed> $arguments The method arguments
316
- * @return static
317
- * @throws BadMethodCallException If the method doesn't exist
327
+ *
328
+ * @param string $name The method name.
329
+ * @param array<mixed> $arguments The method arguments.
330
+ * @return static The enum instance.
331
+ * @throws BadMethodCallException If the method doesn't exist.
318
332
*/
319
333
final public static function __callStatic (string $ name , array $ arguments ): self
320
334
{
@@ -331,11 +345,12 @@ final public static function __callStatic(string $name, array $arguments): self
331
345
}
332
346
333
347
/**
334
- * Convert camelCase to CONSTANT_CASE
348
+ * Converts camelCase to CONSTANT_CASE.
335
349
*
336
350
* @since n.e.x.t
337
- * @param string $camelCase The camelCase string
338
- * @return string The CONSTANT_CASE version
351
+ *
352
+ * @param string $camelCase The camelCase string.
353
+ * @return string The CONSTANT_CASE version.
339
354
*/
340
355
private static function camelCaseToConstant (string $ camelCase ): string
341
356
{
@@ -347,13 +362,14 @@ private static function camelCaseToConstant(string $camelCase): string
347
362
}
348
363
349
364
/**
350
- * String representation of the enum
365
+ * Returns string representation of the enum.
351
366
*
352
367
* @since n.e.x.t
353
- * @return string
368
+ *
369
+ * @return string The enum value.
354
370
*/
355
371
final public function __toString (): string
356
372
{
357
- return ( string ) $ this ->value ;
373
+ return $ this ->value ;
358
374
}
359
375
}
0 commit comments