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