Passed
Push — static-analysis ( ccdceb...ae16b7 )
by SignpostMarv
03:19
created

Restriction::addCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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