1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2023 The s9e authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Configurator\TemplateNormalizations; |
9
|
|
|
|
10
|
|
|
use DOMNode; |
11
|
|
|
use s9e\SweetDOM\Attr; |
12
|
|
|
use s9e\SweetDOM\Comment; |
13
|
|
|
use s9e\SweetDOM\CdataSection; |
14
|
|
|
use s9e\SweetDOM\Document; |
15
|
|
|
use s9e\SweetDOM\Element; |
16
|
|
|
use s9e\SweetDOM\Text; |
17
|
|
|
|
18
|
|
|
abstract class AbstractNormalization |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* XSL namespace |
22
|
|
|
*/ |
23
|
|
|
const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Document Document that holds the template being normalized |
27
|
|
|
*/ |
28
|
|
|
protected Document $ownerDocument; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string[] XPath queries used to retrieve nodes of interest |
32
|
|
|
*/ |
33
|
|
|
protected array $queries = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Apply this normalization rule to given template |
37
|
|
|
* |
38
|
|
|
* @param Element $template <xsl:template/> node |
39
|
|
|
*/ |
40
|
|
|
public function normalize(Element $template): void |
41
|
|
|
{ |
42
|
|
|
$this->ownerDocument = $template->ownerDocument; |
43
|
|
|
foreach ($this->getNodes() as $node) |
44
|
|
|
{ |
45
|
|
|
// Ignore nodes that have been removed from the document |
46
|
|
|
if ($node->parentNode) |
47
|
|
|
{ |
48
|
|
|
$this->normalizeNode($node); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
$this->reset(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create an xsl:text element or a text node in current template |
56
|
|
|
*/ |
57
|
|
|
protected function createPolymorphicText(string $textContent): Element|Text |
58
|
|
|
{ |
59
|
|
|
return (trim($textContent) === '') |
60
|
|
|
? $this->ownerDocument->nodeCreator->createXslText($textContent) |
61
|
|
|
: $this->ownerDocument->createTextNode($textContent); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Query and return a list of nodes of interest |
66
|
|
|
* |
67
|
|
|
* @return DOMNode[] |
68
|
|
|
*/ |
69
|
|
|
protected function getNodes(): array |
70
|
|
|
{ |
71
|
|
|
$query = implode(' | ', $this->queries); |
72
|
|
|
|
73
|
|
|
return ($query === '') ? [] : iterator_to_array($this->ownerDocument->query($query)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Test whether given node is an XSL element |
78
|
|
|
* |
79
|
|
|
* @param DOMNode $node |
80
|
|
|
* @param string $localName |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
protected function isXsl(DOMNode $node, $localName = null): bool |
84
|
|
|
{ |
85
|
|
|
return ($node->namespaceURI === self::XMLNS_XSL && (!isset($localName) || $localName === $node->localName)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Make an ASCII string lowercase |
90
|
|
|
* |
91
|
|
|
* @param string $str Original string |
92
|
|
|
* @return string Lowercased string |
93
|
|
|
*/ |
94
|
|
|
protected function lowercase($str) |
95
|
|
|
{ |
96
|
|
|
return strtr($str, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function normalizeAttribute(Attr $attribute): void |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function normalizeCdataSection(CdataSection $comment): void |
104
|
|
|
{ |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function normalizeComment(Comment $comment): void |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function normalizeElement(Element $element): void |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function normalizeNode(DOMNode $node): void |
116
|
|
|
{ |
117
|
|
|
if ($node instanceof Element) |
118
|
|
|
{ |
119
|
|
|
$this->normalizeElement($node); |
120
|
|
|
} |
121
|
|
|
elseif ($node instanceof Attr) |
122
|
|
|
{ |
123
|
|
|
$this->normalizeAttribute($node); |
124
|
|
|
} |
125
|
|
|
elseif ($node instanceof Text) |
126
|
|
|
{ |
127
|
|
|
$this->normalizeText($node); |
128
|
|
|
} |
129
|
|
|
elseif ($node instanceof Comment) |
130
|
|
|
{ |
131
|
|
|
$this->normalizeComment($node); |
132
|
|
|
} |
133
|
|
|
elseif ($node instanceof CdataSection) |
134
|
|
|
{ |
135
|
|
|
$this->normalizeCdataSection($node); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function normalizeText(Text $node): void |
140
|
|
|
{ |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Reset this instance's properties after usage |
145
|
|
|
*/ |
146
|
|
|
protected function reset(): void |
147
|
|
|
{ |
148
|
|
|
unset($this->ownerDocument); |
149
|
|
|
} |
150
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.