-
Notifications
You must be signed in to change notification settings - Fork 34
/
Cleanup.php
110 lines (89 loc) · 3.25 KB
/
Cleanup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace PHPStamp\Document\WordDocument;
use PHPStamp\XMLHelper;
class Cleanup extends XMLHelper
{
/**
* @var \DOMDocument
*/
private $document;
private $xpath;
private $paragraphQuery;
private $runQuery;
private $runPropertyQuery;
private $textQuery;
public function __construct(\DOMDocument $document, $paragraphQuery, $runQuery, $propertyQuery, $textQuery)
{
$this->document = $document;
$this->xpath = new \DOMXPath($document);
$this->paragraphQuery = $paragraphQuery;
$this->runQuery = $runQuery;
$this->runPropertyQuery = $propertyQuery;
$this->textQuery = $textQuery;
}
public function cleanup()
{
$paragraphs = $this->getParagraphNodeList();
/** @var $paragraph \DOMNode */
foreach ($paragraphs as $paragraph) {
$clonedParagraph = $paragraph->cloneNode(true); // fixed missing paragraph props element
$runNodeList = $this->getRunNodeList($clonedParagraph);
$runIndex = 0;
$currentRun = $runNodeList->item($runIndex);
$nextRun = $runNodeList->item(++$runIndex);
while ($currentRun) { //todo refactor as writerdocument loop
$isEqual = false;
if ($nextRun !== null) {
$isEqual = $this->deepEqual(
$this->getPropertyNode($currentRun),
$this->getPropertyNode($nextRun)
);
}
if ($isEqual === true) {
$this->getValueNode($currentRun)->nodeValue .= $this->getValueNode($nextRun)->nodeValue;
$clonedParagraph->removeChild($nextRun);
} else {
//$clonedParagraph->appendChild($currentRun);
$currentRun = $nextRun;
}
if ($nextRun !== null) {
$nextRun = $runNodeList->item(++$runIndex);
}
}
$paragraph->parentNode->replaceChild($clonedParagraph, $paragraph);
}
}
private function getParagraphNodeList()
{
return $this->xpath->query($this->paragraphQuery);
}
private function getRunNodeList(\DOMNode $paragraphNode)
{
return $this->xpath->query($this->runQuery, $paragraphNode);
}
private function getPropertyNode(\DOMNode $runNode)
{
$nodeList = $this->xpath->query($this->runPropertyQuery, $runNode);
return $nodeList->item(0);
}
private function getValueNode(\DOMNode $runNode)
{
$nodeList = $this->xpath->query($this->textQuery, $runNode);
return $nodeList->item(0);
}
public function hardcoreCleanup() // TODO Move into document specific class
{
// reset locale
$nodeList = $this->xpath->query('//w:lang');
/** @var $langNode \DOMNode */
foreach ($nodeList as $langNode) {
$langNode->parentNode->removeChild($langNode);
}
// cleanup empty rPr
$nodeList = $this->xpath->query('//' . $this->runPropertyQuery . '[not(node())]');
/** @var $langNode \DOMNode */
foreach ($nodeList as $node) {
$node->parentNode->removeChild($node);
}
}
}