|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaloa\Renderer\Xml\Rule; |
|
4
|
|
|
|
|
5
|
|
|
use DOMDocument; |
|
6
|
|
|
use DOMElement; |
|
7
|
|
|
use DOMNode; |
|
8
|
|
|
use DOMXPath; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractRule |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* @var DOMDocument |
|
18
|
|
|
*/ |
|
19
|
|
|
private $document; |
|
20
|
|
|
|
|
21
|
1 |
|
public function init() |
|
22
|
|
|
{ |
|
23
|
|
|
// nop |
|
24
|
1 |
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
public function preSave() |
|
27
|
|
|
{ |
|
28
|
|
|
// nop |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* |
|
33
|
|
|
* @param DOMDocument $document |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function setDocument(DOMDocument $document) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$this->document = $document; |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @return DOMDocument |
|
43
|
|
|
*/ |
|
44
|
1 |
|
protected function getDocument() |
|
45
|
|
|
{ |
|
46
|
1 |
|
return $this->document; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function preRender() |
|
50
|
|
|
{ |
|
51
|
|
|
// nop |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function render() |
|
55
|
|
|
{ |
|
56
|
|
|
// nop |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function postRender() |
|
60
|
|
|
{ |
|
61
|
|
|
// nop |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $string |
|
67
|
|
|
* @param int $flags |
|
68
|
|
|
* @param string $charset |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
1 |
|
protected function escape($string, $flags = ENT_QUOTES, $charset = 'UTF-8') |
|
72
|
|
|
{ |
|
73
|
1 |
|
return htmlspecialchars($string, $flags, $charset); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $xpathExpression |
|
79
|
|
|
* @param DOMNode $contextNode |
|
80
|
|
|
* @param boolean $documentOrdered |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
1 |
|
protected function runXpathQuery($xpathExpression, DOMNode $contextNode = null, $documentOrdered = true) |
|
84
|
|
|
{ |
|
85
|
1 |
|
$xp = new DOMXPath($this->document); |
|
86
|
1 |
|
$xp->registerNamespace('k', 'lalalala'); |
|
87
|
1 |
|
$nodeList = $xp->query($xpathExpression, $contextNode); |
|
88
|
|
|
|
|
89
|
1 |
|
$arrayList = array(); |
|
90
|
|
|
|
|
91
|
1 |
|
foreach ($nodeList as $node) { |
|
92
|
1 |
|
$arrayList[] = $node; |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/* Shall the result be ordered? |
|
96
|
|
|
* |
|
97
|
|
|
* As far as I can tell from web sources, a NodeList returned by |
|
98
|
|
|
* DOMXPath is not necessarily in document order. So we need to sort it |
|
99
|
|
|
* on our own. This code iterates depth first over the DOM tree while |
|
100
|
|
|
* checking every node against the node list returned by XPath. If two |
|
101
|
|
|
* nodes are equal, delete the node from the XPath result set and add it |
|
102
|
|
|
* to a new (now sorted) list. |
|
103
|
|
|
* |
|
104
|
|
|
* Unfortunately, this is not the most efficient thing to do. |
|
105
|
|
|
*/ |
|
106
|
1 |
|
if ($documentOrdered) { |
|
107
|
1 |
|
$newList = array(); |
|
108
|
|
|
|
|
109
|
|
|
// Traverse the tree |
|
110
|
1 |
|
$rec = function ($node) use (&$rec, &$newList, &$arrayList) { |
|
111
|
|
|
/* @var $node DOMElement */ |
|
112
|
|
|
|
|
113
|
1 |
|
foreach ($arrayList as $index => $compareNode) { |
|
114
|
1 |
|
if ($node->isSameNode($compareNode)) { |
|
115
|
1 |
|
$newList[] = $node; |
|
116
|
1 |
|
unset($arrayList[$index]); |
|
117
|
1 |
|
break; |
|
118
|
|
|
} |
|
119
|
1 |
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
if ($node->hasChildNodes()) { |
|
122
|
1 |
|
foreach ($node->childNodes as $child) { |
|
123
|
1 |
|
$rec($child); |
|
124
|
1 |
|
} |
|
125
|
1 |
|
} |
|
126
|
1 |
|
}; |
|
127
|
|
|
|
|
128
|
1 |
|
$rec($this->document); |
|
129
|
1 |
|
$arrayList = $newList; |
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
return $arrayList; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @see http://www.php.net/manual/en/class.domelement.php#86803 |
|
137
|
|
|
* @param mixed $elem |
|
138
|
|
|
*/ |
|
139
|
1 |
|
protected function getInnerXml($elem) |
|
140
|
|
|
{ |
|
141
|
1 |
|
$innerHtml = ''; |
|
142
|
|
|
|
|
143
|
1 |
|
foreach ($elem->childNodes as $child) { |
|
144
|
1 |
|
$tmp_doc = new DOMDocument('1.0', 'UTF-8'); |
|
145
|
1 |
|
$tmp_doc->appendChild($tmp_doc->importNode($child, true)); |
|
146
|
|
|
|
|
147
|
1 |
|
$tmp = $tmp_doc->saveXML(); |
|
148
|
1 |
|
$tmp = preg_replace('/<\?xml[^>]*>\n/', '', $tmp); |
|
149
|
1 |
|
$tmp = rtrim($tmp, "\n"); |
|
150
|
|
|
|
|
151
|
1 |
|
$innerHtml .= $tmp; |
|
152
|
1 |
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
return $innerHtml; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|