Completed
Push — master ( feafc2...e07893 )
by Matt
12s
created

OneOf::validate()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 16

Duplication

Lines 6
Ratio 25 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 6
nop 3
dl 6
loc 24
ccs 19
cts 19
cp 1
crap 4
rs 8.6845
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\SubSchemaValidatorFactory;
7
use League\JsonGuard\ValidationError;
8
use League\JsonGuard\Validator;
9
10
class OneOf implements Constraint
11
{
12
    const KEYWORD = 'oneOf';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 8
    public function validate($value, $parameter, Validator $validator)
18
    {
19 8
        Assert::type($parameter, 'array', self::KEYWORD, $validator->getPointer());
20 6
        Assert::notEmpty($parameter, self::KEYWORD, $validator->getPointer());
21
22 4
        $passed = 0;
23 4 View Code Duplication
        foreach ($parameter as $schema) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24 4
            $validator = $validator->makeSubSchemaValidator($value, $schema, $validator->getPointer());
25 4
            if ($validator->passes()) {
26 4
                $passed++;
27 4
            }
28 4
        }
29 4
        if ($passed !== 1) {
30 4
            return new ValidationError(
31 4
                'Failed matching exactly one of the provided schemas.',
32 4
                self::KEYWORD,
33 4
                $value,
34 4
                $validator->getPointer(),
35 4
                ['one_of' => $parameter]
36 4
            );
37
        }
38
39 4
        return null;
40
    }
41
}
42