Completed
Push — master ( 4744c5...b1b532 )
by Josh
15:48
created

AbstractChooseOptimization::hasOtherwise()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 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 DOMElement;
11
use DOMNode;
12
13
abstract class AbstractChooseOptimization extends AbstractNormalization
14
{
15
	/**
16
	* @var DOMElement Current xsl:choose element
17
	*/
18
	protected $choose;
19
20
	/**
21
	* {@inheritdoc}
22
	*/
23
	protected $queries = ['//xsl:choose'];
24
25
	/**
26
	* Retrieve a list of attributes from given element
27
	*
28
	* @return array NamespaceURI#nodeName as keys, attribute values as values
29
	*/
30
	protected function getAttributes(DOMElement $element)
31
	{
32
		$attributes = array();
33
		foreach ($element->attributes as $attribute)
34
		{
35
			$key = $attribute->namespaceURI . '#' . $attribute->nodeName;
36
			$attributes[$key] = $attribute->nodeValue;
37
		}
38
39
		return $attributes;
40
	}
41
42
	/**
43
	* Return a list the xsl:when and xsl:otherwise children of current xsl:choose element
44
	*
45
	* @return DOMElement[]
46
	*/
47
	protected function getBranches()
48
	{
49
		$query = 'xsl:when|xsl:otherwise';
50
51
		return $this->xpath($query, $this->choose);
52
	}
53
54
	/**
55
	* Test whether current xsl:choose element has an xsl:otherwise child
56
	*
57
	* @return bool
58
	*/
59
	protected function hasOtherwise()
60
	{
61
		return (bool) $this->xpath->evaluate('count(xsl:otherwise)', $this->choose);
62
	}
63
64
	/**
65
	* Test whether current xsl:choose element has no content besides xsl:when and xsl:otherwise
66
	*
67
	* @return bool
68
	*/
69
	protected function isEmpty()
70
	{
71
		$query = 'count(xsl:when/node() | xsl:otherwise/node())';
72
73
		return !$this->xpath->evaluate($query, $this->choose);
74
	}
75
76
	/**
77
	* Test whether two nodes are identical
78
	*
79
	* ext/dom does not support isEqualNode() from DOM Level 3 so this is a makeshift replacement.
80
	* Unlike the DOM 3 function, attributes order matters
81
	*
82
	* @param  DOMNode $node1
83
	* @param  DOMNode $node2
84
	* @return bool
85
	*/
86
	protected function isEqualNode(DOMNode $node1, DOMNode $node2)
87
	{
88
		return ($node1->ownerDocument->saveXML($node1) === $node2->ownerDocument->saveXML($node2));
89
	}
90
91
	/**
92
	* Test whether two elements have the same start tag
93
	*
94
	* @param  DOMElement $el1
95
	* @param  DOMElement $el2
96
	* @return bool
97
	*/
98
	protected function isEqualTag(DOMElement $el1, DOMElement $el2)
99
	{
100
		return ($el1->namespaceURI === $el2->namespaceURI && $el1->nodeName === $el2->nodeName && $this->getAttributes($el1) === $this->getAttributes($el2));
101
	}
102
103
	/**
104
	* {@inheritdoc}
105
	*/
106
	protected function normalizeElement(DOMElement $element)
107
	{
108
		$this->choose = $element;
109
		$this->optimizeChoose();
110
111
		// Remove current xsl:choose if it's still in the document but has no content
112
		if ($this->choose->parentNode && $this->isEmpty())
113
		{
114
			$this->choose->parentNode->removeChild($this->choose);
115
		}
116
	}
117
118
	/**
119
	* Optimize the current xsl:choose element
120
	*
121
	* @return void
122
	*/
123
	abstract protected function optimizeChoose();
124
125
	/**
126
	* {@inheritdoc}
127
	*/
128
	protected function reset()
129
	{
130
		$this->choose = null;
131
		parent::reset();
132
	}
133
}