1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium; |
4
|
|
|
|
5
|
|
|
use ErrorException; |
6
|
|
|
use Formularium\Exception\ClassNotFoundException; |
7
|
|
|
use Formularium\Extradata; |
8
|
|
|
use Formularium\ExtradataParameter; |
9
|
|
|
use Formularium\Field; |
10
|
|
|
use Formularium\Factory\ValidatorFactory; |
11
|
|
|
use Formularium\Metadata; |
12
|
|
|
use GraphQL\Language\AST\ArgumentNode; |
13
|
|
|
use GraphQL\Language\AST\DirectiveNode; |
14
|
|
|
use GraphQL\Language\AST\ListValueNode; |
15
|
|
|
use GraphQL\Language\AST\NodeList; |
16
|
|
|
use Modelarium\Exception\Exception; |
17
|
|
|
|
18
|
|
|
use function Safe\json_encode; |
19
|
|
|
|
20
|
|
|
class FormulariumUtils |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Undocumented function |
24
|
|
|
* |
25
|
|
|
* @param string $fieldName |
26
|
|
|
* @param string $datatypeName |
27
|
|
|
* @param NodeList<DirectiveNode> $directives |
28
|
|
|
* @return Field |
29
|
|
|
*/ |
30
|
10 |
|
public static function getFieldFromDirectives( |
31
|
|
|
string $fieldName, |
32
|
|
|
string $datatypeName, |
33
|
|
|
NodeList $directives |
34
|
|
|
): Field { |
35
|
10 |
|
$validators = []; |
36
|
10 |
|
$renderable = []; |
37
|
10 |
|
$extradata = []; |
38
|
10 |
|
foreach ($directives as $directive) { |
39
|
8 |
|
$name = $directive->name->value; |
40
|
|
|
|
41
|
8 |
|
if ($name === 'renderable') { |
42
|
|
|
foreach ($directive->arguments as $arg) { |
43
|
|
|
/** |
44
|
|
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
$argName = $arg->name->value; |
48
|
|
|
$argValue = $arg->value->value; /** @phpstan-ignore-line */ |
|
|
|
|
49
|
|
|
$renderable[$argName] = $argValue; |
50
|
|
|
} |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
$extradata[] = FormulariumUtils::directiveToExtradata($directive); |
55
|
|
|
|
56
|
8 |
|
$validator = null; |
|
|
|
|
57
|
|
|
try { |
58
|
8 |
|
$validator = ValidatorFactory::class($name); |
59
|
8 |
|
} catch (ClassNotFoundException $e) { |
60
|
8 |
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Metadata $metadata |
65
|
|
|
*/ |
66
|
|
|
$metadata = $validator::getMetadata(); |
67
|
|
|
$arguments = []; |
68
|
|
|
|
69
|
|
|
foreach ($directive->arguments as $arg) { |
70
|
|
|
/** |
71
|
|
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
$argName = $arg->name->value; |
75
|
|
|
$argValue = $arg->value->value; /** @phpstan-ignore-line */ |
76
|
|
|
|
77
|
|
|
$argValidator = $metadata->parameter($argName); |
78
|
|
|
if (!$argValidator) { |
79
|
|
|
throw new Exception("Directive $validator does not have argument $argName"); |
80
|
|
|
} |
81
|
|
|
if ($argValidator->type === 'Int') { |
82
|
|
|
$argValue = (int)$argValue; |
83
|
|
|
} |
84
|
|
|
$arguments[$argName] = $argValue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$validators[$name] = $arguments; |
88
|
|
|
} |
89
|
|
|
|
90
|
10 |
|
return new Field( |
91
|
10 |
|
$fieldName, |
92
|
|
|
$datatypeName, |
93
|
|
|
$renderable, |
94
|
|
|
$validators, |
95
|
|
|
$extradata |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
9 |
|
public static function directiveToExtradata(DirectiveNode $directive): Extradata |
100
|
|
|
{ |
101
|
9 |
|
$metadataArgs = []; |
102
|
9 |
|
foreach ($directive->arguments as $arg) { |
103
|
|
|
/** |
104
|
|
|
* @var ArgumentNode $arg |
105
|
|
|
*/ |
106
|
4 |
|
$v = $arg->value; |
107
|
4 |
|
$extradataV = null; |
108
|
4 |
|
if ($v instanceof ListValueNode) { |
109
|
|
|
$fields = []; |
110
|
|
|
foreach ($v->values as $i) { |
111
|
|
|
$fields[] = $i->value; /** @phpstan-ignore-line */ |
112
|
|
|
} |
113
|
|
|
$extradataV = json_encode($fields); // TODO: ExtradataParameter does not support array |
114
|
|
|
} else { |
115
|
4 |
|
$extradataV = $arg->value->value; /** @phpstan-ignore-line */ |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
$metadataArgs[] = new ExtradataParameter( |
119
|
4 |
|
$arg->name->value, |
120
|
|
|
$extradataV |
121
|
|
|
); |
122
|
|
|
} |
123
|
9 |
|
return new Extradata( |
124
|
9 |
|
$directive->name->value, |
125
|
|
|
$metadataArgs |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|