Passed
Push — develop ( f7ed9e...68fe11 )
by Mikaël
01:31
created

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