Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| EnumArray | |
100.00% |
9 / 9 |
|
100.00% |
6 / 6 |
7 | |
100.00% |
1 / 1 |
| isValidName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getConstants | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isValidValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRandom | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Stdlib\Base |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Stdlib\Base; |
| 16 | |
| 17 | /** |
| 18 | * Enum class. |
| 19 | * |
| 20 | * Replacing the SplEnum class and providing basic enum. |
| 21 | * |
| 22 | * @package phpOMS\Stdlib\Base |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | abstract class EnumArray |
| 28 | { |
| 29 | /** |
| 30 | * Constants. |
| 31 | * |
| 32 | * @var array |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | protected static array $constants = []; |
| 36 | |
| 37 | /** |
| 38 | * Checking enum name. |
| 39 | * |
| 40 | * Checking if a certain const name exists (case sensitive) |
| 41 | * |
| 42 | * @param string $name Name of the value (case sensitive) |
| 43 | * |
| 44 | * @return bool |
| 45 | * |
| 46 | * @since 1.0.0 |
| 47 | */ |
| 48 | public static function isValidName(string $name) : bool |
| 49 | { |
| 50 | return isset(static::$constants[$name]); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Getting all constants of this enum. |
| 55 | * |
| 56 | * @return array |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | */ |
| 60 | public static function getConstants() : array |
| 61 | { |
| 62 | return static::$constants; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Check enum value. |
| 67 | * |
| 68 | * Checking if a given value is part of this enum |
| 69 | * |
| 70 | * @param mixed $value Value to check |
| 71 | * |
| 72 | * @return bool |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | public static function isValidValue(mixed $value) : bool |
| 77 | { |
| 78 | return \in_array($value, static::$constants, true); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get enum value by name. |
| 83 | * |
| 84 | * @param mixed $key Key to look for |
| 85 | * |
| 86 | * @return mixed |
| 87 | * |
| 88 | * @throws \OutOfBoundsException |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | */ |
| 92 | public static function get(mixed $key) : mixed |
| 93 | { |
| 94 | if (!isset(static::$constants[$key])) { |
| 95 | throw new \OutOfBoundsException('Key "' . $key . '" is not valid.'); |
| 96 | } |
| 97 | |
| 98 | return static::$constants[$key]; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Count enum variables |
| 103 | * |
| 104 | * @return int |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | */ |
| 108 | public static function count() : int |
| 109 | { |
| 110 | return \count(static::$constants); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get random enum value. |
| 115 | * |
| 116 | * @return mixed |
| 117 | * |
| 118 | * @since 1.0.0 |
| 119 | */ |
| 120 | public static function getRandom() : mixed |
| 121 | { |
| 122 | $keys = \array_keys(static::$constants); |
| 123 | |
| 124 | return static::$constants[$keys[\mt_rand(0, \count(static::$constants) - 1)]]; |
| 125 | } |
| 126 | } |