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