Completed
Push — master ( 5927eb...1e13db )
by
unknown
04:04
created

Properties::validate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6
Metric Value
cc 6
eloc 12
nc 5
nop 4
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 6
rs 8.5906
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard\SubSchemaValidatorFactory;
6
7
class Properties implements ContainerInstanceConstraint
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12 28
    public static function validate($data, $parameter, SubSchemaValidatorFactory $validatorFactory, $pointer = null)
13
    {
14 28
        if (!is_object($data)) {
15 8
            return null;
16
        }
17
18
        // Iterate through the properties and create a new
19
        // validator for that property's schema and data.
20
        // merge the errors.
21 28
        $errors = [];
22 28
        foreach ($parameter as $property => $schema) {
23 28
            if (is_object($data) && property_exists($data, $property)) {
24 28
                $propertyData    = $data->$property;
25 28
                $propertyPointer = $pointer . '/' . $property;
26 28
                $validator       = $validatorFactory->makeSubSchemaValidator($propertyData, $schema, $propertyPointer);
27 28
                if ($validator->fails()) {
28 20
                    $errors = array_merge($errors, $validator->errors());
29 20
                }
30 26
            }
31 26
        }
32
33 26
        return $errors;
34
    }
35
}
36