Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
StringToEnum | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |
0.00% |
0 / 1 |
convert | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 |
1 | <?php |
2 | namespace Apie\StorageMetadata\Converters; |
3 | |
4 | use Apie\Core\Utils\ConverterUtils; |
5 | use Apie\TypeConverter\ConverterInterface; |
6 | use BackedEnum; |
7 | use ReflectionType; |
8 | |
9 | /** |
10 | * @implements ConverterInterface<string, BackedEnum> |
11 | */ |
12 | class StringToEnum implements ConverterInterface |
13 | { |
14 | public function convert(string $input, ?ReflectionType $wantedType): ?BackedEnum |
15 | { |
16 | $class = ConverterUtils::toReflectionClass($wantedType); |
17 | assert(null !== $class); |
18 | $className = $class->name; |
19 | if ($wantedType->allowsNull()) { |
20 | // @phpstan-ignore-next-line |
21 | return $className::tryFrom($input); |
22 | } |
23 | // @phpstan-ignore-next-line |
24 | return $className::from($input); |
25 | } |
26 | } |