| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Classes; |
| 4 |
|
| 5 |
use ReflectionClass; |
| 6 |
use ReflectionException; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
abstract class Basic_Enum { |
| 13 |
private static array $entries = []; |
| 14 |
|
| 15 |
/** |
| 16 |
* @throws ReflectionException |
| 17 |
*/ |
| 18 |
public static function get_values(): array { |
| 19 |
return array_values( self::get_entries() ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Whether the given value is a defined enum member. |
| 24 |
* |
| 25 |
* @param mixed $value |
| 26 |
* @return bool |
| 27 |
* @throws ReflectionException |
| 28 |
*/ |
| 29 |
public static function is_valid( $value ): bool { |
| 30 |
return in_array( $value, self::get_values(), true ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @throws ReflectionException |
| 35 |
*/ |
| 36 |
protected static function get_entries(): array { |
| 37 |
$caller = get_called_class(); |
| 38 |
|
| 39 |
if ( ! array_key_exists( $caller, self::$entries ) ) { |
| 40 |
$reflect = new ReflectionClass( $caller ); |
| 41 |
self::$entries[ $caller ] = $reflect->getConstants(); |
| 42 |
} |
| 43 |
|
| 44 |
return self::$entries[ $caller ]; |
| 45 |
} |
| 46 |
} |
| 47 |
|