Completed
Pull Request — master (#108)
by Matt
19:10
created

AnyOf::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 11
Ratio 61.11 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 3
dl 11
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraints\DraftFour;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\Constraint;
7
use League\JsonGuard\Validator;
8
use function League\JsonGuard\error;
9
use function League\JsonReference\pointer_push;
10
11
class AnyOf implements Constraint
12
{
13
    const KEYWORD = 'anyOf';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function validate($value, $parameter, Validator $validator)
19
    {
20
        Assert::type($parameter, 'array', self::KEYWORD, $validator->getSchemaPath());
21
        Assert::notEmpty($parameter, self::KEYWORD, $validator->getSchemaPath());
22
23 View Code Duplication
        foreach ($parameter as $key => $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
            $subValidator = $validator->makeSubSchemaValidator(
25
                $value,
26
                $schema,
27
                $validator->getDataPath(),
28
                pointer_push($validator->getSchemaPath(), $key)
29
            );
30
            if ($subValidator->passes()) {
31
                return null;
32
            }
33
        }
34
        return error('The data must match one of the schemas.', $validator);
35
    }
36
}
37