Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
75.00% |
12 / 16 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
EnumUtils | |
75.00% |
12 / 16 |
|
25.00% |
1 / 4 |
10.27 | |
0.00% |
0 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
getValues | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
isEnum | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
isStringEnum | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
isIntEnum | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 |
1 | <?php |
2 | namespace Apie\Core\Utils; |
3 | |
4 | use ReflectionClass; |
5 | use ReflectionEnum; |
6 | use ReflectionMethod; |
7 | use ReflectionProperty; |
8 | use ReflectionType; |
9 | use UnitEnum; |
10 | |
11 | final class EnumUtils |
12 | { |
13 | /** |
14 | * @codeCoverageIgnore |
15 | */ |
16 | private function __construct() |
17 | { |
18 | } |
19 | |
20 | /** |
21 | * @param ReflectionEnum<UnitEnum> $enumClass |
22 | * @return array<int, string> |
23 | */ |
24 | public static function getValues(ReflectionEnum $enumClass): array |
25 | { |
26 | $result = []; |
27 | foreach ($enumClass->getCases() as $enumCase) { |
28 | $result[$enumCase->getValue()->value ?? $enumCase->getValue()->name] = $enumCase->getValue()->name; |
29 | } |
30 | return $result; |
31 | } |
32 | |
33 | /** |
34 | * @param string|ReflectionClass<object>|ReflectionProperty|ReflectionType|ReflectionMethod $input |
35 | */ |
36 | public static function isEnum(string|ReflectionClass|ReflectionProperty|ReflectionType|ReflectionMethod $input): bool |
37 | { |
38 | $class = ConverterUtils::toReflectionClass($input); |
39 | return $class->isEnum(); |
40 | } |
41 | |
42 | /** |
43 | * @param string|ReflectionClass<object>|ReflectionProperty|ReflectionType|ReflectionMethod $input |
44 | */ |
45 | public static function isStringEnum(string|ReflectionClass|ReflectionProperty|ReflectionType|ReflectionMethod $input): bool |
46 | { |
47 | $class = ConverterUtils::toReflectionClass($input); |
48 | if (!$class->isEnum()) { |
49 | return false; |
50 | } |
51 | $enum = new ReflectionEnum($class->name); |
52 | return (!$enum->getBackingType() || $enum->getBackingType()->getName() === 'string'); |
53 | } |
54 | |
55 | /** |
56 | * @param string|ReflectionClass<object>|ReflectionProperty|ReflectionType|ReflectionMethod $input |
57 | */ |
58 | public static function isIntEnum(string|ReflectionClass|ReflectionProperty|ReflectionType|ReflectionMethod $input): bool |
59 | { |
60 | $class = ConverterUtils::toReflectionClass($input); |
61 | if (!$class->isEnum()) { |
62 | return false; |
63 | } |
64 | $enum = new ReflectionEnum($class->name); |
65 | return $enum->getBackingType()?->getName() === 'int'; |
66 | } |
67 | } |