Test Failed
Pull Request — master (#18)
by SignpostMarv
03:33
created

Restriction::loadRestriction()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 26
rs 8.5806
ccs 0
cts 0
cp 0
crap 20
1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Schema\Inheritance;
3
4
use DOMElement;
5
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
6
use GoetasWebservices\XML\XSDReader\SchemaReader;
7
use GoetasWebservices\XML\XSDReader\SchemaReaderLoadAbstraction;
8
9 45
class Restriction extends Base
10
{
11 45
    /**
12 45
    * @var mixed[][]
13
    */
14
    protected $checks = array();
15 10
16
    /**
17 10
    * @param string $type
18
    * @param mixed[] $value
19
    *
20
    * @return $this
21
    */
22
    public function addCheck($type, $value)
23
    {
24
        $this->checks[$type][] = $value;
25
        return $this;
26
    }
27
28
    /**
29
    * @return mixed[][]
30
    */
31
    public function getChecks()
32
    {
33
        return $this->checks;
34
    }
35
36
    /**
37
    * @param string $type
38
    *
39
    * @return mixed[]
40
    */
41
    public function getChecksByType($type)
42
    {
43
        return isset($this->checks[$type])?$this->checks[$type]:array();
44
    }
45
46
    public static function loadRestriction(
47
        SchemaReaderLoadAbstraction $reader,
48
        Type $type,
49
        DOMElement $node
50
    ) {
51
        $restriction = new Restriction();
52
        $type->setRestriction($restriction);
53
        if ($node->hasAttribute("base")) {
54
            $reader->findAndSetSomeBase($type, $restriction, $node);
55
        } else {
56
            $addCallback = function (Type $restType) use ($restriction) {
57
                $restriction->setBase($restType);
58
            };
59
60
            Type::loadTypeWithCallbackOnChildNodes(
61
                $reader,
62
                $type->getSchema(),
63
                $node,
64
                $addCallback
65
            );
66
        }
67
        foreach ($node->childNodes as $childNode) {
68
            if ($childNode instanceof DOMElement) {
69
                static::maybeLoadRestrictionOnChildNode(
70
                    $restriction,
71
                    $childNode
72
                );
73
            }
74
        }
75
    }
76
77
    protected static function maybeLoadRestrictionOnChildNode(
78
        Restriction $restriction,
79
        DOMElement $childNode
80
    ) {
81
        if (
82
            in_array(
83
                $childNode->localName,
84
                [
85
                    'enumeration',
86
                    'pattern',
87
                    'length',
88
                    'minLength',
89
                    'maxLength',
90
                    'minInclusive',
91
                    'maxInclusive',
92
                    'minExclusive',
93
                    'maxExclusive',
94
                    'fractionDigits',
95
                    'totalDigits',
96
                    'whiteSpace'
97
                ],
98
                true
99
            )
100
        ) {
101
            static::definitelyLoadRestrictionOnChildNode(
102
                $restriction,
103
                $childNode
104
            );
105
        }
106
    }
107
108
    protected static function definitelyLoadRestrictionOnChildNode(
109
        Restriction $restriction,
110
        DOMElement $childNode
111
    ) {
112
        $restriction->addCheck(
113
            $childNode->localName,
114
            [
115
                'value' => $childNode->getAttribute("value"),
116
                'doc' => SchemaReader::getDocumentation($childNode)
117
            ]
118
        );
119
    }
120
}
121