1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xml\Impl\Util; |
4
|
|
|
|
5
|
|
|
use Xml\Instance\{ |
6
|
|
|
DomDocumentInterface, |
7
|
|
|
DomElementInterface |
8
|
|
|
}; |
9
|
|
|
|
10
|
|
|
class XmlQName |
11
|
|
|
{ |
12
|
|
|
public const KNOWN_PREFIXES = [ |
13
|
|
|
'http://www.test.com/fox' => 'fox', |
14
|
|
|
'http://activiti.org/bpmn' => 'test', |
15
|
|
|
'http://test.org/schema/1.0/bpmn' => 'test', |
16
|
|
|
'http://www.omg.org/spec/BPMN/20100524/MODEL' => 'bpmn2', |
17
|
|
|
'http://www.omg.org/spec/BPMN/20100524/DI' => 'di', |
18
|
|
|
'http://www.omg.org/spec/DD/20100524/DI' => 'di', |
19
|
|
|
'http://www.omg.org/spec/DD/20100524/DC' => 'dc', |
20
|
|
|
'http://www.w3.org/2000/xmlns/' => '' |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected $rootElement; |
24
|
|
|
protected $element; |
25
|
|
|
protected $localName; |
26
|
|
|
protected $namespaceUri; |
27
|
|
|
protected $prefix; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
?DomDocumentInterface $document, |
31
|
|
|
?DomElementInterface $element, |
32
|
|
|
?string $namespaceUri, |
33
|
|
|
string $localName |
34
|
|
|
) { |
35
|
|
|
if ($element !== null) { |
36
|
|
|
$document = $element->getDocument(); |
37
|
|
|
$this->rootElement = $document->getRootElement(); |
38
|
|
|
} elseif ($document !== null) { |
39
|
|
|
$this->rootElement = $document->getRootElement(); |
40
|
|
|
} |
41
|
|
|
$this->element = $element; |
42
|
|
|
$this->localName = $localName; |
43
|
|
|
$this->namespaceUri = $namespaceUri; |
44
|
|
|
$this->prefix = null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getNamespaceUri(): ?string |
48
|
|
|
{ |
49
|
|
|
return $this->namespaceUri; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getLocalName(): string |
53
|
|
|
{ |
54
|
|
|
return $this->localName; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getPrefixedName(): string |
58
|
|
|
{ |
59
|
|
|
if ($this->prefix === null) { |
60
|
|
|
$this->prefix = $this->determinePrefixAndNamespaceUri(); |
61
|
|
|
} |
62
|
|
|
return QName::combine($this->prefix, $this->localName); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function hasLocalNamespace(): bool |
66
|
|
|
{ |
67
|
|
|
if ($this->element !== null) { |
68
|
|
|
return $this->element->getNamespaceURI() == $this->namespaceUri || empty($this->namespaceUri); |
69
|
|
|
} |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function determinePrefixAndNamespaceUri(): ?string |
74
|
|
|
{ |
75
|
|
|
if ($this->namespaceUri !== null) { |
76
|
|
|
if ($this->rootElement !== null && $this->namespaceUri == $this->rootElement->getNamespaceURI()) { |
77
|
|
|
return null; |
78
|
|
|
} else { |
79
|
|
|
$lookupPrefix = $this->lookupPrefix(); |
80
|
|
|
if ($lookupPrefix === null && $this->rootElement !== null) { |
81
|
|
|
$knownPrefix = array_search($this->namespaceUri, self::KNOWN_PREFIXES); |
82
|
|
|
if ($knownPrefix === false) { |
83
|
|
|
return $this->rootElement->registerNamespace(null, $this->namespaceUri); |
84
|
|
|
} elseif (empty($knownPrefix)) { |
85
|
|
|
return null; |
86
|
|
|
} else { |
87
|
|
|
$this->rootElement->registerNamespace($knownPrefix, $this->namespaceUri); |
88
|
|
|
return $knownPrefix; |
89
|
|
|
} |
90
|
|
|
} else { |
91
|
|
|
return $lookupPrefix; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function lookupPrefix(): ?string |
100
|
|
|
{ |
101
|
|
|
if ($this->namespaceUri !== null) { |
102
|
|
|
$lookupPrefix = null; |
103
|
|
|
if ($this->element !== null) { |
104
|
|
|
$lookupPrefix = $this->element->lookupPrefix($this->namespaceUri); |
105
|
|
|
} elseif ($this->rootElement !== null) { |
106
|
|
|
$lookupPrefix = $this->rootElement->lookupPrefix($this->namespaceUri); |
107
|
|
|
} |
108
|
|
|
return $lookupPrefix; |
109
|
|
|
} else { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|