1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FluentDOM { |
4
|
|
|
|
5
|
|
|
use FluentDOM\XMLWriter\NamespaceDefinition; |
6
|
|
|
use FluentDOM\XMLWriter\NamespaceStack; |
7
|
|
|
|
8
|
|
|
class XMLWriter extends \XMLWriter { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var Namespaces |
12
|
|
|
*/ |
13
|
|
|
private $_namespaces; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var XMLWriter\NamespaceStack |
17
|
|
|
*/ |
18
|
|
|
private $_xmlnsStack; |
19
|
|
|
|
20
|
3 |
|
public function __construct() { |
21
|
3 |
|
$this->_namespaces = new Namespaces(); |
22
|
3 |
|
$this->_xmlnsStack = new XMLWriter\NamespaceStack(); |
23
|
3 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* register a namespace prefix for the xml reader, it will be used in |
27
|
|
|
* next() and other methods with a tag name argument |
28
|
|
|
* |
29
|
|
|
* @param string $prefix |
30
|
|
|
* @param string $namespace |
31
|
|
|
* @throws \LogicException |
32
|
|
|
*/ |
33
|
2 |
|
public function registerNamespace($prefix, $namespace) { |
34
|
2 |
|
$this->_namespaces[$prefix] = $namespace; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add the current namespace configuration as xmlns* attributes to the element node. |
39
|
|
|
*/ |
40
|
1 |
|
public function applyNamespaces() { |
41
|
1 |
|
foreach ($this->_namespaces as $prefix => $namespaceUri) { |
42
|
1 |
|
$this->writeAttribute( |
43
|
1 |
|
empty($prefix) || $prefix == '#default' ? 'xmlns' : 'xmlns:'.$prefix, $namespaceUri |
44
|
1 |
|
); |
45
|
1 |
|
} |
46
|
1 |
|
} |
47
|
|
|
|
48
|
3 |
|
public function startElement($name) { |
49
|
3 |
|
list($prefix, $localName) = QualifiedName::split($name); |
50
|
3 |
|
$namespaceUri = $this->_namespaces->resolveNamespace($prefix); |
|
|
|
|
51
|
3 |
|
return $this->startElementNS((string)$prefix, $localName, $namespaceUri); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
public function writeElement($name, $content = NULL) { |
55
|
2 |
|
list($prefix, $localName) = QualifiedName::split($name); |
56
|
2 |
|
$namespaceUri = $this->_namespaces->resolveNamespace($prefix); |
|
|
|
|
57
|
2 |
|
return $this->writeElementNS((string)$prefix, $localName, $namespaceUri, $content); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
public function startElementNS($prefix, $name, $namespaceUri) { |
61
|
3 |
|
$this->_xmlnsStack->push(); |
62
|
3 |
View Code Duplication |
if ($this->_xmlnsStack->isDefined($prefix, $namespaceUri)) { |
|
|
|
|
63
|
3 |
|
parent::startElement(empty($prefix) ? $name : $prefix.':'.$name); |
|
|
|
|
64
|
3 |
|
} else { |
65
|
2 |
|
parent::startElementNS(empty($prefix) ? NULL : $prefix, $name, $namespaceUri); |
66
|
2 |
|
$this->_xmlnsStack->add($prefix, $namespaceUri); |
67
|
|
|
} |
68
|
3 |
|
} |
69
|
|
|
|
70
|
2 |
|
public function writeElementNS($prefix, $name, $uri, $content = NULL) { |
71
|
2 |
View Code Duplication |
if ($this->_xmlnsStack->isDefined($prefix, $uri)) { |
|
|
|
|
72
|
2 |
|
parent::writeElement(empty($prefix) ? $name : $prefix.':'.$name, $content); |
|
|
|
|
73
|
2 |
|
} else { |
74
|
|
|
parent::writeElementNS(empty($prefix) ? NULL : $prefix, $name, $uri, $content); |
75
|
|
|
} |
76
|
2 |
|
} |
77
|
|
|
|
78
|
3 |
|
public function endElement() { |
79
|
3 |
|
$this->_xmlnsStack->pop(); |
80
|
3 |
|
parent::endElement(); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
public function startAttribute($name) { |
84
|
|
|
list($prefix, $localName) = QualifiedName::split($name); |
85
|
|
|
$this->startAttributeNS($prefix, $localName, $this->_namespaces->resolveNamespace($prefix)); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
public function writeAttribute($name, $value) { |
89
|
2 |
|
list($prefix, $localName) = QualifiedName::split($name); |
90
|
2 |
|
$this->writeAttributeNS($prefix, $localName, $this->_namespaces->resolveNamespace($prefix), $value); |
|
|
|
|
91
|
2 |
|
} |
92
|
|
|
|
93
|
|
|
public function startAttributeNS($prefix, $name, $uri) { |
94
|
|
|
if (empty($prefix)) { |
95
|
|
|
parent::startAttribute($name); |
|
|
|
|
96
|
|
|
} elseif ($this->_xmlnsStack->isDefined($prefix, $uri)) { |
97
|
|
|
parent::startAttribute($prefix.':'.$name); |
|
|
|
|
98
|
|
|
} else { |
99
|
|
|
parent::startAttributeNS($prefix, $name, $uri); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
public function writeAttributeNS($prefix, $localName, $uri, $content) { |
104
|
2 |
|
if ((empty($prefix) && $localName == 'xmlns') || $prefix == 'xmlns') { |
105
|
1 |
|
$namespacePrefix = empty($prefix) ? '' : $localName; |
106
|
1 |
|
$namespaceUri = $content; |
107
|
1 |
|
if (!$this->_xmlnsStack->isDefined($namespacePrefix, $namespaceUri)) { |
108
|
1 |
|
parent::writeAttribute(empty($prefix) ? 'xmlns' : 'xmlns:'.$localName, $namespaceUri); |
|
|
|
|
109
|
1 |
|
$this->_xmlnsStack->add($namespacePrefix, $namespaceUri); |
110
|
1 |
|
} |
111
|
2 |
|
} elseif (empty($prefix)) { |
112
|
2 |
|
parent::writeAttribute($localName, $content); |
|
|
|
|
113
|
2 |
|
} elseif ($this->_xmlnsStack->isDefined($prefix, $uri)) { |
114
|
1 |
|
parent::writeAttribute($prefix.':'.$localName, $content); |
|
|
|
|
115
|
1 |
|
} else { |
116
|
|
|
parent::writeAttributeNS($prefix, $localName, $uri, $content); |
117
|
|
|
} |
118
|
2 |
|
} |
119
|
|
|
} |
120
|
|
|
} |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.