Passed
Push — develop ( cd7fef...a71946 )
by Mikaël
01:37
created

AbstractElementHandler::isRequired()   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 78
    public function __construct(DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1)
12
    {
13 78
        parent::__construct($element, $domDocument, $index);
14 78
    }
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 = [];
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 = [];
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
     *
79
     * @return mixed
80
     */
81 10
    public function getMaxOccurs()
82
    {
83 10
        $maxOccurs = $this->getAttributeValue(AbstractAttributeHandler::ATTRIBUTE_MAX_OCCURS);
84 10
        if (AbstractAttributeHandler::VALUE_UNBOUNDED === $maxOccurs) {
85 4
            return $maxOccurs;
86
        }
87 6
        if (!is_numeric($maxOccurs)) {
88
            return AbstractAttributeHandler::DEFAULT_OCCURRENCE_VALUE;
89
        }
90
91 6
        return (int) $maxOccurs;
92
    }
93
94
    /**
95
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
96
     *
97
     * @return int
98
     */
99 20
    public function getMinOccurs()
100
    {
101 20
        $minOccurs = $this->getAttributeValue(AbstractAttributeHandler::ATTRIBUTE_MIN_OCCURS);
102 20
        if (!is_numeric($minOccurs)) {
103 4
            return AbstractAttributeHandler::DEFAULT_OCCURRENCE_VALUE;
104
        }
105
106 16
        return (int) $minOccurs;
107
    }
108
109
    /**
110
     * Info at {@link http://www.w3schools.com/xml/el_element.asp}.
111
     */
112 12
    public function getNillable(): bool
113
    {
114 12
        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 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
     */
128 4
    public function canOccurOnlyOnce(): bool
129
    {
130 4
        return 1 === $this->getMaxOccurs();
131
    }
132
133
    /**
134
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
135
     */
136 10
    public function isOptional(): bool
137
    {
138 10
        return 0 === $this->getMinOccurs();
139
    }
140
141
    /**
142
     * Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
143
     */
144 4
    public function isRequired(): bool
145
    {
146 4
        return 1 <= $this->getMinOccurs();
147
    }
148
149 8
    public function isRemovable(): bool
150
    {
151 8
        return $this->isOptional() && $this->getNillable();
152
    }
153
}
154