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
@@ -58,10 +58,10 @@ abstract class AbstractEnum
58
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
61
+ * @param string $value The enum value
62
62
* @param string $name The constant name
63
63
*/
64
- final private function __construct ($ value , string $ name )
64
+ final private function __construct (string $ value , string $ name )
65
65
{
66
66
$ this ->value = $ value ;
67
67
$ this ->name = $ name ;
@@ -105,16 +105,16 @@ final public function __set(string $property, $value): void
105
105
* Create an enum instance from a value, throws exception if invalid
106
106
*
107
107
* @since n.e.x.t
108
- * @param string|int $value The enum value
108
+ * @param string $value The enum value
109
109
* @return static
110
110
* @throws InvalidArgumentException If the value is not valid
111
111
*/
112
- final public static function from ($ value ): self
112
+ final public static function from (string $ value ): self
113
113
{
114
114
$ instance = self ::tryFrom ($ value );
115
115
if ($ instance === null ) {
116
116
throw new InvalidArgumentException (
117
- sprintf ('%s is not a valid backing value for enum %s ' , ( string ) $ value , static ::class)
117
+ sprintf ('%s is not a valid backing value for enum %s ' , $ value , static ::class)
118
118
);
119
119
}
120
120
return $ instance ;
@@ -124,10 +124,10 @@ final public static function from($value): self
124
124
* Try to create an enum instance from a value, returns null if invalid
125
125
*
126
126
* @since n.e.x.t
127
- * @param string|int $value The enum value
127
+ * @param string $value The enum value
128
128
* @return static|null
129
129
*/
130
- final public static function tryFrom ($ value ): ?self
130
+ final public static function tryFrom (string $ value ): ?self
131
131
{
132
132
$ constants = self ::getConstants ();
133
133
foreach ($ constants as $ name => $ constantValue ) {
@@ -158,7 +158,7 @@ final public static function cases(): array
158
158
* Check if this enum has the same value as the given value
159
159
*
160
160
* @since n.e.x.t
161
- * @param string|int| self $other The value or enum to compare
161
+ * @param string|self $other The value or enum to compare
162
162
* @return bool
163
163
*/
164
164
final public function equals ($ other ): bool
@@ -186,7 +186,7 @@ final public function is(self $other): bool
186
186
* Get all valid values for this enum
187
187
*
188
188
* @since n.e.x.t
189
- * @return array<string, string|int >
189
+ * @return array<string, string>
190
190
*/
191
191
final public static function getValues (): array
192
192
{
@@ -197,10 +197,10 @@ final public static function getValues(): array
197
197
* Check if a value is valid for this enum
198
198
*
199
199
* @since n.e.x.t
200
- * @param string|int $value The value to check
200
+ * @param string $value The value to check
201
201
* @return bool
202
202
*/
203
- final public static function isValidValue ($ value ): bool
203
+ final public static function isValidValue (string $ value ): bool
204
204
{
205
205
return in_array ($ value , self ::getValues (), true );
206
206
}
@@ -209,11 +209,11 @@ final public static function isValidValue($value): bool
209
209
* Get or create a singleton instance for the given value and name
210
210
*
211
211
* @since n.e.x.t
212
- * @param string|int $value The enum value
212
+ * @param string $value The enum value
213
213
* @param string $name The constant name
214
214
* @return static
215
215
*/
216
- private static function getInstance ($ value , string $ name ): self
216
+ private static function getInstance (string $ value , string $ name ): self
217
217
{
218
218
$ className = static ::class;
219
219
@@ -234,7 +234,7 @@ private static function getInstance($value, string $name): self
234
234
* Get all constants for this enum class
235
235
*
236
236
* @since n.e.x.t
237
- * @return array<string, string|int >
237
+ * @return array<string, string>
238
238
* @throws \RuntimeException If invalid constant found
239
239
*/
240
240
final protected static function getConstants (): array
@@ -260,11 +260,11 @@ final protected static function getConstants(): array
260
260
}
261
261
262
262
// Check if value is valid type
263
- if (!is_string ($ value ) && ! is_int ( $ value ) ) {
263
+ if (!is_string ($ value )) {
264
264
throw new \RuntimeException (
265
265
sprintf (
266
266
'Invalid enum value type for constant %s::%s. ' .
267
- 'Only string and int values are allowed, %s given. ' ,
267
+ 'Only string values are allowed, %s given. ' ,
268
268
$ className ,
269
269
$ name ,
270
270
gettype ($ value )
@@ -354,6 +354,6 @@ private static function camelCaseToConstant(string $camelCase): string
354
354
*/
355
355
final public function __toString (): string
356
356
{
357
- return ( string ) $ this ->value ;
357
+ return $ this ->value ;
358
358
}
359
359
}
0 commit comments