Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Enum
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
8 / 8
9
100.00% covered (success)
100.00%
1 / 1
 isValidValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getConstants
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getRandom
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getByName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isValidName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 count
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasFlag
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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 */
13declare(strict_types=1);
14
15namespace 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 */
27abstract class Enum
28{
29    /**
30     * Check enum value.
31     *
32     * Checking if a given value is part of this enum
33     *
34     * @param mixed $value Value to check
35     *
36     * @return bool
37     *
38     * @since 1.0.0
39     */
40    public static function isValidValue(mixed $value) : bool
41    {
42        $reflect   = new \ReflectionClass(static::class);
43        $constants = $reflect->getConstants();
44
45        return \in_array($value, $constants, true);
46    }
47
48    /**
49     * Getting all constants of this enum.
50     *
51     * @return array
52     *
53     * @since 1.0.0
54     */
55    public static function getConstants() : array
56    {
57        $reflect = new \ReflectionClass(static::class);
58
59        return $reflect->getConstants();
60    }
61
62    /**
63     * Get random enum value.
64     *
65     * @return mixed
66     *
67     * @since 1.0.0
68     */
69    public static function getRandom() : mixed
70    {
71        $reflect   = new \ReflectionClass(static::class);
72        $constants = $reflect->getConstants();
73        $keys      = \array_keys($constants);
74
75        return $constants[$keys[\mt_rand(0, \count($constants) - 1)]];
76    }
77
78    /**
79     * Get enum value by name.
80     *
81     * @param string $name Enum name
82     *
83     * @return mixed
84     *
85     * @since 1.0.0
86     */
87    public static function getByName(string $name) : mixed
88    {
89        if (!self::isValidName($name)) {
90            return null;
91        }
92
93        return \constant('static::' . $name);
94    }
95
96    /**
97     * Get enum name by value.
98     *
99     * @param string $value Enum value
100     *
101     * @return false|int|string
102     *
103     * @since 1.0.0
104     */
105    public static function getName(string $value) : bool | int | string
106    {
107        $reflect   = new \ReflectionClass(static::class);
108        $constants = $reflect->getConstants();
109
110        return \array_search($value, $constants);
111    }
112
113    /**
114     * Checking enum name.
115     *
116     * Checking if a certain const name exists (case sensitive)
117     *
118     * @param string $name Name of the value (case sensitive)
119     *
120     * @return bool
121     *
122     * @since 1.0.0
123     */
124    public static function isValidName(string $name) : bool
125    {
126        return \defined('static::' . $name);
127    }
128
129    /**
130     * Count enum variables
131     *
132     * @return int
133     *
134     * @since 1.0.0
135     */
136    public static function count() : int
137    {
138        return \count(self::getConstants());
139    }
140
141    /**
142     * Check if flag is set
143     *
144     * This only works for binary flags.
145     *
146     * @param int $flags        Set flags
147     * @param int $checkForFlag Check if this flag is part of the set flags
148     *
149     * @return bool
150     *
151     * @since 1.0.0
152     */
153    public static function hasFlag(int $flags, int $checkForFlag) : bool
154    {
155        return ($flags & $checkForFlag) === $checkForFlag;
156    }
157}