Required   A
last analyzed

Coupling/Cohesion

Components 0
Dependencies 3

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3

1 Method

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