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

Validator   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 20 1
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