|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Formularium; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\Exception\Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Abstract base class for frameworks. Each framework should have a class inheriting |
|
9
|
|
|
* from this class. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Formularium |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Returns a list of datatype class names |
|
15
|
|
|
* |
|
16
|
2 |
|
* @return array |
|
17
|
|
|
*/ |
|
18
|
2 |
|
public static function getDatatypeNames(): array |
|
19
|
|
|
{ |
|
20
|
2 |
|
$files = scandir(__DIR__ . '/Datatype/'); |
|
21
|
2 |
|
if (!$files) { |
|
22
|
2 |
|
throw new Exception('Datatypes not found'); |
|
23
|
|
|
} |
|
24
|
|
|
$datatypes = array_map( |
|
25
|
|
|
function ($x) { |
|
26
|
2 |
|
return str_replace('Datatype_', '', str_replace('.php', '', $x)); |
|
27
|
2 |
|
}, |
|
28
|
|
|
array_diff($files, array('.', '..')) |
|
29
|
2 |
|
); |
|
30
|
2 |
|
|
|
31
|
|
|
// TODO: avoid abstract classes |
|
32
|
|
|
$datatypes = array_filter( |
|
33
|
2 |
|
$datatypes, |
|
34
|
|
|
function ($t) { |
|
35
|
|
|
return ($t !== 'number' && $t !== 'choice' && $t !== 'association'); |
|
36
|
1 |
|
} |
|
37
|
|
|
); |
|
38
|
1 |
|
|
|
39
|
|
|
return $datatypes; |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
/** |
|
43
|
1 |
|
* Returns a list of datatype class names |
|
44
|
1 |
|
* |
|
45
|
|
|
* @return array |
|
46
|
1 |
|
*/ |
|
47
|
|
|
public static function getValidatorNames(): array |
|
48
|
|
|
{ |
|
49
|
|
|
$files = scandir(__DIR__ . '/Validator/'); |
|
50
|
1 |
|
if (!$files) { |
|
51
|
|
|
throw new Exception('Validators not found'); |
|
52
|
|
|
} |
|
53
|
1 |
|
$validators = array_map( |
|
54
|
|
|
function ($x) { |
|
55
|
1 |
|
return str_replace('.php', '', $x); |
|
56
|
|
|
}, |
|
57
|
1 |
|
array_diff($files, array('.', '..')) |
|
58
|
1 |
|
); |
|
59
|
|
|
|
|
60
|
|
|
return $validators; |
|
61
|
|
|
} |
|
62
|
1 |
|
|
|
63
|
|
|
public static function getDatatypeValidators(): array |
|
64
|
1 |
|
{ |
|
65
|
|
|
$datatypes = self::getDatatypeNames(); |
|
66
|
|
|
|
|
67
|
1 |
|
$validators = []; |
|
68
|
1 |
|
|
|
69
|
|
|
foreach ($datatypes as $name) { |
|
70
|
|
|
$datatype = Datatype::factory($name); |
|
71
|
1 |
|
$validators = array_merge($validators, $datatype->getValidatorMetadata()); |
|
72
|
1 |
|
} |
|
73
|
1 |
|
|
|
74
|
|
|
$classes = static::getValidatorNames(); |
|
75
|
1 |
|
foreach ($classes as $name) { |
|
76
|
|
|
$v = Validator::factory($name); |
|
77
|
1 |
|
$graphql[] = $v->getMetadata()->toGraphql(); |
|
78
|
|
|
} |
|
79
|
1 |
|
|
|
80
|
|
|
return $validators; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public static function validatorGraphqlDirectives(): string |
|
84
|
1 |
|
{ |
|
85
|
|
|
$classes = static::getValidatorNames(); |
|
86
|
|
|
$graphql = []; |
|
87
|
|
|
foreach ($classes as $name) { |
|
88
|
|
|
$v = Validator::factory($name); |
|
89
|
|
|
$graphql[] = $v->getMetadata()->toGraphql(); |
|
90
|
|
|
} |
|
91
|
1 |
|
|
|
92
|
|
|
return ' |
|
93
|
|
|
# File generated by Formularium. |
|
94
|
|
|
# Do not edit this file directly. |
|
95
|
|
|
|
|
96
|
|
|
' . join("\n", $graphql); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function scalarGraphqlDirectives(): string |
|
100
|
|
|
{ |
|
101
|
|
|
$classes = static::getDatatypeNames(); |
|
102
|
|
|
$graphql = []; |
|
103
|
|
|
foreach ($classes as $name) { |
|
104
|
|
|
$v = Datatype::factory($name); |
|
105
|
|
|
$className = get_class($v); |
|
106
|
|
|
$graphql[] = "scalar $name @scalar(class: \"{$className}\")"; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return ' |
|
110
|
|
|
# File generated by Formularium. |
|
111
|
|
|
# Do not edit this file directly. |
|
112
|
|
|
|
|
113
|
|
|
' . join("\n\n", $graphql); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
public static function graphqlDirectives(): string |
|
119
|
|
|
{ |
|
120
|
|
|
// TODO: review with ValidatorMetadata; |
|
121
|
|
|
$validators = static::getDatatypeValidators(); |
|
122
|
|
|
$graphql = []; |
|
123
|
|
|
|
|
124
|
|
|
foreach ($validators as $name => $v) { |
|
125
|
|
|
$args = array_map( |
|
126
|
|
|
function ($a) { |
|
127
|
|
|
return <<<EOF |
|
128
|
|
|
""" |
|
129
|
|
|
{$a['comment']} |
|
130
|
|
|
""" |
|
131
|
|
|
{$a['name']}: {$a['type']} |
|
132
|
|
|
|
|
133
|
|
|
EOF; |
|
134
|
|
|
}, |
|
135
|
|
|
$v['args'] |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
$argString = ''; |
|
139
|
|
|
if ($args) { |
|
|
|
|
|
|
140
|
|
|
$argString = "(\n" . join("\n", $args) . ')'; |
|
141
|
|
|
} |
|
142
|
|
|
$graphql[$name] = <<< EOF |
|
143
|
|
|
""" |
|
144
|
|
|
{$v['comment']} |
|
145
|
|
|
""" |
|
146
|
|
|
directive @{$name}{$argString} on FIELD_DEFINITION |
|
147
|
|
|
|
|
148
|
|
|
EOF; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
unset($graphql['required']); // ! already does this |
|
152
|
|
|
|
|
153
|
|
|
return ' |
|
154
|
|
|
# File generated by Formularium. |
|
155
|
|
|
# Do not edit this file directly. |
|
156
|
|
|
|
|
157
|
|
|
' . join("\n", $graphql); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.