Completed
Pull Request — master (#328)
by Sam
05:43
created

SchemaValidator::assertValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Schema\Validation;
4
5
use Digia\GraphQL\Schema\Schema;
6
use Digia\GraphQL\Schema\Validation\Rule\RuleInterface;
7
use Digia\GraphQL\Schema\Validation\Rule\SupportedRules;
8
use Digia\GraphQL\Validation\ValidationExceptionInterface;
9
10
class SchemaValidator implements SchemaValidatorInterface
11
{
12
    /**
13
     * @param Schema               $schema
14
     * @param RuleInterface[]|null $rules
15
     * @return ValidationExceptionInterface[]
16
     */
17
    public function validate(Schema $schema, ?array $rules = null): array
18
    {
19
        $context = $this->createContext($schema);
20
21
        $rules = $rules ?? SupportedRules::build();
22
23
        foreach ($rules as $rule) {
24
            $rule->setContext($context)->evaluate();
25
        }
26
27
        return $context->getErrors();
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function createContext(Schema $schema): ValidationContextInterface
34
    {
35
        return new ValidationContext($schema);
36
    }
37
}
38