1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Validation\Conflict; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode; |
6
|
|
|
use Digia\GraphQL\Language\AST\Node\FragmentSpreadNode; |
7
|
|
|
use Digia\GraphQL\Language\AST\Node\InlineFragmentNode; |
8
|
|
|
use Digia\GraphQL\Language\AST\Node\NodeInterface; |
9
|
|
|
|
10
|
|
|
class ComparisonContext |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $fieldMap = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $fragmentNames = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array|Conflict[] |
24
|
|
|
*/ |
25
|
|
|
protected $conflicts = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param FieldContext $field |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function registerField(FieldContext $field) |
32
|
|
|
{ |
33
|
|
|
$responseName = $field->getNode()->getAliasOrNameValue(); |
34
|
|
|
|
35
|
|
|
if (!isset($this->fieldMap[$responseName])) { |
36
|
|
|
$this->fieldMap[$responseName] = []; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->fieldMap[$responseName][] = $field; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param NodeInterface|FragmentSpreadNode|FragmentDefinitionNode $fragment |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function registerFragment(NodeInterface $fragment) |
49
|
|
|
{ |
50
|
|
|
$this->fragmentNames[] = $fragment->getNameValue(); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Conflict $conflict |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function reportConflict(Conflict $conflict) |
60
|
|
|
{ |
61
|
|
|
$this->conflicts[] = $conflict; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function getFieldMap(): array |
70
|
|
|
{ |
71
|
|
|
return $this->fieldMap; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getFragmentNames(): array |
78
|
|
|
{ |
79
|
|
|
return $this->fragmentNames; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function hasConflicts(): bool |
86
|
|
|
{ |
87
|
|
|
return !empty($this->conflicts); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array|Conflict[] |
92
|
|
|
*/ |
93
|
|
|
public function getConflicts() |
94
|
|
|
{ |
95
|
|
|
return $this->conflicts; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|