Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
42.11% covered (danger)
42.11%
8 / 19
72.73% covered (warning)
72.73%
8 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
DiscriminatorColumn
42.11% covered (danger)
42.11%
8 / 19
72.73% covered (warning)
72.73%
8 / 11
65.68
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
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValueForClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOptions
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
 allowsNull
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRequired
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 appliesToContext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTypehint
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFieldPriority
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAttributes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\Core\Metadata\Fields;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\Entities\PolymorphicEntityInterface;
6use Apie\Core\Metadata\GetterInterface;
7use Apie\Core\Other\DiscriminatorMapping;
8use Apie\TypeConverter\ReflectionTypeFactory;
9use ReflectionClass;
10use ReflectionType;
11
12class DiscriminatorColumn implements FieldInterface, GetterInterface
13{
14    public function __construct(private DiscriminatorMapping $discriminatorMapping)
15    {
16    }
17
18    public function getValue(object $object, ApieContext $apieContext): mixed
19    {
20        return $this->discriminatorMapping->getDiscriminatorForObject($object);
21    }
22
23    /**
24     * @param ReflectionClass<PolymorphicEntityInterface> $class
25     */
26    public function getValueForClass(ReflectionClass $class): string
27    {
28        return $this->discriminatorMapping->getDiscriminatorForClass($class);
29    }
30
31    /** @return array<string, string> */
32    public function getOptions(ApieContext $apieContext, bool $runtimeFilter = false): array
33    {
34        $options = [];
35        foreach ($this->discriminatorMapping->getConfigs() as $config) {
36            $refl = new ReflectionClass($config->getClassName());
37            $constructor = $refl->getConstructor();
38            if (!$runtimeFilter
39                || $apieContext->appliesToContext($refl)
40                || ($constructor === null || $apieContext->appliesToContext($constructor))) {
41                $options[$config->getDiscriminator()] = $refl->name;
42            }
43        }
44        return $options;
45    }
46
47    public function allowsNull(): bool
48    {
49        return false;
50    }
51
52    public function isRequired(): bool
53    {
54        return true;
55    }
56
57    public function isField(): bool
58    {
59        return true;
60    }
61
62    public function appliesToContext(ApieContext $apieContext): bool
63    {
64        return true;
65    }
66
67    public function getTypehint(): ?ReflectionType
68    {
69        return ReflectionTypeFactory::createReflectionType('string');
70    }
71
72    public function getFieldPriority(): int
73    {
74        return -280;
75    }
76
77    public function getAttributes(string $attributeClass, bool $classDocBlock = true, bool $propertyDocblock = true, bool $argumentDocBlock = true): array
78    {
79        return [];
80    }
81}