AnyOfConstraint::apply()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 15
cts 15
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 4
crap 4
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal\Constraint;
11
12
use JVal\Context;
13
use JVal\Walker;
14
use stdClass;
15
16
/**
17
 * Constraint for the "anyOf" keyword.
18
 */
19
class AnyOfConstraint extends AbstractOfConstraint
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 377
    public function keywords()
25
    {
26 377
        return ['anyOf'];
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 16
    public function apply($instance, stdClass $schema, Context $context, Walker $walker)
33
    {
34 16
        $accumulatingContext = $context->duplicate();
35 16
        $hasMatch = false;
36
37 16
        foreach ($schema->anyOf as $subSchema) {
38 16
            $originalCount = $accumulatingContext->countViolations();
39 16
            $walker->applyConstraints($instance, $subSchema, $accumulatingContext);
40
41 16
            if ($accumulatingContext->countViolations() === $originalCount) {
42 12
                $hasMatch = true;
43 12
                break;
44
            }
45 16
        }
46
47 16
        if (!$hasMatch) {
48 4
            $context->mergeViolations($accumulatingContext);
49 4
            $context->addViolation('instance must match at least one of the schemas listed in anyOf');
50 4
        }
51 16
    }
52
}
53