Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.42% covered (warning)
68.42%
13 / 19
70.00% covered (warning)
70.00%
7 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
ValueObjectMetadata
68.42% covered (warning)
68.42%
13 / 19
70.00% covered (warning)
70.00%
7 / 10
18.32
0.00% covered (danger)
0.00%
0 / 1
 getValueOptions
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
5.67
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDisplayName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNativeType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 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
 toScalarType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getArrayItemType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowsNull
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
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 Apie\Core\ValueObjects\Interfaces\LimitedOptionsInterface;
11use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface;
12use Apie\Core\ValueObjects\Utils;
13use ReflectionClass;
14
15class ValueObjectMetadata implements NullableMetadataInterface
16{
17    // TODO: check regex to see if the options are limited?
18    public function getValueOptions(ApieContext $context, bool $runtimeFilter = false): ?ValueOptionList
19    {
20        if (in_array(LimitedOptionsInterface::class, $this->class->getInterfaceNames())) {
21            //$translator = $context->getContext(ApieTranslator::class, false) ? : new ApieTranslator();
22            $options = [];
23            foreach ($this->class->getMethod('getOptions')->invoke(null) as $option) {
24                $options[] = new ValueOption(Utils::toString($option), $option);
25            }
26            return new ValueOptionList($options);
27        }
28        return null;
29    }
30    /**
31     * @param ReflectionClass<ValueObjectInterface> $class
32     */
33    public function __construct(private ReflectionClass $class)
34    {
35    }
36
37    public function getDisplayName(): string
38    {
39        return $this->class->getShortName();
40    }
41
42    /**
43     * @return ReflectionClass<ValueObjectInterface>
44     */
45    public function toClass(): ReflectionClass
46    {
47        return $this->class;
48    }
49
50    public function getNativeType(): MetadataInterface
51    {
52        $method = $this->class->getMethod('toNative');
53        return MetadataFactory::getCreationMetadata($method->getReturnType(), new ApieContext());
54    }
55
56    public function getHashmap(): MetadataFieldHashmap
57    {
58        return $this->getNativeType()->getHashmap();
59    }
60
61    public function getRequiredFields(): StringList
62    {
63        return $this->getNativeType()->getRequiredFields();
64    }
65
66    public function toScalarType(bool $ignoreNull = false): ScalarType
67    {
68        return $this->getNativeType()->toScalarType($ignoreNull);
69    }
70
71    public function getArrayItemType(): ?MetadataInterface
72    {
73        return $this->getNativeType()->getArrayItemType();
74    }
75
76    public function allowsNull(): bool
77    {
78        $nativeType = $this->getNativeType();
79        if ($nativeType instanceof NullableMetadataInterface) {
80            return $nativeType->allowsNull();
81        }
82        return $nativeType->toScalarType() === ScalarType::NULLVALUE;
83    }
84}