Required::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 3
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
crap 3.0067
rs 9.4285
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
10
final class Required implements ConstraintInterface
11
{
12
    const KEYWORD = 'required';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 16
    public function validate($value, $parameter, Validator $validator)
18
    {
19 16
        Assert::type($parameter, 'array', self::KEYWORD, $validator->getSchemaPath());
20 14
        Assert::notEmpty($parameter, self::KEYWORD, $validator->getSchemaPath());
21
22 12
        if (!is_object($value)) {
23
            return null;
24
        }
25
26 12
        $actualProperties = array_keys(get_object_vars($value));
27 12
        $missing          = array_diff($parameter, $actualProperties);
28 12
        if (count($missing)) {
29 10
            return error('The object must contain the properties {cause}.', $validator)
30 10
                ->withCause(array_values($missing));
31
        }
32
33 10
        return null;
34
    }
35
}
36