Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
31.58% |
6 / 19 |
|
54.55% |
6 / 11 |
CRAP | |
0.00% |
0 / 1 |
| DiscriminatorColumn | |
31.58% |
6 / 19 |
|
54.55% |
6 / 11 |
98.00 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getValueForClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOptions | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
| allowsNull | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isRequired | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| appliesToContext | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTypehint | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFieldPriority | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAttributes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Metadata\Fields; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Entities\PolymorphicEntityInterface; |
| 6 | use Apie\Core\Metadata\GetterInterface; |
| 7 | use Apie\Core\Other\DiscriminatorMapping; |
| 8 | use Apie\TypeConverter\ReflectionTypeFactory; |
| 9 | use ReflectionClass; |
| 10 | use ReflectionType; |
| 11 | |
| 12 | class 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 | } |