Completed
Push — master ( e98ae3...0e888f )
by Matt
02:50
created

Required::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 3
dl 0
loc 23
ccs 15
cts 16
cp 0.9375
crap 3.0021
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\ValidationError;
7
8
class Required implements PropertyConstraint
9
{
10
    const KEYWORD = 'required';
11
12
    /**
13
     * {@inheritdoc}
14
     */
15 20
    public static function validate($data, $parameter, $pointer = null)
16
    {
17 20
        Assert::type($parameter, 'array', self::KEYWORD, $pointer);
18 18
        Assert::notEmpty($parameter, self::KEYWORD, $pointer);
19
20 16
        if (!is_object($data)) {
21
            return null;
22
        }
23
24 16
        $actualProperties = array_keys(get_object_vars($data));
25 16
        $missing          = array_diff($parameter, $actualProperties);
26 16
        if (count($missing)) {
27 14
            return new ValidationError(
28 14
                'Required properties missing: {missing}',
29 14
                self::KEYWORD,
30 14
                $data,
31 14
                $pointer,
32 14
                ['missing' => array_values($missing)]
33 14
            );
34
        }
35
36 14
        return null;
37
    }
38
}
39