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

Required   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 23 3
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