validateAdditionalItemsAgainstSchema()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 8
Ratio 57.14 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 8
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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
use function League\JsonGuard\pointer_push;
10
11
final class AdditionalItems implements ConstraintInterface
12
{
13
    const KEYWORD = 'additionalItems';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 4
    public function validate($value, $parameter, Validator $validator)
19
    {
20 4
        Assert::type($parameter, ['boolean', 'object'], self::KEYWORD, $validator->getSchemaPath());
21
22 2
        if (!is_array($value) || $parameter === true) {
23 2
            return null;
24
        }
25
26 2
        if (!is_array($items = self::getItems($validator->getSchema()))) {
27 2
            return null;
28
        }
29
30 2
        if ($parameter === false) {
31 2
            return self::validateAdditionalItemsWhenNotAllowed($value, $items, $validator);
32 2
        } elseif (is_object($parameter)) {
33 2
            $additionalItems = array_slice($value, count($items));
34
35 2
            return self::validateAdditionalItemsAgainstSchema($additionalItems, $parameter, $validator);
36
        }
37
    }
38
39
    /**
40
     * @param object $schema
41
     *
42
     * @return mixed
43
     */
44 2
    private static function getItems($schema)
45
    {
46 2
        return property_exists($schema, Items::KEYWORD) ? $schema->items : null;
47
    }
48
49
    /**
50
     * @param array     $items
51
     * @param object    $schema
52
     * @param Validator $validator
53
     *
54
     * @return array
55
     */
56 2
    private static function validateAdditionalItemsAgainstSchema($items, $schema, Validator $validator)
57
    {
58 2
        $errors = [];
59 2 View Code Duplication
        foreach ($items as $key => $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60 2
            $subValidator = $validator->makeSubSchemaValidator(
61 2
                $item,
62 2
                $schema,
63 2
                pointer_push($validator->getDataPath(), $key)
64
            );
65 2
            $errors = array_merge($errors, $subValidator->errors());
66
        }
67
68 2
        return $errors;
69
    }
70
71
    /**
72
     * @param array                       $value
73
     * @param array                       $items
74
     * @param \League\JsonGuard\Validator $validator
75
     *
76
     * @return \League\JsonGuard\ValidationError
77
     */
78 2
    private static function validateAdditionalItemsWhenNotAllowed($value, $items, Validator $validator)
79
    {
80 2
        if (count($value) > count($items)) {
81 2
            return error('The array must not contain additional items.', $validator);
82
        }
83 2
    }
84
}
85