Completed
Pull Request — master (#45)
by Christoffer
02:04
created

Validator::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 4
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Validation;
4
5
use Digia\GraphQL\Language\AST\Node\DocumentNode;
6
use Digia\GraphQL\Language\AST\Visitor\ParallelVisitor;
7
use Digia\GraphQL\Language\AST\Visitor\TypeInfoVisitor;
8
use Digia\GraphQL\Type\SchemaInterface;
9
use Digia\GraphQL\Util\TypeInfo;
10
use Digia\GraphQL\Validation\Rule\RuleInterface;
11
12
class Validator implements ValidatorInterface
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public function validate(
18
        SchemaInterface $schema,
19
        DocumentNode $document,
20
        array $rules = [],
21
        ?TypeInfo $typeInfo = null
22
    ): array {
23
        $typeInfo = $typeInfo ?? new TypeInfo($schema);
24
25
        $context = new ValidationContext($schema, $document, $typeInfo);
26
27
        $visitors = array_map(function (RuleInterface $rule) use ($context) {
28
            return $rule->setContext($context);
29
        }, $rules ?? specifiedRules());
30
31
        $visitor = new TypeInfoVisitor($typeInfo, new ParallelVisitor($visitors));
32
33
        // Visit the whole document with each instance of all provided rules.
34
        $document->accept($visitor);
35
36
        return $context->getErrors();
37
    }
38
}
39