1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Softonic\GraphQL; |
4
|
|
|
|
5
|
|
|
use Softonic\GraphQL\Config\MutationConfigObject; |
6
|
|
|
use Softonic\GraphQL\Mutation\Collection as MutationCollection; |
7
|
|
|
use Softonic\GraphQL\Mutation\Item as MutationItem; |
8
|
|
|
use Softonic\GraphQL\Mutation\MutationObject; |
9
|
|
|
use Softonic\GraphQL\Query\Collection as QueryCollection; |
10
|
|
|
use Softonic\GraphQL\Query\Item as QueryItem; |
11
|
|
|
use Softonic\GraphQL\Query\ReadObject; |
12
|
|
|
|
13
|
|
|
class MutationBuilder |
14
|
|
|
{ |
15
|
|
|
const MUTATION_TYPE_MAP = [ |
16
|
|
|
QueryItem::class => MutationItem::class, |
17
|
|
|
QueryCollection::class => MutationCollection::class, |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
const DEFAULT_MUTATION_TYPE = MutationItem::class; |
21
|
|
|
|
22
|
|
|
private $config; |
23
|
|
|
|
24
|
|
|
private $source; |
25
|
|
|
|
26
|
48 |
|
public function __construct(MutationConfigObject $config, QueryItem $source) |
27
|
|
|
{ |
28
|
48 |
|
$this->config = $config; |
29
|
48 |
|
$this->source = $source; |
30
|
48 |
|
} |
31
|
|
|
|
32
|
48 |
|
public function build(): MutationItem |
33
|
|
|
{ |
34
|
48 |
|
$path = $this->config->linksTo; |
35
|
48 |
|
$config = $this->config->get($path); |
36
|
|
|
|
37
|
48 |
|
$mutationType = $this->getMutationType($config, $this->source); |
38
|
48 |
|
$arguments = $this->generateMutationArguments($config, $this->source, $path); |
39
|
|
|
|
40
|
48 |
|
return new $mutationType($arguments, $config->children); |
41
|
|
|
} |
42
|
|
|
|
43
|
48 |
|
private function getMutationType(MutationConfigObject $config, ReadObject $source): string |
44
|
|
|
{ |
45
|
48 |
|
return !empty($config->linksTo) ? self::MUTATION_TYPE_MAP[get_class($source)] : self::DEFAULT_MUTATION_TYPE; |
46
|
|
|
} |
47
|
|
|
|
48
|
48 |
|
private function generateMutationArguments(MutationConfigObject $config, ReadObject $source, string $path): array |
49
|
|
|
{ |
50
|
48 |
|
$arguments = []; |
51
|
48 |
|
foreach ($source as $sourceKey => $sourceValue) { |
|
|
|
|
52
|
48 |
|
if ($sourceValue instanceof ReadObject) { |
53
|
40 |
|
$childPath = $this->createPathFromParent($path, $sourceKey); |
54
|
|
|
|
55
|
40 |
|
$arguments[$sourceKey] = $this->mutateChild($config->children[$sourceKey], $sourceValue, $childPath); |
56
|
|
|
} else { |
57
|
48 |
|
$arguments[$sourceKey] = $sourceValue; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
48 |
|
return $arguments; |
62
|
|
|
} |
63
|
|
|
|
64
|
40 |
|
private function createPathFromParent(string $parent, string $child): string |
65
|
|
|
{ |
66
|
40 |
|
return ('.' === $parent) ? ".{$child}" : "{$parent}.{$child}"; |
67
|
|
|
} |
68
|
|
|
|
69
|
40 |
|
private function mutateChild(MutationConfigObject $config, ReadObject $source, string $path): MutationObject |
70
|
|
|
{ |
71
|
40 |
|
if (is_null($config->linksTo)) { |
72
|
40 |
|
$arguments = []; |
73
|
40 |
|
foreach ($config->children as $key => $childConfig) { |
74
|
40 |
|
$childArguments = []; |
75
|
40 |
|
foreach ($source as $sourceKey => $sourceValue) { |
|
|
|
|
76
|
38 |
|
$childChildrenType = $this->getMutationType($childConfig, $sourceValue); |
77
|
38 |
|
$childChildrenArguments = $this->generateMutationArguments($childConfig, $sourceValue, $path); |
78
|
|
|
|
79
|
38 |
|
$childArguments[$sourceKey] = new $childChildrenType( |
80
|
38 |
|
$childChildrenArguments, |
81
|
38 |
|
$childConfig->children |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
40 |
|
$childType = $this->getMutationType($childConfig, $source); |
86
|
|
|
|
87
|
40 |
|
$arguments[$key] = new $childType($childArguments, $childConfig->children); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
40 |
|
$type = $this->getMutationType($config, $source); |
92
|
|
|
|
93
|
40 |
|
return new $type($arguments, $config->children); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|