Passed
Branch develop (52b304)
by Mikaël
01:34
created

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