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