1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Config; |
6
|
|
|
|
7
|
|
|
use Overblog\GraphQLBundle\DependencyInjection\Configuration; |
8
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
9
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
10
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeParentInterface; |
11
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
12
|
|
|
|
13
|
|
|
abstract class TypeDefinition |
14
|
|
|
{ |
15
|
|
|
public const VALIDATION_LEVEL_CLASS = 0; |
16
|
|
|
public const VALIDATION_LEVEL_PROPERTY = 1; |
17
|
|
|
|
18
|
|
|
abstract public function getDefinition(); |
19
|
|
|
|
20
|
47 |
|
protected function __construct() |
21
|
|
|
{ |
22
|
47 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return static |
26
|
|
|
*/ |
27
|
47 |
|
public static function create() |
28
|
|
|
{ |
29
|
47 |
|
return new static(); |
30
|
|
|
} |
31
|
|
|
|
32
|
47 |
|
protected function resolveTypeSection() |
33
|
|
|
{ |
34
|
47 |
|
$node = self::createNode('resolveType', 'variable'); |
35
|
|
|
|
36
|
47 |
|
return $node; |
37
|
|
|
} |
38
|
|
|
|
39
|
47 |
|
protected function nameSection() |
40
|
|
|
{ |
41
|
47 |
|
$node = self::createNode('name', 'scalar'); |
42
|
47 |
|
$node->isRequired(); |
43
|
47 |
|
$node->validate() |
44
|
47 |
|
->ifTrue(fn ($name) => !\preg_match('/^[_a-z][_0-9a-z]*$/i', $name)) |
45
|
47 |
|
->thenInvalid('Invalid type name "%s". (see http://spec.graphql.org/June2018/#sec-Names)') |
46
|
47 |
|
->end(); |
47
|
|
|
|
48
|
47 |
|
return $node; |
49
|
|
|
} |
50
|
|
|
|
51
|
47 |
|
protected function defaultValueSection() |
52
|
|
|
{ |
53
|
47 |
|
return self::createNode('defaultValue', 'variable'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return ArrayNodeDefinition|NodeDefinition |
58
|
|
|
*/ |
59
|
47 |
|
protected function validationSection(int $level): NodeParentInterface |
60
|
|
|
{ |
61
|
47 |
|
$node = self::createNode('validation', 'array'); |
62
|
|
|
|
63
|
|
|
$node |
64
|
|
|
// allow shorthands |
65
|
47 |
|
->beforeNormalization() |
66
|
|
|
->always(function ($value) { |
67
|
3 |
|
if (\is_string($value)) { |
68
|
|
|
// shorthand: cascade or link |
69
|
2 |
|
return 'cascade' === $value ? ['cascade' => null] : ['link' => $value]; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
if (\is_array($value)) { |
73
|
2 |
|
foreach ($value as $k => $a) { |
74
|
2 |
|
if (!\is_int($k)) { |
75
|
|
|
// validation: { link: ... , constraints: ..., cascade: ... } |
76
|
1 |
|
return $value; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
// validation: [list of constraints] |
80
|
2 |
|
return ['constraints' => $value]; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return []; |
84
|
47 |
|
}) |
85
|
47 |
|
->end() |
86
|
47 |
|
->children() |
87
|
47 |
|
->scalarNode('link') |
88
|
|
|
// ->defaultNull() |
89
|
47 |
|
->validate() |
90
|
|
|
->ifTrue(function ($link) use ($level) { |
91
|
1 |
|
if (self::VALIDATION_LEVEL_PROPERTY === $level) { |
92
|
1 |
|
return !\preg_match('/^(?:\\\\?[A-Za-z][A-Za-z\d]+)*[A-Za-z\d]+::(?:[$]?[A-Za-z][A-Za-z_\d]+|[A-Za-z_\d]+\(\))$/m', $link); |
93
|
|
|
} else { |
94
|
1 |
|
return !\preg_match('/^(?:\\\\?[A-Za-z][A-Za-z\d]+)*[A-Za-z\d]$/m', $link); |
95
|
|
|
} |
96
|
47 |
|
}) |
97
|
47 |
|
->thenInvalid('Invalid link provided: "%s".') |
98
|
47 |
|
->end() |
99
|
47 |
|
->end() |
100
|
47 |
|
->variableNode('constraints')->end() |
101
|
47 |
|
->end(); |
102
|
|
|
|
103
|
|
|
// Add the 'cascade' option if it's a property level validation section |
104
|
47 |
|
if (self::VALIDATION_LEVEL_PROPERTY === $level) { |
105
|
|
|
$node |
106
|
47 |
|
->children() |
107
|
47 |
|
->arrayNode('cascade') |
108
|
47 |
|
->children() |
109
|
47 |
|
->arrayNode('groups') |
110
|
47 |
|
->beforeNormalization() |
111
|
47 |
|
->castToArray() |
112
|
47 |
|
->end() |
113
|
47 |
|
->scalarPrototype()->end() |
|
|
|
|
114
|
47 |
|
->end() |
115
|
47 |
|
->end() |
116
|
47 |
|
->end() |
117
|
47 |
|
->end(); |
118
|
|
|
} |
119
|
|
|
|
120
|
47 |
|
return $node; |
121
|
|
|
} |
122
|
|
|
|
123
|
47 |
|
protected function descriptionSection() |
124
|
|
|
{ |
125
|
47 |
|
$node = self::createNode('description', 'scalar'); |
126
|
|
|
|
127
|
47 |
|
return $node; |
128
|
|
|
} |
129
|
|
|
|
130
|
47 |
|
protected function deprecationReasonSection() |
131
|
|
|
{ |
132
|
47 |
|
$node = self::createNode('deprecationReason', 'scalar'); |
133
|
|
|
|
134
|
47 |
|
$node->info('Text describing why this field is deprecated. When not empty - field will not be returned by introspection queries (unless forced)'); |
135
|
|
|
|
136
|
47 |
|
return $node; |
137
|
|
|
} |
138
|
|
|
|
139
|
47 |
|
protected function typeSection($isRequired = false) |
140
|
|
|
{ |
141
|
47 |
|
$node = self::createNode('type', 'scalar'); |
142
|
|
|
|
143
|
47 |
|
$node->info('One of internal or custom types.'); |
144
|
|
|
|
145
|
47 |
|
if ($isRequired) { |
146
|
47 |
|
$node->isRequired(); |
147
|
|
|
} |
148
|
|
|
|
149
|
47 |
|
return $node; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return ArrayNodeDefinition|NodeDefinition |
154
|
|
|
* |
155
|
|
|
* @internal |
156
|
|
|
*/ |
157
|
47 |
|
protected static function createNode(string $name, string $type = 'array') |
158
|
|
|
{ |
159
|
47 |
|
return Configuration::getRootNodeWithoutDeprecation(new TreeBuilder($name, $type), $name, $type); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|