|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\Exception\ClassNotFoundException; |
|
6
|
|
|
use Formularium\Field; |
|
7
|
|
|
use Formularium\ValidatorFactory; |
|
8
|
|
|
use GraphQL\Language\AST\NodeList; |
|
9
|
|
|
use Modelarium\Exception\Exception; |
|
10
|
|
|
|
|
11
|
|
|
class FormulariumUtils |
|
12
|
|
|
{ |
|
13
|
|
|
public static function getFieldFromDirectives( |
|
14
|
|
|
string $fieldName, |
|
15
|
|
|
string $datatypeName, |
|
16
|
|
|
NodeList $directives |
|
17
|
|
|
): Field { |
|
18
|
|
|
$validators = []; |
|
19
|
|
|
$renderable = []; |
|
20
|
|
|
foreach ($directives as $directive) { |
|
21
|
|
|
$name = $directive->name->value; |
|
22
|
|
|
|
|
23
|
|
|
if ($name === 'renderable') { |
|
24
|
|
|
foreach ($directive->arguments as $arg) { |
|
25
|
|
|
/** |
|
26
|
|
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
$argName = $arg->name->value; |
|
30
|
|
|
$argValue = $arg->value->value; /** @phpstan-ignore-line */ |
|
|
|
|
|
|
31
|
|
|
$renderable[$argName] = $argValue; |
|
32
|
|
|
} |
|
33
|
|
|
continue; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$validator = null; |
|
|
|
|
|
|
37
|
|
|
try { |
|
38
|
|
|
$validator = ValidatorFactory::class($name); |
|
39
|
|
|
} catch (ClassNotFoundException $e) { |
|
40
|
|
|
continue; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ValidatorMetadata $metadata |
|
45
|
|
|
*/ |
|
46
|
|
|
$metadata = $validator::getMetadata(); |
|
47
|
|
|
$arguments = []; |
|
48
|
|
|
|
|
49
|
|
|
foreach ($directive->arguments as $arg) { |
|
50
|
|
|
/** |
|
51
|
|
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
|
52
|
|
|
*/ |
|
53
|
|
|
|
|
54
|
|
|
$argName = $arg->name->value; |
|
55
|
|
|
$argValue = $arg->value->value; /** @phpstan-ignore-line */ |
|
56
|
|
|
|
|
57
|
|
|
$argValidator = $metadata->argument($argName); |
|
58
|
|
|
if (!$argValidator) { |
|
59
|
|
|
throw new Exception("Directive $validator does not have argument $argName"); |
|
60
|
|
|
} |
|
61
|
|
|
if ($argValidator->type === 'Int') { |
|
62
|
|
|
$argValue = (int)$argValue; |
|
63
|
|
|
} |
|
64
|
|
|
$arguments[$argName] = $argValue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$validators[$name] = $arguments; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return new Field( |
|
71
|
|
|
$fieldName, |
|
72
|
|
|
$datatypeName, |
|
73
|
|
|
$renderable, |
|
74
|
|
|
$validators |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|