Completed
Push — master ( 0f7b0a...750008 )
by Christoffer
02:09
created

Validator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 20 1
A __construct() 0 3 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
     * @var ContextBuilderInterface
16
     */
17
    protected $contextBuilder;
18
19
    /**
20
     * Validator constructor.
21
     * @param ContextBuilderInterface $contextBuilder
22
     */
23
    public function __construct(ContextBuilderInterface $contextBuilder)
24
    {
25
        $this->contextBuilder = $contextBuilder;
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function validate(
32
        SchemaInterface $schema,
33
        DocumentNode $document,
34
        array $rules = [],
35
        ?TypeInfo $typeInfo = null
36
    ): array {
37
        $typeInfo = $typeInfo ?? new TypeInfo($schema);
38
39
        $context = $this->contextBuilder->build($schema, $document, $typeInfo);
40
41
        $visitors = array_map(function (RuleInterface $rule) use ($context) {
42
            return $rule->setValidationContext($context);
43
        }, $rules ?? specifiedRules());
44
45
        $visitor = new TypeInfoVisitor($typeInfo, new ParallelVisitor($visitors));
46
47
        // Visit the whole document with each instance of all provided rules.
48
        $document->acceptVisitor($visitor);
49
50
        return $context->getErrors();
51
    }
52
}
53