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