Passed
Branch develop (cd7fef)
by Mikaël
01:30
created

AbstractElementHandler   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
eloc 38
c 1
b 0
f 0
dl 0
loc 143
ccs 0
cts 54
cp 0
rs 10

17 Methods

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