Passed
Push — develop ( 52b304...9fd2b6 )
by Mikaël
01:40
created

AbstractElementHandler::getNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\DomHandler;
6
7
use DOMElement;
8
9
abstract class AbstractElementHandler extends AbstractNodeHandler
10
{
11 76
    public function __construct(DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1)
12
    {
13 76
        parent::__construct($element, $domDocument, $index);
14 76
    }
15
16 60
    public function getElement(): DOMElement
17
    {
18 60
        return $this->getNode();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getNode() returns the type DOMNode which includes types incompatible with the type-hinted return DOMElement.
Loading history...
19
    }
20
21 58
    public function hasAttribute(string $name): bool
22
    {
23 58
        return $this->getElement()->hasAttribute($name);
24
    }
25
26 56
    public function getAttribute(string $name): ?AttributeHandler
27
    {
28 56
        return $this->hasAttribute($name) ? $this->getDomDocumentHandler()->getHandler($this->getNode()->getAttributeNode($name)) : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hasAttribu...buteNode($name)) : null could return the type WsdlToPhp\DomHandler\Ele...\DomHandler\NodeHandler which is incompatible with the type-hinted return WsdlToPhp\DomHandler\AttributeHandler|null. Consider adding an additional type-check to rule them out.
Loading history...
29
    }
30
31 42
    public function getAttributeValue(string $name, bool $withNamespace = false, bool $withinItsType = true, ?string $asType = AbstractAttributeHandler::DEFAULT_VALUE_TYPE)
32
    {
33 42
        $value = null;
34 42
        $attribute = $this->getAttribute($name);
35 42
        if ($attribute instanceof AbstractAttributeHandler) {
0 ignored issues
show
introduced by
$attribute is always a sub-type of WsdlToPhp\DomHandler\AbstractAttributeHandler.
Loading history...
36 36
            $value = $attribute->getValue($withNamespace, $withinItsType, $asType);
37
        }
38
39 42
        return $value;
40
    }
41
42 4
    public function getChildrenByName(string $name): array
43
    {
44 4
        $children = array();
45 4
        if ($this->hasChildren()) {
46 4
            foreach ($this->getElement()->getElementsByTagName($name) as $index => $node) {
47 4
                $children[] = $this->getDomDocumentHandler()->getHandler($node, $index);
48
            }
49
        }
50
51 4
        return $children;
52
    }
53
54 2
    public function getElementChildren(): array
55
    {
56 2
        $children = array();
57 2
        if ($this->hasChildren()) {
58 2
            $children = $this->getDomDocumentHandler()->getElementsHandlers($this->getChildNodes());
59
        }
60
61 2
        return $children;
62
    }
63
64 2
    public function getChildrenByNameAndAttributes(string $name, array $attributes): array
65
    {
66 2
        return $this->getDomDocumentHandler()->getElementsByNameAndAttributes($name, $attributes, $this->getNode());
67
    }
68
69 2
    public function getChildByNameAndAttributes(string $name, array $attributes): ?ElementHandler
70
    {
71 2
        $children = $this->getChildrenByNameAndAttributes($name, $attributes);
72
73 2
        return empty($children) ? null : array_shift($children);
74
    }
75
76
    /**
77
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
78
     * @return mixed
79
     */
80 10
    public function getMaxOccurs()
81
    {
82 10
        $maxOccurs = $this->getAttributeValue(AbstractAttributeHandler::ATTRIBUTE_MAX_OCCURS);
83 10
        if (AbstractAttributeHandler::VALUE_UNBOUNDED === $maxOccurs) {
84 4
            return $maxOccurs;
85
        }
86 6
        if (!is_numeric($maxOccurs)) {
87
            return AbstractAttributeHandler::DEFAULT_OCCURRENCE_VALUE;
88
        }
89
90 6
        return (int) $maxOccurs;
91
    }
92
93
    /**
94
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
95
     * @return int
96
     */
97 20
    public function getMinOccurs()
98
    {
99 20
        $minOccurs = $this->getAttributeValue(AbstractAttributeHandler::ATTRIBUTE_MIN_OCCURS);
100 20
        if (!is_numeric($minOccurs)) {
101 4
            return AbstractAttributeHandler::DEFAULT_OCCURRENCE_VALUE;
102
        }
103
104 16
        return (int) $minOccurs;
105
    }
106
107
    /**
108
     * Info at {@link http://www.w3schools.com/xml/el_element.asp}
109
     * @return bool
110
     */
111 12
    public function getNillable(): bool
112
    {
113 12
        return (bool) $this->getAttributeValue(AbstractAttributeHandler::ATTRIBUTE_NILLABLE, false, true, 'bool');
114
    }
115
116
    /**
117
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
118
     * @return bool
119
     */
120 2
    public function canOccurSeveralTimes(): bool
121
    {
122 2
        return (1 < $this->getMinOccurs()) || (1 < $this->getMaxOccurs()) || (AbstractAttributeHandler::VALUE_UNBOUNDED === $this->getMaxOccurs());
123
    }
124
125
    /**
126
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
127
     * @return bool
128
     */
129 4
    public function canOccurOnlyOnce(): bool
130
    {
131 4
        return 1 === $this->getMaxOccurs();
132
    }
133
134
    /**
135
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
136
     * @return bool
137
     */
138 10
    public function isOptional(): bool
139
    {
140 10
        return 0 === $this->getMinOccurs();
141
    }
142
143
    /**
144
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
145
     * @return bool
146
     */
147 4
    public function isRequired(): bool
148
    {
149 4
        return 1 <= $this->getMinOccurs();
150
    }
151
152
    /**
153
     * @return bool
154
     */
155 8
    public function isRemovable(): bool
156
    {
157 8
        return $this->isOptional() && $this->getNillable();
158
    }
159
}
160