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