Dependencies::validate()   C
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 24
nc 5
nop 3
dl 0
loc 35
ccs 24
cts 24
cp 1
crap 7
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraint\DraftFour;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\ConstraintInterface;
7
use League\JsonGuard\Validator;
8
use function League\JsonGuard\error;
9
use function League\JsonGuard\pointer_push;
10
11
final class Dependencies implements ConstraintInterface
12
{
13
    const KEYWORD = 'dependencies';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 8
    public function validate($value, $parameter, Validator $validator)
19
    {
20 8
        Assert::type($parameter, ['object', 'array'], self::KEYWORD, $validator->getSchemaPath());
21
22 6
        $errors = [];
23 6
        foreach ($parameter as $property => $dependencies) {
24 6
            if (!is_object($value) || !property_exists($value, $property)) {
25 6
                continue;
26
            }
27
28 2
            if (is_array($dependencies)) {
29 2
                $errors = array_merge(
30 2
                    $errors,
31 2
                    array_filter(array_map(function ($dependency) use ($value, $validator) {
32 2
                        if (!in_array($dependency, array_keys(get_object_vars($value)), true)) {
33 2
                            return error('The object must contain the dependent property {cause}.', $validator)
34 2
                                ->withCause($dependency);
35
                        }
36 2
                    }, $dependencies))
37
                );
38 2
            } elseif (is_object($dependencies)) {
39 2
                $errors = array_merge(
40 2
                    $errors,
41 2
                    $validator->makeSubSchemaValidator(
42 2
                        $value,
43 2
                        $dependencies,
44 2
                        $validator->getDataPath(),
45 2
                        pointer_push($validator->getSchemaPath(), $property)
46 2
                    )->errors()
47
                );
48
            }
49
        }
50
51 6
        return $errors;
52
    }
53
}
54