Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
FromMetadataInputType
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2namespace Apie\Graphql\Types;
3
4use Apie\Core\Attributes\Description;
5use Apie\Core\Metadata\MetadataInterface;
6use Apie\Core\ValueObjects\Utils;
7use Apie\Graphql\Concerns\CreatesFromMeta;
8use GraphQL\Type\Definition\InputObjectType;
9use GraphQL\Type\Definition\Type;
10use ReflectionAttribute;
11
12class FromMetadataInputType extends InputObjectType
13{
14    use CreatesFromMeta;
15
16    public function __construct(MetadataInterface $metadata, string $suffix = '')
17    {
18        $class = $metadata->toClass();
19        $name = $class ? Utils::getDisplayNameForValueObject($class) : $metadata->toScalarType()->value;
20        $config = [
21            'name' => $name . $suffix,
22            'fields' => [
23            ],
24        ];
25        
26        foreach ($metadata->toClass()?->getAttributes(Description::class, ReflectionAttribute::IS_INSTANCEOF) ?? [] as $descriptionAttribute) {
27            $description = $descriptionAttribute->newInstance();
28            $config['description'] = $description->description;
29        }
30        foreach ($metadata->getHashmap() as $name => $field) {
31            if ($field->isField()) {
32                $config['fields'][$name] = [
33                    'type' => self::createFromField($field),
34                ];
35                foreach ($field->getAttributes(Description::class) as $description) {
36                    $config['fields'][$name]['description'] = $description->description;
37                }
38            }
39        }
40        if (empty($config['fields'])) {
41            $config['fields']['_'] = [
42                'type' => Type::string(),
43                'description' => 'This is a dummy field, because GraphQL does not allow empty input types.',
44            ];
45        }
46        parent::__construct($config);
47    }
48}