Passed
Pull Request — master (#18)
by SignpostMarv
03:22
created

Element::loadElement()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

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