Passed
Push — php-7.1 ( ed6d10...451fb2 )
by SignpostMarv
02:49
created

Restriction::loadRestriction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2

Importance

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