|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Date: 10/24/16 |
|
4
|
|
|
* |
|
5
|
|
|
* @author Portey Vasil <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Validator\RequestValidator; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Execution\Request; |
|
12
|
|
|
use Youshido\GraphQL\Parser\Exception\InvalidRequestException; |
|
13
|
|
|
|
|
14
|
|
|
class RequestValidator implements RequestValidatorInterface |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
34 |
|
public function validate(Request $request) |
|
18
|
|
|
{ |
|
19
|
34 |
|
$this->assertFragmentReferencesValid($request); |
|
20
|
32 |
|
$this->assetFragmentsUsed($request); |
|
21
|
31 |
|
$this->assertAllVariablesExists($request); |
|
22
|
30 |
|
$this->assertAllVariablesUsed($request); |
|
23
|
29 |
|
} |
|
24
|
|
|
|
|
25
|
32 |
|
private function assetFragmentsUsed(Request $request) |
|
26
|
|
|
{ |
|
27
|
32 |
|
foreach ($request->getFragmentReferences() as $fragmentReference) { |
|
28
|
3 |
|
$request->getFragment($fragmentReference->getName())->setUsed(true); |
|
29
|
32 |
|
} |
|
30
|
|
|
|
|
31
|
32 |
|
foreach ($request->getFragments() as $fragment) { |
|
32
|
3 |
|
if (!$fragment->isUsed()) { |
|
33
|
1 |
|
throw new InvalidRequestException(sprintf('Fragment "%s" not used', $fragment->getName())); |
|
34
|
|
|
} |
|
35
|
32 |
|
} |
|
36
|
31 |
|
} |
|
37
|
|
|
|
|
38
|
34 |
|
private function assertFragmentReferencesValid(Request $request) |
|
39
|
|
|
{ |
|
40
|
34 |
|
foreach ($request->getFragmentReferences() as $fragmentReference) { |
|
41
|
5 |
|
if (!$request->getFragment($fragmentReference->getName())) { |
|
42
|
2 |
|
throw new InvalidRequestException(sprintf('Fragment "%s" not defined in query', $fragmentReference->getName())); |
|
43
|
|
|
} |
|
44
|
33 |
|
} |
|
45
|
32 |
|
} |
|
46
|
|
|
|
|
47
|
31 |
|
private function assertAllVariablesExists(Request $request) |
|
48
|
|
|
{ |
|
49
|
31 |
|
foreach ($request->getVariableReferences() as $variableReference) { |
|
50
|
5 |
|
if (!$variableReference->getVariable()) { |
|
51
|
1 |
|
throw new InvalidRequestException(sprintf('Variable "%s" not exists', $variableReference->getName())); |
|
52
|
|
|
} |
|
53
|
30 |
|
} |
|
54
|
30 |
|
} |
|
55
|
|
|
|
|
56
|
30 |
|
private function assertAllVariablesUsed(Request $request) |
|
57
|
|
|
{ |
|
58
|
30 |
|
foreach ($request->getQueryVariables() as $queryVariable) { |
|
59
|
4 |
|
if (!$queryVariable->isUsed()) { |
|
60
|
1 |
|
throw new InvalidRequestException(sprintf('Variable "%s" not used', $queryVariable->getName())); |
|
61
|
|
|
} |
|
62
|
30 |
|
} |
|
63
|
29 |
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|