Completed
Pull Request — master (#18)
by SignpostMarv
02:40
created

Restriction   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 115
ccs 56
cts 58
cp 0.9655
rs 10
c 4
b 0
f 0
wmc 9

6 Methods

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