Completed
Branch wip (acc569)
by Josh
16:51 queued 07:59
created

Document::createXslAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
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
	* @return void
23
	*/
24
	public function __construct(string $version = '1.0', string $encoding = 'utf-8')
25
	{
26
		parent::__construct($version, $encoding);
27
28
		$this->registerNodeClass('DOMElement', Element::class);
29
	}
30
31
	/**
32
	* Create and return an xsl:apply-templates element
33
	*
34
	* @param  string  $select XPath expression for the "select" attribute
35
	* @return Element
36
	*/
37
	public function createXslApplyTemplates(string $select = null): Element
38
	{
39
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:apply-templates');
40
		if (isset($select))
41
		{
42
			$element->setAttribute('select', $select);
43
		}
44
45
		return $element;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $element returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
46
	}
47
48
	/**
49
	* Create and return an xsl:attribute element
50
	*
51
	* @param  string  $name      Attribute's name
52
	* @param  string  $namespace Attribute's namespace URI
53
	* @return Element
54
	*/
55
	public function createXslAttribute(string $name, string $namespace = null): Element
56
	{
57
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:attribute');
58
		$element->setAttribute('name', $name);
59
		if (isset($namespace))
60
		{
61
			$element->setAttribute('namespace', $namespace);
62
		}
63
64
		return $element;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $element returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
65
	}
66
67
	/**
68
	* Create and return an xsl:choose element
69
	*
70
	* @return Element
71
	*/
72
	public function createXslChoose(): Element
73
	{
74
		return $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:choose');
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->createElem...ansform', 'xsl:choose') returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
75
	}
76
77
	/**
78
	* Create and return an xsl:comment element
79
	*
80
	* @param  string  $text Text content for the comment
81
	* @return Element
82
	*/
83
	public function createXslComment(string $text = ''): Element
84
	{
85
		return $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:comment', htmlspecialchars($text, ENT_XML1));
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->createElem...s9e\SweetDOM\ENT_XML1)) returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
86
	}
87
88
	/**
89
	* Create and return an xsl:copy-of element
90
	*
91
	* @param  string  $select XPath expression for the "select" attribute
92
	* @return Element
93
	*/
94
	public function createXslCopyOf(string $select): Element
95
	{
96
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:copy-of');
97
		$element->setAttribute('select', $select);
98
99
		return $element;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $element returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
100
	}
101
102
	/**
103
	* Create and return an xsl:if element
104
	*
105
	* @param  string  $test XPath expression for the "test" attribute
106
	* @return Element
107
	*/
108
	public function createXslIf(string $test): Element
109
	{
110
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:if');
111
		$element->setAttribute('test', $test);
112
113
		return $element;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $element returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
114
	}
115
116
	/**
117
	* Create and return an xsl:otherwise element
118
	*
119
	* @return Element
120
	*/
121
	public function createXslOtherwise(): Element
122
	{
123
		return $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:otherwise');
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->createElem...form', 'xsl:otherwise') returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
124
	}
125
126
	/**
127
	* Create and return an xsl:param element
128
	*
129
	* @param  string  $name   Name of the parameter
130
	* @param  string  $select XPath expression
131
	* @return Element
132
	*/
133
	public function createXslParam(string $name, string $select = null): Element
134
	{
135
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:param');
136
		$element->setAttribute('name', $name);
137
		if (isset($select))
138
		{
139
			$element->setAttribute('select', $select);
140
		}
141
142
		return $element;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $element returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
143
	}
144
145
	/**
146
	* Create and return an xsl:text element
147
	*
148
	* @param  string  $text Text content for the element
149
	* @return Element
150
	*/
151
	public function createXslText(string $text = ''): Element
152
	{
153
		return $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:text', htmlspecialchars($text, ENT_XML1));
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->createElem...s9e\SweetDOM\ENT_XML1)) returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
154
	}
155
156
	/**
157
	* Create and return an xsl:value-of element
158
	*
159
	* @param  string  $select XPath expression for the "select" attribute
160
	* @return Element
161
	*/
162
	public function createXslValueOf(string $select): Element
163
	{
164
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:value-of');
165
		$element->setAttribute('select', $select);
166
167
		return $element;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $element returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
168
	}
169
170
	/**
171
	* Create and return an xsl:variable element
172
	*
173
	* @param  string  $name   Name of the variable
174
	* @param  string  $select XPath expression
175
	* @return Element
176
	*/
177
	public function createXslVariable(string $name, string $select = null): Element
178
	{
179
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:variable');
180
		$element->setAttribute('name', $name);
181
		if (isset($select))
182
		{
183
			$element->setAttribute('select', $select);
184
		}
185
186
		return $element;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $element returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
187
	}
188
189
	/**
190
	* Create and return an xsl:when element
191
	*
192
	* @param  string  $test XPath expression for the "test" attribute
193
	* @return Element
194
	*/
195
	public function createXslWhen(string $test): Element
196
	{
197
		$element = $this->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:when');
198
		$element->setAttribute('test', $test);
199
200
		return $element;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $element returns the type DOMElement which includes types incompatible with the type-hinted return s9e\SweetDOM\Element.
Loading history...
201
	}
202
203
	/**
204
	* Evaluate and return the result of a given XPath expression
205
	*
206
	* @param  string  $expr           XPath expression
207
	* @param  DOMNode $node           Context node
208
	* @param  bool    $registerNodeNS Whether to register the node's namespace
209
	* @return mixed
210
	*/
211
	public function evaluate(string $expr, DOMNode $node = null, bool $registerNodeNS = true)
212
	{
213
		return call_user_func_array([$this->xpath(), 'evaluate'], func_get_args());
214
	}
215
216
	/**
217
	* Evaluate and return the first element of a given XPath query
218
	*
219
	* @param  string      $expr           XPath expression
220
	* @param  DOMNode     $node           Context node
221
	* @param  bool        $registerNodeNS Whether to register the node's namespace
222
	* @return DOMNode|null
223
	*/
224
	public function firstOf(string $expr, DOMNode $node = null, bool $registerNodeNS = true): ?DOMNode
225
	{
226
		return call_user_func_array([$this, 'query'], func_get_args())->item(0);
227
	}
228
229
	/**
230
	* Evaluate and return the result of a given XPath query
231
	*
232
	* @param  string      $expr           XPath expression
233
	* @param  DOMNode     $node           Context node
234
	* @param  bool        $registerNodeNS Whether to register the node's namespace
235
	* @return DOMNodeList
236
	*/
237
	public function query(string $expr, DOMNode $node = null, bool $registerNodeNS = true): DOMNodeList
238
	{
239
		return call_user_func_array([$this->xpath(), 'query'], func_get_args());
240
	}
241
242
	protected function xpath(): DOMXPath
243
	{
244
		$xpath = new DOMXPath($this);
245
		$xpath->registerNamespace('xsl', 'http://www.w3.org/1999/XSL/Transform');
246
247
		return $xpath;
248
	}
249
}