Test Failed
Pull Request — master (#18)
by SignpostMarv
02:37
created

Element::loadElement()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 8
nop 3
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 20
1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Schema\Element;
3
4
use DOMElement;
5
use DOMXPath;
6
use GoetasWebservices\XML\XSDReader\Schema\Item;
7
use GoetasWebservices\XML\XSDReader\Schema\Schema;
8
use GoetasWebservices\XML\XSDReader\SchemaReader;
9
10
class Element extends Item implements ElementItem, ElementSingle
11
{
12
    /**
13
    * @var int
14
    */
15
    protected $min = 1;
16
17 2
    /**
18
    * @var int
19 2
    */
20
    protected $max = 1;
21
22 45
    /**
23
    * @var bool
24 45
    */
25 45
    protected $qualified = false;
26
27
    /**
28 4
    * @var bool
29
    */
30 4
    protected $nil = false;
31
32
    /**
33 45
    * @return int
34
    */
35 45
    public function getMin()
36 45
    {
37
        return $this->min;
38
    }
39 1
40
    /**
41 1
    * @param int $min
42
    *
43
    * @return $this
44 3
    */
45
    public function setMin($min)
46 3
    {
47 3
        $this->min = $min;
48
        return $this;
49
    }
50 1
51
    /**
52 1
    * @return int
53
    */
54
    public function getMax()
55 3
    {
56
        return $this->max;
57 3
    }
58 3
59
    /**
60
    * @param int $max
61
    *
62
    * @return $this
63
    */
64
    public function setMax($max)
65
    {
66
        $this->max = $max;
67
        return $this;
68
    }
69
70
    /**
71
    * @return bool
72
    */
73
    public function isQualified()
74
    {
75
        return $this->qualified;
76
    }
77
78
    /**
79
    * @param bool $qualified
80
    *
81
    * @return $this
82
    */
83
    public function setQualified($qualified)
84
    {
85
        $this->qualified = is_bool($qualified) ? $qualified : (boolean) $qualified;
86
        return $this;
87
    }
88
89
    /**
90
    * @return bool
91
    */
92
    public function isNil()
93
    {
94
        return $this->nil;
95
    }
96
97
    /**
98
    * @param bool $nil
99
    *
100
    * @return $this
101
    */
102
    public function setNil($nil)
103
    {
104
        $this->nil = is_bool($nil) ? $nil : (boolean) $nil;
105
        return $this;
106
    }
107
108
    /**
109
    * @return Element
110
    */
111
    public static function loadElement(
112
        SchemaReader $reader,
113
        Schema $schema,
114
        DOMElement $node
115
    ) {
116
        $element = new Element($schema, $node->getAttribute("name"));
117
        $element->setDoc(SchemaReader::getDocumentation($node));
118
119
        $reader->fillItem($element, $node);
120
121
        SchemaReader::maybeSetMax($element, $node);
122
        SchemaReader::maybeSetMin($element, $node);
123
124
        $xp = new \DOMXPath($node->ownerDocument);
125
        $xp->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');
126
127
        if ($xp->query('ancestor::xs:choice', $node)->length) {
128
            $element->setMin(0);
129
        }
130
131
        if ($node->hasAttribute("nillable")) {
132
            $element->setNil($node->getAttribute("nillable") == "true");
133
        }
134
        if ($node->hasAttribute("form")) {
135
            $element->setQualified($node->getAttribute("form") == "qualified");
136
        }
137
        return $element;
138
    }
139
}
140