Completed
Push — master ( 274a8c...2b20b1 )
by Josh
02:21
created

Document   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 213
rs 10
c 1
b 0
f 0
wmc 19

16 Methods

Rating   Name   Duplication   Size   Complexity  
A createXslComment() 0 3 1
A createXslValueOf() 0 6 1
A createXslOtherwise() 0 3 1
A createXslText() 0 3 1
A createXslApplyTemplates() 0 9 2
A createXslAttribute() 0 10 2
A createXslVariable() 0 10 2
A createXslChoose() 0 3 1
A evaluate() 0 3 1
A xpath() 0 6 1
A __construct() 0 5 1
A createXslCopyOf() 0 6 1
A createXslWhen() 0 6 1
A firstOf() 0 3 1
A query() 0 3 1
A createXslIf() 0 6 1
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\SweetDOM
5
* @copyright Copyright (c) 2019-2020 The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\SweetDOM;
9
10
use DOMDocument;
11
use DOMNode;
12
use DOMNodeList;
13
use DOMXPath;
14
15
class Document extends DOMDocument
16
{
17
	/**
18
	* @link https://www.php.net/manual/domdocument.construct.php
19
	*
20
	* @param string $version  Version number of the document
21
	* @param string $encoding Encoding of the document
22
	*/
23
	public function __construct(string $version = '1.0', string $encoding = 'utf-8')
24
	{
25
		parent::__construct($version, $encoding);
26
27
		$this->registerNodeClass('DOMElement', Element::class);
28
	}
29
30
	/**
31
	* Create and return an xsl:apply-templates element
32
	*
33
	* @param  string  $select XPath expression for the "select" attribute
34
	* @return Element
35
	*/
36
	public function createXslApplyTemplates(string $select = null): Element
37
	{
38
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:apply-templates');
39
		if (isset($select))
40
		{
41
			$element->setAttribute('select', $select);
42
		}
43
44
		return $element;
45
	}
46
47
	/**
48
	* Create and return an xsl:attribute element
49
	*
50
	* @param  string  $name      Attribute's name
51
	* @param  string  $namespace Attribute's namespace URI
52
	* @return Element
53
	*/
54
	public function createXslAttribute(string $name, string $namespace = null): Element
55
	{
56
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:attribute');
57
		$element->setAttribute('name', $name);
58
		if (isset($namespace))
59
		{
60
			$element->setAttribute('namespace', $namespace);
61
		}
62
63
		return $element;
64
	}
65
66
	/**
67
	* Create and return an xsl:choose element
68
	*
69
	* @return Element
70
	*/
71
	public function createXslChoose(): Element
72
	{
73
		return $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:choose');
74
	}
75
76
	/**
77
	* Create and return an xsl:comment element
78
	*
79
	* @param  string  $text Text content for the comment
80
	* @return Element
81
	*/
82
	public function createXslComment(string $text = ''): Element
83
	{
84
		return $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:comment', htmlspecialchars($text, ENT_XML1));
85
	}
86
87
	/**
88
	* Create and return an xsl:copy-of element
89
	*
90
	* @param  string  $select XPath expression for the "select" attribute
91
	* @return Element
92
	*/
93
	public function createXslCopyOf(string $select): Element
94
	{
95
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:copy-of');
96
		$element->setAttribute('select', $select);
97
98
		return $element;
99
	}
100
101
	/**
102
	* Create and return an xsl:if element
103
	*
104
	* @param  string  $test XPath expression for the "test" attribute
105
	* @return Element
106
	*/
107
	public function createXslIf(string $test): Element
108
	{
109
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:if');
110
		$element->setAttribute('test', $test);
111
112
		return $element;
113
	}
114
115
	/**
116
	* Create and return an xsl:otherwise element
117
	*
118
	* @return Element
119
	*/
120
	public function createXslOtherwise(): Element
121
	{
122
		return $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:otherwise');
123
	}
124
125
	/**
126
	* Create and return an xsl:text element
127
	*
128
	* @param  string  $text Text content for the element
129
	* @return Element
130
	*/
131
	public function createXslText(string $text = ''): Element
132
	{
133
		return $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:text', htmlspecialchars($text, ENT_XML1));
134
	}
135
136
	/**
137
	* Create and return an xsl:value-of element
138
	*
139
	* @param  string  $select XPath expression for the "select" attribute
140
	* @return Element
141
	*/
142
	public function createXslValueOf(string $select): Element
143
	{
144
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:value-of');
145
		$element->setAttribute('select', $select);
146
147
		return $element;
148
	}
149
150
	/**
151
	* Create and return an xsl:variable element
152
	*
153
	* @param  string  $name   Name of the variable
154
	* @param  string  $select XPath expression
155
	* @return Element
156
	*/
157
	public function createXslVariable(string $name, string $select = null): Element
158
	{
159
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:variable');
160
		$element->setAttribute('name', $name);
161
		if (isset($select))
162
		{
163
			$element->setAttribute('select', $select);
164
		}
165
166
		return $element;
167
	}
168
169
	/**
170
	* Create and return an xsl:when element
171
	*
172
	* @param  string  $test XPath expression for the "test" attribute
173
	* @return Element
174
	*/
175
	public function createXslWhen(string $test): Element
176
	{
177
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:when');
178
		$element->setAttribute('test', $test);
179
180
		return $element;
181
	}
182
183
	/**
184
	* Evaluate and return the result of a given XPath expression
185
	*
186
	* @param  string  $expr           XPath expression
187
	* @param  DOMNode $node           Context node
188
	* @param  bool    $registerNodeNS Whether to register the node's namespace
189
	* @return mixed
190
	*/
191
	public function evaluate(string $expr, DOMNode $node = null, bool $registerNodeNS = true)
192
	{
193
		return call_user_func_array([$this->xpath(), 'evaluate'], func_get_args());
194
	}
195
196
	/**
197
	* Evaluate and return the first element of a given XPath query
198
	*
199
	* @param  string      $expr           XPath expression
200
	* @param  DOMNode     $node           Context node
201
	* @param  bool        $registerNodeNS Whether to register the node's namespace
202
	* @return DOMNode|null
203
	*/
204
	public function firstOf(string $expr, DOMNode $node = null, bool $registerNodeNS = true): ?DOMNode
205
	{
206
		return call_user_func_array([$this, 'query'], func_get_args())->item(0);
207
	}
208
209
	/**
210
	* Evaluate and return the result of a given XPath query
211
	*
212
	* @param  string      $expr           XPath expression
213
	* @param  DOMNode     $node           Context node
214
	* @param  bool        $registerNodeNS Whether to register the node's namespace
215
	* @return DOMNodeList
216
	*/
217
	public function query(string $expr, DOMNode $node = null, bool $registerNodeNS = true): DOMNodeList
218
	{
219
		return call_user_func_array([$this->xpath(), 'query'], func_get_args());
220
	}
221
222
	protected function xpath(): DOMXPath
223
	{
224
		$xpath = new DOMXPath($this);
225
		$xpath->registerNamespace('xsl', 'http://www.w3.org/1999/XSL/Transform');
226
227
		return $xpath;
228
	}
229
}