Completed
Pull Request — master (#118)
by Christoffer
03:34
created

SchemaValidator::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Digia\GraphQL\SchemaValidator;
4
5
use Digia\GraphQL\SchemaValidator\Rule\SupportedRules;
6
use Digia\GraphQL\Type\SchemaInterface;
7
8
class SchemaValidator implements SchemaValidatorInterface
9
{
10
    /**
11
     * @var ContextBuilderInterface
12
     */
13
    protected $contextBuilder;
14
15
    /**
16
     * @var SchemaValidatorInterface[]
17
     */
18
    protected $validators;
19
20
    /**
21
     * SchemaValidator constructor.
22
     * @param ContextBuilderInterface|null $contextBuilder
23
     */
24
    public function __construct(?ContextBuilderInterface $contextBuilder = null)
25
    {
26
        $this->contextBuilder = $contextBuilder ?? new ContextBuilder();
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function validate(SchemaInterface $schema, ?array $validators = null): array
33
    {
34
        $context = $this->contextBuilder->build($schema);
35
36
        $validators = $validators ?? SupportedRules::build();
37
38
        foreach ($validators as $validator) {
39
            $validator->validate($context);
40
        }
41
42
        return $context->getErrors();
43
    }
44
}
45