|
5 | 5 | namespace Hypervel\Config; |
6 | 6 |
|
7 | 7 | use ArrayAccess; |
| 8 | +use BackedEnum; |
8 | 9 | use Closure; |
9 | 10 | use Hypervel\Contracts\Config\Repository as ConfigContract; |
10 | 11 | use Hypervel\Support\Arr; |
|
13 | 14 | use Hypervel\Support\Traits\Macroable; |
14 | 15 | use InvalidArgumentException; |
15 | 16 | use stdClass; |
| 17 | +use UnitEnum; |
| 18 | + |
| 19 | +use function Hypervel\Support\enum_try_from; |
16 | 20 |
|
17 | 21 | class Repository implements ArrayAccess, ConfigContract |
18 | 22 | { |
@@ -286,6 +290,67 @@ public function nullableArray(string $key, mixed $default = null): ?array |
286 | 290 | return $value; |
287 | 291 | } |
288 | 292 |
|
| 293 | + /** |
| 294 | + * Get the specified enum configuration value. |
| 295 | + * |
| 296 | + * @template TEnum of UnitEnum |
| 297 | + * @param class-string<TEnum> $enumClass |
| 298 | + * @param null|(Closure():(null|TEnum))|TEnum $default |
| 299 | + * @return TEnum |
| 300 | + * @throws InvalidArgumentException |
| 301 | + */ |
| 302 | + public function enum(string $key, string $enumClass, mixed $default = null): UnitEnum |
| 303 | + { |
| 304 | + return $this->resolveEnum($key, $enumClass, $this->get($key, $default)); |
| 305 | + } |
| 306 | + |
| 307 | + /** |
| 308 | + * Get the specified nullable enum configuration value. |
| 309 | + * |
| 310 | + * Without a default the key must be present, even if its value is null. |
| 311 | + * |
| 312 | + * @template TEnum of UnitEnum |
| 313 | + * @param class-string<TEnum> $enumClass |
| 314 | + * @param null|(Closure():(null|TEnum))|TEnum $default |
| 315 | + * @return null|TEnum |
| 316 | + * @throws InvalidArgumentException |
| 317 | + */ |
| 318 | + public function nullableEnum(string $key, string $enumClass, mixed $default = null): ?UnitEnum |
| 319 | + { |
| 320 | + $value = func_num_args() === 2 ? $this->getRequired($key) : $this->get($key, $default); |
| 321 | + |
| 322 | + return $value === null ? null : $this->resolveEnum($key, $enumClass, $value); |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * Resolve an enum case or its exact backing value. |
| 327 | + * |
| 328 | + * @template TEnum of UnitEnum |
| 329 | + * @param class-string<TEnum> $enumClass |
| 330 | + * @return TEnum |
| 331 | + */ |
| 332 | + private function resolveEnum(string $key, string $enumClass, mixed $value): UnitEnum |
| 333 | + { |
| 334 | + if ($value instanceof $enumClass) { |
| 335 | + return $value; |
| 336 | + } |
| 337 | + |
| 338 | + if ((is_string($value) || is_int($value)) && is_subclass_of($enumClass, BackedEnum::class)) { |
| 339 | + $case = enum_try_from($enumClass, $value); |
| 340 | + |
| 341 | + if ($case !== null && $case->value === $value) { |
| 342 | + return $case; |
| 343 | + } |
| 344 | + } |
| 345 | + |
| 346 | + throw new InvalidArgumentException(sprintf( |
| 347 | + 'Configuration value for key [%s] must be a case or backing value of [%s], %s given.', |
| 348 | + $key, |
| 349 | + $enumClass, |
| 350 | + is_string($value) || is_int($value) ? var_export($value, true) : get_debug_type($value), |
| 351 | + )); |
| 352 | + } |
| 353 | + |
289 | 354 | /** |
290 | 355 | * Get the specified configuration value as a Collection. |
291 | 356 | * |
|
0 commit comments