Completed
Push — develop ( d22805...7fc434 )
by Mikaël
35:39
created

AbstractElementHandler::getMaxOccurs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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