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

Items   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 47
ccs 18
cts 18
cp 1
rs 10
wmc 9
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 22 5
A getSchema() 0 12 4
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard\SubSchemaValidatorFactory;
6
7
class Items implements ContainerInstanceConstraint
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12 12
    public static function validate($data, $parameter, SubSchemaValidatorFactory $validatorFactory, $pointer = null)
13
    {
14 12
        if (!is_array($data)) {
15 4
            return null;
16
        }
17
18 10
        $errors = [];
19 10
        foreach ($data as $key => $value) {
20 10
            $schema = self::getSchema($parameter, $key);
21
22
            // Additional items are allowed by default,
23
            // so there might not be a schema for this.
24 10
            if (is_null($schema)) {
25 2
                continue;
26
            }
27
28 10
            $validator = $validatorFactory->makeSubSchemaValidator($value, $schema, $pointer . '/' . $key);
29 10
            $errors = array_merge($errors, $validator->errors());
30 10
        }
31
32 10
        return $errors ?: null;
33
    }
34
35
    /**
36
     * @param $parameter
37
     * @param $key
38
     *
39
     * @return mixed
40
     */
41 10
    protected static function getSchema($parameter, $key)
42
    {
43 10
        if (is_object($parameter)) {
44
            // list validation
45 8
            return $parameter;
46 6
        } elseif (is_array($parameter) && array_key_exists($key, $parameter)) {
47
            // tuple validation
48 6
            return $parameter[$key];
49
        }
50
51 2
        return null;
52
    }
53
}
54