Passed
Push — php-7.1 ( b6da63...280be0 )
by SignpostMarv
01:58
created

definitelyLoadRestrictionOnChildNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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