Passed
Branch major/TemplateNormalizationsSw... (03d600)
by Josh
02:27
created

AbstractNormalization::xpath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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 DOMAttr;
11
use DOMComment;
12
use DOMNode;
13
use DOMText;
14
use s9e\SweetDOM\Document;
15
use s9e\SweetDOM\Element;
16
17
abstract class AbstractNormalization
18
{
19
	/**
20
	* XSL namespace
21
	*/
22
	const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform';
23
24
	/**
25
	* @var Document Document that holds the template being normalized
26
	*/
27
	protected Document $ownerDocument;
28
29
	/**
30
	* @var string[] XPath queries used to retrieve nodes of interest
31
	*/
32
	protected array $queries = [];
33
34
	/**
35
	* Apply this normalization rule to given template
36
	*
37
	* @param Element $template <xsl:template/> node
38
	*/
39
	public function normalize(Element $template): void
40
	{
41
		$this->ownerDocument = $template->ownerDocument;
42
		foreach ($this->getNodes() as $node)
43
		{
44
			// Ignore nodes that have been removed from the document
45
			if ($node->parentNode)
46
			{
47
				$this->normalizeNode($node);
48
			}
49
		}
50
		$this->reset();
51
	}
52
53
	/**
54
	* Create an xsl:text element or a text node in current template
55
	*
56
	* @param  string  $content
57
	* @return DOMNode
58
	*/
59
	protected function createText(string $content): DOMNode
60
	{
61
		return (trim($content) === '')
62
		     ? $this->ownerDocument->createXslText($content)
63
		     : $this->ownerDocument->createTextNode($content);
64
	}
65
66
	/**
67
	* Create a text node in current template
68
	*
69
	* @param  string  $content
70
	* @return DOMText
71
	*/
72
	protected function createTextNode($content): DOMText
73
	{
74
		return $this->ownerDocument->createTextNode($content);
75
	}
76
77
	/**
78
	* Query and return a list of nodes of interest
79
	*
80
	* @return DOMNode[]
81
	*/
82
	protected function getNodes(): array
83
	{
84
		$query = implode(' | ', $this->queries);
85
86
		return ($query === '') ? [] : iterator_to_array($this->ownerDocument->query($query));
87
	}
88
89
	/**
90
	* Test whether given node is an XSL element
91
	*
92
	* @param  DOMNode $node
93
	* @param  string  $localName
94
	* @return bool
95
	*/
96
	protected function isXsl(DOMNode $node, $localName = null)
97
	{
98
		return ($node->namespaceURI === self::XMLNS_XSL && (!isset($localName) || $localName === $node->localName));
99
	}
100
101
	/**
102
	* Make an ASCII string lowercase
103
	*
104
	* @param  string $str Original string
105
	* @return string      Lowercased string
106
	*/
107
	protected function lowercase($str)
108
	{
109
		return strtr($str, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
110
	}
111
112
	/**
113
	* Normalize given attribute
114
	*
115
	* @param  DOMAttr $attribute
116
	* @return void
117
	*/
118
	protected function normalizeAttribute(DOMAttr $attribute)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
	protected function normalizeAttribute(/** @scrutinizer ignore-unused */ DOMAttr $attribute)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
	{
120
	}
121
122
	/**
123
	* Normalize given element
124
	*/
125
	protected function normalizeElement(Element $element): void
1 ignored issue
show
Unused Code introduced by
The parameter $element is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

125
	protected function normalizeElement(/** @scrutinizer ignore-unused */ Element $element): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
	{
127
	}
128
129
	/**
130
	* Normalize given node
131
	*
132
	* @param  DOMNode $node
133
	* @return void
134
	*/
135
	protected function normalizeNode(DOMNode $node)
136
	{
137
		if ($node instanceof Element)
138
		{
139
			$this->normalizeElement($node);
140
		}
141
		elseif ($node instanceof DOMAttr)
142
		{
143
			$this->normalizeAttribute($node);
144
		}
145
		elseif ($node instanceof DOMText)
146
		{
147
			$this->normalizeText($node);
148
		}
149
	}
150
151
	/**
152
	* Normalize given text node
153
	*/
154
	protected function normalizeText(DOMText $node): void
155
	{
156
	}
157
158
	/**
159
	* Reset this instance's properties after usage
160
	*
161
	* @return void
162
	*/
163
	protected function reset()
164
	{
165
		unset($this->ownerDocument);
166
	}
167
}