1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\WsdlHandler\Tag; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use WsdlToPhp\DomHandler\AbstractAttributeHandler as Attribute; |
9
|
|
|
use WsdlToPhp\DomHandler\AbstractNodeHandler; |
10
|
|
|
use WsdlToPhp\DomHandler\ElementHandler; |
11
|
|
|
use WsdlToPhp\DomHandler\NodeHandler; |
12
|
|
|
use WsdlToPhp\WsdlHandler\AbstractDocument; |
13
|
|
|
|
14
|
|
|
abstract class AbstractTag extends ElementHandler |
15
|
|
|
{ |
16
|
|
|
const MAX_DEEP = 5; |
17
|
|
|
|
18
|
|
|
public function getDomDocumentHandler(): AbstractDocument |
19
|
|
|
{ |
20
|
|
|
return parent::getDomDocumentHandler(); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
28 |
|
/** |
24
|
|
|
* This method aims to get the parent element that matches a valid Wsdl element (aka struct). |
25
|
28 |
|
* |
26
|
28 |
|
* @return null|NodeHandler |
27
|
28 |
|
*/ |
28
|
28 |
|
public function getSuitableParent(bool $checkName = true, array $additionalTags = [], int $maxDeep = self::MAX_DEEP, bool $strict = false): ?AbstractNodeHandler |
29
|
28 |
|
{ |
30
|
12 |
|
$parentNode = null; |
31
|
|
|
if ($this->getParent() instanceof AbstractNodeHandler) { |
|
|
|
|
32
|
28 |
|
$parentTags = $strict ? $additionalTags : $this->getSuitableParentTags($additionalTags); |
33
|
24 |
|
$parentNode = $this->getParent()->getNode(); |
34
|
|
|
while ($maxDeep-- > 0 && ($parentNode instanceof DOMElement) && !empty($parentNode->nodeName) && (!preg_match('/'.implode('|', $parentTags).'/i', $parentNode->nodeName) || ($checkName && preg_match('/'.implode('|', $parentTags).'/i', $parentNode->nodeName) && (!$parentNode->hasAttribute('name') || '' === $parentNode->getAttribute('name'))))) { |
35
|
8 |
|
$parentNode = $parentNode->parentNode; |
36
|
|
|
} |
37
|
|
|
if ($parentNode instanceof DOMElement) { |
38
|
|
|
$parentNode = $this->getDomDocumentHandler()->getHandler($parentNode); |
39
|
28 |
|
} else { |
40
|
|
|
$parentNode = null; |
41
|
|
|
} |
42
|
4 |
|
} |
43
|
|
|
|
44
|
4 |
|
return $parentNode; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
public function hasAttributeName(): bool |
48
|
|
|
{ |
49
|
2 |
|
return $this->hasAttribute(Attribute::ATTRIBUTE_NAME); |
50
|
|
|
} |
51
|
|
|
|
52
|
22 |
|
public function hasAttributeRef(): bool |
53
|
|
|
{ |
54
|
22 |
|
return $this->hasAttribute(Attribute::ATTRIBUTE_REF); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public function getAttributeName(): string |
58
|
|
|
{ |
59
|
4 |
|
return $this->getAttribute(Attribute::ATTRIBUTE_NAME) instanceof Attribute ? $this->getAttribute(Attribute::ATTRIBUTE_NAME)->getValue() : ''; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
public function getAttributeRef(): string |
63
|
|
|
{ |
64
|
4 |
|
return $this->getAttribute(Attribute::ATTRIBUTE_REF) instanceof Attribute ? $this->getAttribute(Attribute::ATTRIBUTE_REF)->getValue() : ''; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
public function hasAttributeValue(): bool |
68
|
|
|
{ |
69
|
4 |
|
return $this->hasAttribute(Attribute::ATTRIBUTE_VALUE); |
70
|
|
|
} |
71
|
|
|
|
72
|
18 |
|
public function getValueAttributeValue(bool $withNamespace = false, bool $withinItsType = true, ?string $asType = null) |
73
|
|
|
{ |
74
|
18 |
|
return $this->getAttribute(Attribute::ATTRIBUTE_VALUE) instanceof Attribute ? $this->getAttribute(Attribute::ATTRIBUTE_VALUE)->getValue($withNamespace, $withinItsType, $asType) : ''; |
|
|
|
|
75
|
18 |
|
} |
76
|
|
|
|
77
|
|
|
public function hasAttributeTargetNamespace(): bool |
78
|
|
|
{ |
79
|
|
|
return $this->hasAttribute(AbstractDocument::ATTRIBUTE_TARGET_NAMESPACE); |
80
|
|
|
} |
81
|
|
|
|
82
|
10 |
|
public function getTargetNamespaceAttributeValue() |
83
|
|
|
{ |
84
|
10 |
|
return $this->getAttribute(AbstractDocument::ATTRIBUTE_TARGET_NAMESPACE) instanceof Attribute ? $this->getAttribute(AbstractDocument::ATTRIBUTE_TARGET_NAMESPACE)->getValue(true) : ''; |
|
|
|
|
85
|
10 |
|
} |
86
|
10 |
|
|
87
|
|
|
/** |
88
|
10 |
|
* Retrieve element targetNamespace applicable value, |
89
|
8 |
|
* from targetNamespace attribute depending on the current Tag. |
90
|
|
|
*/ |
91
|
|
|
public function getTargetNamespace(): string |
92
|
4 |
|
{ |
93
|
|
|
$schema = $this instanceof TagSchema ? $this : $this->getStrictParent(AbstractDocument::TAG_SCHEMA); |
94
|
|
|
if ($schema instanceof TagSchema && $schema->hasAttributeTargetNamespace()) { |
95
|
|
|
$namespace = $schema->getTargetNamespaceAttributeValue(); |
96
|
|
|
} else { |
97
|
|
|
$namespace = $this->getDomDocumentHandler()->getAttributeTargetNamespaceValue(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $namespace; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getSuitableParentTags(array $additionalTags = []): array |
104
|
|
|
{ |
105
|
|
|
return array_merge([ |
106
|
|
|
AbstractDocument::TAG_ELEMENT, |
107
|
|
|
AbstractDocument::TAG_ATTRIBUTE, |
108
|
|
|
AbstractDocument::TAG_SIMPLE_TYPE, |
109
|
|
|
AbstractDocument::TAG_COMPLEX_TYPE, |
110
|
|
|
], $additionalTags); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function getStrictParent(string $name, bool $checkName = false): ?AbstractNodeHandler |
114
|
|
|
{ |
115
|
|
|
$parent = $this->getSuitableParent($checkName, [ |
116
|
|
|
$name, |
117
|
|
|
], self::MAX_DEEP, true); |
118
|
|
|
|
119
|
|
|
if ($parent instanceof AbstractNodeHandler && $parent->getName() === $name) { |
120
|
|
|
return $parent; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|