Passed
Push — develop ( 477204...0132d1 )
by Mikaël
07:12
created

AbstractElementHandler   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
eloc 36
c 1
b 0
f 0
dl 0
loc 158
ccs 50
cts 52
cp 0.9615
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAttribute() 0 3 2
A getElement() 0 3 1
A hasAttribute() 0 3 1
A isOptional() 0 3 1
A isRemovable() 0 3 2
A isRequired() 0 3 1
A getAttributeValue() 0 9 2
A getChildrenByName() 0 13 3
A getChildrenByNameAndAttributes() 0 3 1
A canOccurSeveralTimes() 0 3 3
A getMaxOccurs() 0 11 3
A getMinOccurs() 0 8 2
A getChildByNameAndAttributes() 0 5 1
A getNillable() 0 3 1
A canOccurOnlyOnce() 0 3 1
A getElementChildren() 0 3 2
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 78
    public function __construct(DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1)
12
    {
13 78
        parent::__construct($element, $domDocument, $index);
14
    }
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->getElement()->getAttributeNode($name)) : null;
29
    }
30
31
    /**
32
     * @return null|bool|mixed|string
33
     */
34 42
    public function getAttributeValue(string $name, bool $withNamespace = false, bool $withinItsType = true, ?string $asType = AbstractAttributeHandler::DEFAULT_VALUE_TYPE)
35
    {
36 42
        $value = null;
37 42
        $attribute = $this->getAttribute($name);
38 42
        if ($attribute instanceof AbstractAttributeHandler) {
39 36
            $value = $attribute->getValue($withNamespace, $withinItsType, $asType);
40
        }
41
42 42
        return $value;
43
    }
44
45
    /**
46
     * @return array<int, AbstractElementHandler|AbstractNodeHandler>
47
     */
48 4
    public function getChildrenByName(string $name): array
49
    {
50 4
        $children = [];
51
52 4
        if (!$this->hasChildren()) {
53
            return $children;
54
        }
55
56 4
        foreach ($this->getElement()->getElementsByTagName($name) as $index => $node) {
57 4
            $children[] = $this->getDomDocumentHandler()->getHandler($node, $index);
58
        }
59
60 4
        return $children;
61
    }
62
63
    /**
64
     * @return AbstractElementHandler[]
65
     */
66 2
    public function getElementChildren(): array
67
    {
68 2
        return $this->hasChildren() ? $this->getDomDocumentHandler()->getElementsHandlers($this->getChildNodes()) : [];
69
    }
70
71
    /**
72
     * @param string[] $attributes
73
     *
74
     * @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
75
     */
76 2
    public function getChildrenByNameAndAttributes(string $name, array $attributes): array
77
    {
78 2
        return $this->getDomDocumentHandler()->getElementsByNameAndAttributes($name, $attributes, $this->getNode());
79
    }
80
81
    /**
82
     * @param string[] $attributes
83
     */
84 2
    public function getChildByNameAndAttributes(string $name, array $attributes): ?ElementHandler
85
    {
86 2
        $children = $this->getChildrenByNameAndAttributes($name, $attributes);
87
88 2
        return array_shift($children);
89
    }
90
91
    /**
92
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
93
     *
94
     * @return mixed
95
     */
96 10
    public function getMaxOccurs()
97
    {
98 10
        $maxOccurs = $this->getAttributeValue(AbstractAttributeHandler::ATTRIBUTE_MAX_OCCURS);
99 10
        if (AbstractAttributeHandler::VALUE_UNBOUNDED === $maxOccurs) {
100 4
            return $maxOccurs;
101
        }
102 6
        if (!is_numeric($maxOccurs)) {
103
            return AbstractAttributeHandler::DEFAULT_OCCURRENCE_VALUE;
104
        }
105
106 6
        return (int) $maxOccurs;
107
    }
108
109
    /**
110
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
111
     *
112
     * @return int
113
     */
114 20
    public function getMinOccurs()
115
    {
116 20
        $minOccurs = $this->getAttributeValue(AbstractAttributeHandler::ATTRIBUTE_MIN_OCCURS);
117 20
        if (!is_numeric($minOccurs)) {
118 4
            return AbstractAttributeHandler::DEFAULT_OCCURRENCE_VALUE;
119
        }
120
121 16
        return (int) $minOccurs;
122
    }
123
124
    /**
125
     * Info at {@link http://www.w3schools.com/xml/el_element.asp}.
126
     */
127 12
    public function getNillable(): bool
128
    {
129 12
        return (bool) $this->getAttributeValue(AbstractAttributeHandler::ATTRIBUTE_NILLABLE, false, true, 'bool');
130
    }
131
132
    /**
133
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
134
     */
135 2
    public function canOccurSeveralTimes(): bool
136
    {
137 2
        return (1 < $this->getMinOccurs()) || (1 < $this->getMaxOccurs()) || (AbstractAttributeHandler::VALUE_UNBOUNDED === $this->getMaxOccurs());
138
    }
139
140
    /**
141
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
142
     */
143 4
    public function canOccurOnlyOnce(): bool
144
    {
145 4
        return 1 === $this->getMaxOccurs();
146
    }
147
148
    /**
149
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
150
     */
151 10
    public function isOptional(): bool
152
    {
153 10
        return 0 === $this->getMinOccurs();
154
    }
155
156
    /**
157
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
158
     */
159 4
    public function isRequired(): bool
160
    {
161 4
        return 1 <= $this->getMinOccurs();
162
    }
163
164 8
    public function isRemovable(): bool
165
    {
166 8
        return $this->isOptional() && $this->getNillable();
167
    }
168
}
169