AbstractOfConstraint::normalize()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 10
Ratio 38.46 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
dl 10
loc 26
c 0
b 0
f 0
ccs 17
cts 17
cp 1
rs 8.439
cc 5
eloc 14
nc 5
nop 3
crap 5
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal\Constraint;
11
12
use JVal\Constraint;
13
use JVal\Context;
14
use JVal\Exception\Constraint\EmptyArrayException;
15
use JVal\Exception\Constraint\InvalidTypeException;
16
use JVal\Types;
17
use JVal\Walker;
18
use stdClass;
19
20
/**
21
 * Base class for constraints based on a set of sub-schemas.
22
 */
23
abstract class AbstractOfConstraint implements Constraint
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 357
    public function supports($type)
29
    {
30 357
        return true;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 68
    public function normalize(stdClass $schema, Context $context, Walker $walker)
37
    {
38 68
        $keyword = $this->keywords()[0];
39 68
        $context->enterNode($keyword);
40
41 68
        if (!is_array($schema->{$keyword})) {
42 3
            throw new InvalidTypeException($context, Types::TYPE_ARRAY);
43
        }
44
45 65
        if (count($schema->{$keyword}) === 0) {
46 3
            throw new EmptyArrayException($context);
47 1
        }
48
49 62 View Code Duplication
        foreach ($schema->{$keyword} as $index => $subSchema) {
50 62
            $context->enterNode($index);
51
52 62
            if (!is_object($subSchema)) {
53 3
                throw new InvalidTypeException($context, Types::TYPE_OBJECT);
54
            }
55
56 62
            $walker->parseSchema($subSchema, $context);
57 62
            $context->leaveNode();
58 62
        }
59
60 59
        $context->leaveNode();
61 59
    }
62
}
63