Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.48% covered (success)
90.48%
19 / 21
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
EnumMetadata
90.48% covered (success)
90.48%
19 / 21
75.00% covered (warning)
75.00%
6 / 8
15.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHashmap
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequiredFields
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOptions
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 toScalarType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getArrayItemType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValueOptions
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2namespace Apie\Core\Metadata;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\Context\MetadataFieldHashmap;
6use Apie\Core\Dto\ValueOption;
7use Apie\Core\Enums\ScalarType;
8use Apie\Core\Lists\StringList;
9use Apie\Core\Lists\ValueOptionList;
10use ReflectionEnum;
11use UnitEnum;
12
13class EnumMetadata implements MetadataInterface
14{
15    /**
16     * @param ReflectionEnum<UnitEnum> $enum
17     */
18    public function __construct(private ReflectionEnum $enum)
19    {
20    }
21
22    /**
23     * @return ReflectionEnum<UnitEnum>
24     */
25    public function toClass(): ReflectionEnum
26    {
27        return $this->enum;
28    }
29
30    public function getHashmap(): MetadataFieldHashmap
31    {
32        return new MetadataFieldHashmap();
33    }
34
35    public function getRequiredFields(): StringList
36    {
37        return new StringList([]);
38    }
39
40    /** @return array<string|int, string|int> */
41    public function getOptions(ApieContext $apieContext, bool $runtimeFilter = false): array
42    {
43        $cases = $this->enum->getCases();
44        $result = [];
45        foreach ($cases as $case) {
46            if ($apieContext->appliesToContext($case, $runtimeFilter)) {
47                $result[$case->getName()] = $this->enum->isBacked() ? $case->getBackingValue() : $case->getName();
48            }
49        }
50        return $result;
51    }
52
53    public function toScalarType(): ScalarType
54    {
55        return $this->enum->isBacked() ? ScalarType::from((string) $this->enum->getBackingType()) : ScalarType::STRING;
56    }
57
58    public function getArrayItemType(): ?MetadataInterface
59    {
60        return null;
61    }
62
63    public function getValueOptions(ApieContext $context, bool $runtimeFilter = false): ?ValueOptionList
64    {
65        $cases = $this->enum->getCases();
66        $result = [];
67        foreach ($cases as $case) {
68            if ($context->appliesToContext($case, $runtimeFilter)) {
69                $result[] = new ValueOption(
70                    $this->enum->isBacked() ? $case->getBackingValue() : $case->getName(),
71                    $case->getValue()
72                );
73            }
74        }
75        return new ValueOptionList($result);
76    }
77}