13
13
14
14
abstract class MagicConstant
15
15
{
16
- /** @var mixed */
17
- protected $ value ;
16
+ protected mixed $ value ;
18
17
19
- /** @var array */
20
- protected static $ cache = [];
18
+ /** @var array<class-string<static>, array<mixed>> */
19
+ protected static array $ cache = [];
21
20
22
- /**
23
- * @param mixed $value
24
- */
25
- final public function __construct ($ value )
21
+ final public function __construct (mixed $ value )
26
22
{
27
23
if ($ value instanceof self) {
28
24
$ value = $ value ->getValue ();
@@ -31,17 +27,13 @@ final public function __construct($value)
31
27
$ this ->setValue ($ value );
32
28
}
33
29
34
- /**
35
- * @param string|null $format
36
- * @return mixed
37
- */
38
- public function getValue (string $ format = null )
30
+ public function getValue (string |int $ format = null ): mixed
39
31
{
40
32
if (empty ($ format )) {
41
33
return $ this ->value ;
42
34
}
43
35
44
- $ values = static ::toArray ();
36
+ $ values = self ::toArray ();
45
37
46
38
if (!isset ($ values [$ this ->getKey ()][$ format ])) {
47
39
throw new InvalidFormatException ($ this , $ format );
@@ -55,7 +47,7 @@ public function getValue(string $format = null)
55
47
*/
56
48
public function getAllFormats (): array
57
49
{
58
- $ values = static ::toArray ();
50
+ $ values = self ::toArray ();
59
51
$ instances = array_map (
60
52
function ($ value ) {
61
53
return new static ($ value );
@@ -71,24 +63,22 @@ function ($value) {
71
63
*/
72
64
public function getAllValues (): array
73
65
{
74
- $ values = static ::toArray ();
66
+ $ values = self ::toArray ();
75
67
76
68
return array_values ($ values [$ this ->getKey ()]);
77
69
}
78
70
79
71
public function getKey (): string
80
72
{
81
- return (string )static ::search ($ this ->value );
73
+ return (string )self ::search ($ this ->value );
82
74
}
83
75
84
76
/**
85
77
* Returns the current instance format.
86
- *
87
- * @return int|string|null
88
78
*/
89
- public function getFormat ()
79
+ public function getFormat (): int | string | null
90
80
{
91
- $ values = static ::toArray ();
81
+ $ values = self ::toArray ();
92
82
93
83
foreach ($ values [$ this ->getKey ()] as $ format => $ value ) {
94
84
if ($ value === $ this ->value ) {
@@ -99,31 +89,22 @@ public function getFormat()
99
89
return null ;
100
90
}
101
91
102
- /**
103
- * @return string
104
- */
105
- public function __toString ()
92
+ public function __toString (): string
106
93
{
107
94
return (string )$ this ->value ;
108
95
}
109
96
110
- /**
111
- * @return MagicConstant
112
- */
113
97
public function normalize (): MagicConstant
114
98
{
115
- $ array = static ::toArray ();
99
+ $ array = self ::toArray ();
116
100
$ key = $ this ->getKey ();
117
101
118
102
$ values = array_values ($ array [$ key ]);
119
103
120
104
return new static ($ values [0 ]);
121
105
}
122
106
123
- /**
124
- * @param mixed $value
125
- */
126
- protected function setValue ($ value ): void
107
+ protected function setValue (mixed $ value ): void
127
108
{
128
109
if (!static ::isValidValue ($ value )) {
129
110
throw new InvalidValueException (static ::class, $ value );
@@ -143,14 +124,13 @@ final public function equals(?MagicConstant $other): bool
143
124
}
144
125
145
126
$ ownKey = $ this ->getKey ();
146
- $ otherKey = static ::search ($ other ->getValue ());
127
+ $ otherKey = self ::search ($ other ->getValue ());
147
128
148
129
return $ ownKey === $ otherKey ;
149
130
}
150
131
151
132
/**
152
133
* @param mixed[] $values
153
- * @return bool
154
134
*/
155
135
public function in (array $ values ): bool
156
136
{
@@ -167,11 +147,7 @@ public function in(array $values): bool
167
147
return false ;
168
148
}
169
149
170
- /**
171
- * @param string $format
172
- * @return static
173
- */
174
- public function toFormat (string $ format ): self
150
+ public function toFormat (string $ format ): static
175
151
{
176
152
return new static ($ this ->getValue ($ format ));
177
153
}
@@ -181,18 +157,17 @@ public function toFormat(string $format): self
181
157
*/
182
158
public static function keys (): array
183
159
{
184
- return array_keys (static ::toArray ());
160
+ return array_keys (self ::toArray ());
185
161
}
186
162
187
163
/**
188
- * @param string|null $pattern
189
164
* @return static[]
190
165
*/
191
166
public static function values (string $ pattern = null ): array
192
167
{
193
168
$ out = [];
194
169
195
- foreach (static ::toArray () as $ key => $ values ) {
170
+ foreach (self ::toArray () as $ key => $ values ) {
196
171
if (null === $ pattern || preg_match ($ pattern , $ key )) {
197
172
$ out [$ key ] = new static (reset ($ values ));
198
173
}
@@ -229,37 +204,25 @@ private static function toArray(): array
229
204
return static ::$ cache [static ::class];
230
205
}
231
206
232
- /**
233
- * @param mixed $value
234
- * @return bool
235
- */
236
- public static function isValidValue ($ value ): bool
207
+ public static function isValidValue (mixed $ value ): bool
237
208
{
238
- return false !== static ::search ($ value );
209
+ return false !== self ::search ($ value );
239
210
}
240
211
241
- /**
242
- * @param mixed $key
243
- * @return bool
244
- */
245
- public static function isValidKey ($ key ): bool
212
+ public static function isValidKey (mixed $ key ): bool
246
213
{
247
- $ array = static ::toArray ();
214
+ $ array = self ::toArray ();
248
215
249
216
return isset ($ array [$ key ]);
250
217
}
251
218
252
- /**
253
- * @param mixed $value
254
- * @return false|string
255
- */
256
- private static function search ($ value )
219
+ private static function search (mixed $ value ): string |false
257
220
{
258
221
/**
259
222
* @var string $constant
260
223
* @var array $values
261
224
*/
262
- foreach (static ::toArray () as $ constant => $ values ) {
225
+ foreach (self ::toArray () as $ constant => $ values ) {
263
226
if (in_array ($ value , $ values , true )) {
264
227
return $ constant ;
265
228
}
@@ -268,11 +231,7 @@ private static function search($value)
268
231
return false ;
269
232
}
270
233
271
- /**
272
- * @param mixed $value
273
- * @return static|null
274
- */
275
- public static function tryFrom ($ value ): ?self
234
+ public static function tryFrom (mixed $ value ): ?self
276
235
{
277
236
try {
278
237
return new static ($ value );
@@ -282,14 +241,12 @@ public static function tryFrom($value): ?self
282
241
}
283
242
284
243
/**
285
- * @param string $name
286
244
* @param array $arguments
287
- * @return static
288
245
* @throws InvalidKeyException
289
246
*/
290
- public static function __callStatic (string $ name , array $ arguments = [])
247
+ public static function __callStatic (string $ name , array $ arguments = []): static
291
248
{
292
- $ array = static ::toArray ();
249
+ $ array = self ::toArray ();
293
250
294
251
if (!isset ($ array [$ name ])) {
295
252
throw new InvalidKeyException (static ::class, $ name );
0 commit comments