Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
InteractiveConsoleCommand
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 2
7.12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInputs
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
6.01
1<?php
2namespace Apie\IntegrationTests\Console;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\Dto\DtoInterface;
6use Apie\Core\Entities\EntityInterface;
7use Apie\Core\Metadata\Fields\DiscriminatorColumn;
8use Apie\Core\Metadata\Fields\StaticDiscriminatorColumn;
9use Apie\Core\Metadata\MetadataFactory;
10use ReflectionClass;
11
12class InteractiveConsoleCommand implements DtoInterface
13{
14    /**
15     * @param class-string<EntityInterface> $class
16     * @param array<string, array<int, int|float|string>> $inputPerField
17     */
18    public function __construct(
19        public readonly string $command,
20        public readonly string $class,
21        public readonly array $inputPerField,
22        public readonly ?string $polymorphicClass = null
23    ) {
24
25    }
26
27    /**
28     * @return array<int, string|int|float>
29     */
30    public function getInputs(): array
31    {
32        $metadata = MetadataFactory::getCreationMetadata(new ReflectionClass($this->polymorphicClass ?? $this->class), new ApieContext());
33        $inputs = [];
34        if ($this->polymorphicClass) {
35            $inputs[] = $this->polymorphicClass;
36        }
37        $handled = [];
38        foreach ($metadata->getHashmap() as $key => $mapping) {
39            if ($mapping instanceof DiscriminatorColumn || $mapping instanceof StaticDiscriminatorColumn) {
40                continue;
41            }
42            $handled[] = $key;
43            $inputs = [...$inputs, ...($this->inputPerField[$key] ?? [])];
44        }
45        $diff = array_diff($handled, array_keys($this->inputPerField));
46        if (!empty($diff)) {
47            throw new \LogicException('Mapping inputs failed, found unknown keys: ' . implode(', ', $diff));
48        }
49        return $inputs;
50    }
51}