1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Portiny\GraphQL\Converter; |
6
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
8
|
|
|
use Portiny\GraphQL\Contract\Field\QueryFieldInterface; |
9
|
|
|
|
10
|
|
|
|
11
|
|
View Code Duplication |
final class QueryFieldConverter |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
3 |
|
public static function toArray(QueryFieldInterface $queryField): array |
15
|
|
|
{ |
16
|
|
|
return [ |
17
|
3 |
|
$queryField->getName() => [ |
18
|
3 |
|
'type' => $queryField->getType(), |
19
|
3 |
|
'description' => $queryField->getDescription(), |
20
|
3 |
|
'args' => $queryField->getArgs(), |
21
|
3 |
|
'resolve' => function ($root, $args, $context) use ($queryField) { |
22
|
1 |
|
return call_user_func_array([$queryField, 'resolve'], [$root, $args, $context]); |
23
|
3 |
|
} |
24
|
|
|
] |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public static function toObject(array $data): QueryFieldInterface |
30
|
|
|
{ |
31
|
|
|
return (new class ($data) implements QueryFieldInterface |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $name; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $data; |
43
|
|
|
|
44
|
|
|
|
45
|
1 |
|
public function __construct(array $data) |
46
|
|
|
{ |
47
|
1 |
|
$this->name = key($data); |
48
|
1 |
|
$this->data = reset($data); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function getName(): string |
56
|
|
|
{ |
57
|
1 |
|
return $this->name; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function getType(): Type |
65
|
|
|
{ |
66
|
1 |
|
return $this->data['type']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function getDescription(): string |
74
|
|
|
{ |
75
|
1 |
|
return $this->data['description']; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
1 |
|
public function getArgs(): array |
83
|
|
|
{ |
84
|
1 |
|
return $this->data['args']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
1 |
|
public function resolve(array $root, array $args, $context = NULL) |
92
|
|
|
{ |
93
|
1 |
|
return $this->data['resolve']($root, $args, $context); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.