1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Config; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
8
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
9
|
|
|
|
10
|
|
|
abstract class TypeWithOutputFieldsDefinition extends TypeDefinition |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $name |
14
|
|
|
* |
15
|
|
|
* @return ArrayNodeDefinition|NodeDefinition |
16
|
|
|
*/ |
17
|
47 |
|
protected function outputFieldsSelection(string $name = 'fields') |
18
|
|
|
{ |
19
|
47 |
|
$node = self::createNode($name); |
20
|
|
|
$node |
21
|
47 |
|
->isRequired() |
22
|
47 |
|
->requiresAtLeastOneElement(); |
|
|
|
|
23
|
|
|
/* @var ArrayNodeDefinition $prototype */ |
24
|
47 |
|
$prototype = $node->useAttributeAsKey('name', false)->prototype('array'); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$prototype |
27
|
|
|
// Allow field type short syntax (Field: Type => Field: {type: Type}) |
28
|
47 |
|
->beforeNormalization() |
29
|
|
|
->ifTrue(function ($options) { |
30
|
37 |
|
return \is_string($options); |
31
|
47 |
|
}) |
32
|
|
|
->then(function ($options) { |
33
|
3 |
|
return ['type' => $options]; |
34
|
47 |
|
}) |
35
|
47 |
|
->end() |
36
|
47 |
|
->validate() |
37
|
|
|
->always(function ($value) { |
38
|
37 |
|
if (empty($value['validationGroups'])) { |
39
|
37 |
|
unset($value['validationGroups']); |
40
|
|
|
} |
41
|
|
|
|
42
|
37 |
|
return $value; |
43
|
47 |
|
}) |
44
|
47 |
|
->end() |
45
|
47 |
|
->children() |
46
|
47 |
|
->append($this->typeSelection()) |
47
|
47 |
|
->append($this->validationSection(self::VALIDATION_LEVEL_CLASS)) |
48
|
47 |
|
->arrayNode('validationGroups') |
49
|
47 |
|
->prototype('scalar') |
50
|
47 |
|
->info('List of validation groups') |
51
|
47 |
|
->end() |
52
|
47 |
|
->end() |
53
|
47 |
|
->arrayNode('args') |
54
|
47 |
|
->info('Array of possible type arguments. Each entry is expected to be an array with following keys: name (string), type') |
55
|
47 |
|
->useAttributeAsKey('name', false) |
56
|
47 |
|
->prototype('array') |
57
|
|
|
// Allow arg type short syntax (Arg: Type => Arg: {type: Type}) |
58
|
47 |
|
->beforeNormalization() |
59
|
|
|
->ifTrue(function ($options) { |
60
|
28 |
|
return \is_string($options); |
61
|
47 |
|
}) |
62
|
|
|
->then(function ($options) { |
63
|
4 |
|
return ['type' => $options]; |
64
|
47 |
|
}) |
65
|
47 |
|
->end() |
66
|
47 |
|
->children() |
67
|
47 |
|
->append($this->typeSelection(true)) |
68
|
47 |
|
->append($this->descriptionSection()) |
69
|
47 |
|
->append($this->defaultValueSection()) |
70
|
47 |
|
->append($this->validationSection(self::VALIDATION_LEVEL_PROPERTY)) |
71
|
47 |
|
->end() |
72
|
47 |
|
->end() |
73
|
47 |
|
->end() |
74
|
47 |
|
->variableNode('resolve') |
75
|
47 |
|
->info('Value resolver (expression language can be used here)') |
76
|
47 |
|
->end() |
77
|
47 |
|
->append($this->descriptionSection()) |
78
|
47 |
|
->append($this->deprecationReasonSelection()) |
79
|
47 |
|
->variableNode('access') |
80
|
47 |
|
->info('Access control to field (expression language can be used here)') |
81
|
47 |
|
->end() |
82
|
47 |
|
->variableNode('public') |
83
|
47 |
|
->info('Visibility control to field (expression language can be used here)') |
84
|
47 |
|
->end() |
85
|
47 |
|
->variableNode('complexity') |
86
|
47 |
|
->info('Custom complexity calculator.') |
87
|
47 |
|
->end() |
88
|
47 |
|
->end(); |
89
|
|
|
|
90
|
47 |
|
return $node; |
91
|
|
|
} |
92
|
|
|
|
93
|
47 |
|
protected function fieldsBuilderSection() |
94
|
|
|
{ |
95
|
47 |
|
$node = self::createNode('builders'); |
96
|
|
|
|
97
|
47 |
|
$prototype = $node->prototype('array'); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$prototype |
100
|
47 |
|
->children() |
101
|
47 |
|
->variableNode('builder')->isRequired()->end() |
102
|
47 |
|
->variableNode('builderConfig')->end() |
103
|
47 |
|
->end(); |
104
|
|
|
|
105
|
47 |
|
return $node; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|