|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Softonic\GraphQL; |
|
4
|
|
|
|
|
5
|
|
|
use Softonic\GraphQL\Config\MutationTypeConfig; |
|
6
|
|
|
use Softonic\GraphQL\DataObjects\Mutation\Collection as MutationCollection; |
|
7
|
|
|
use Softonic\GraphQL\DataObjects\Mutation\Item as MutationItem; |
|
8
|
|
|
use Softonic\GraphQL\DataObjects\Mutation\MutationObject; |
|
9
|
|
|
use Softonic\GraphQL\DataObjects\Query\Collection as QueryCollection; |
|
10
|
|
|
use Softonic\GraphQL\DataObjects\Query\Item as QueryItem; |
|
11
|
|
|
use Softonic\GraphQL\DataObjects\Query\QueryObject; |
|
12
|
|
|
|
|
13
|
|
|
class Mutation |
|
14
|
|
|
{ |
|
15
|
|
|
const SOURCE_ROOT_PATH = '.'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $hasChanged; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var MutationTypeConfig |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $mutationTypeConfig; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array<MutationTypeConfig> $config |
|
34
|
|
|
*/ |
|
35
|
98 |
|
public static function build(array $config, QueryObject $source, bool $fromMutation = false): MutationObject |
|
36
|
|
|
{ |
|
37
|
98 |
|
static::$config = $config; |
|
|
|
|
|
|
38
|
98 |
|
static::$hasChanged = $fromMutation; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
98 |
|
$mutationVariables = []; |
|
41
|
98 |
|
foreach (static::$config as $variableName => $mutationTypeConfig) { |
|
|
|
|
|
|
42
|
96 |
|
static::$mutationTypeConfig = $mutationTypeConfig; |
|
|
|
|
|
|
43
|
96 |
|
$path = self::SOURCE_ROOT_PATH; |
|
44
|
96 |
|
$config = $mutationTypeConfig->get($path); |
|
45
|
96 |
|
if ($config->type === MutationCollection::class) { |
|
46
|
6 |
|
$arguments = []; |
|
47
|
6 |
|
foreach ($source as $sourceItem) { |
|
|
|
|
|
|
48
|
6 |
|
$mutationItemArguments = static::generateMutationArguments($sourceItem, $path); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
6 |
|
$arguments[] = new MutationItem($mutationItemArguments, $config->children, static::$hasChanged); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
6 |
|
$mutationVariables[$variableName] = new $config->type( |
|
54
|
6 |
|
$arguments, |
|
55
|
6 |
|
$config->children, |
|
56
|
6 |
|
static::$hasChanged |
|
|
|
|
|
|
57
|
|
|
); |
|
58
|
|
|
} else { |
|
59
|
90 |
|
$arguments = static::generateMutationArguments($source, $path); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
90 |
|
$mutationVariables[$variableName] = new $config->type( |
|
62
|
90 |
|
$arguments, |
|
63
|
90 |
|
$config->children, |
|
64
|
90 |
|
static::$hasChanged |
|
|
|
|
|
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
98 |
|
return new MutationItem($mutationVariables, static::$config, static::$hasChanged); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
96 |
|
private static function generateMutationArguments(QueryItem $source, string $path): array |
|
73
|
|
|
{ |
|
74
|
96 |
|
$arguments = []; |
|
75
|
96 |
|
foreach ($source as $sourceKey => $sourceValue) { |
|
76
|
96 |
|
$childPath = static::createPathFromParent($path, $sourceKey); |
|
|
|
|
|
|
77
|
96 |
|
$childConfig = static::$mutationTypeConfig->get($childPath); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
96 |
|
if (is_null($childConfig)) { |
|
80
|
4 |
|
break; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
96 |
|
if ($sourceValue instanceof QueryObject) { |
|
84
|
74 |
|
if ($sourceValue instanceof QueryCollection) { |
|
85
|
74 |
|
$arguments[$sourceKey] = static::mutateChild($childConfig, $sourceValue, $childPath); |
|
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
4 |
|
$mutationItemArguments = static::generateMutationArguments($sourceValue, $childPath); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
4 |
|
$arguments[$sourceKey] = new MutationItem( |
|
90
|
4 |
|
$mutationItemArguments, |
|
91
|
4 |
|
$childConfig->children, |
|
92
|
74 |
|
static::$hasChanged |
|
|
|
|
|
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
96 |
|
} elseif ($childConfig->type === MutationTypeConfig::SCALAR_DATA_TYPE) { |
|
96
|
96 |
|
$arguments[$sourceKey] = $sourceValue; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
96 |
|
return $arguments; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
96 |
|
private static function createPathFromParent(string $parent, string $child): string |
|
104
|
|
|
{ |
|
105
|
96 |
|
return ('.' === $parent) ? ".{$child}" : "{$parent}.{$child}"; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
74 |
|
private static function mutateChild( |
|
109
|
|
|
MutationTypeConfig $config, |
|
110
|
|
|
QueryCollection $sourceCollection, |
|
111
|
|
|
string $path |
|
112
|
|
|
): MutationObject { |
|
113
|
74 |
|
$arguments = []; |
|
114
|
74 |
|
if (is_null($config->linksTo)) { |
|
115
|
66 |
|
foreach ($config->children as $key => $childConfig) { |
|
116
|
66 |
|
$childPath = static::createPathFromParent($path, $key); |
|
|
|
|
|
|
117
|
66 |
|
$arguments[$key] = static::mutateChild($childConfig, $sourceCollection, $childPath); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
} else { |
|
120
|
74 |
|
foreach ($sourceCollection as $sourceItem) { |
|
121
|
72 |
|
$itemArguments = static::generateMutationArguments($sourceItem, $path); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
72 |
|
$arguments[] = new MutationItem($itemArguments, $config->children, static::$hasChanged); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
74 |
|
return new $config->type($arguments, $config->children, static::$hasChanged); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: