|
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 s9e\SweetDOM\Element; |
|
11
|
|
|
use DOMNode; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractChooseOptimization extends AbstractNormalization |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Element Current xsl:choose element |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $choose; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
protected array $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
|
5 |
|
protected function getAttributes(Element $element) |
|
31
|
|
|
{ |
|
32
|
5 |
|
$attributes = array(); |
|
33
|
5 |
|
foreach ($element->attributes as $attribute) |
|
34
|
|
|
{ |
|
35
|
4 |
|
$key = $attribute->namespaceURI . '#' . $attribute->nodeName; |
|
36
|
4 |
|
$attributes[$key] = $attribute->nodeValue; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
return $attributes; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Return a list the xsl:when and xsl:otherwise children of current xsl:choose element |
|
44
|
|
|
* |
|
45
|
|
|
* @return Element[] |
|
46
|
|
|
*/ |
|
47
|
34 |
|
protected function getBranches(): array |
|
48
|
|
|
{ |
|
49
|
34 |
|
return iterator_to_array($this->choose->query('xsl:when|xsl:otherwise')); |
|
50
|
|
|
} |
|
51
|
34 |
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Test whether current xsl:choose element has an xsl:otherwise child |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function hasOtherwise() |
|
58
|
|
|
{ |
|
59
|
27 |
|
return (bool) $this->choose->evaluate('count(xsl:otherwise)'); |
|
60
|
|
|
} |
|
61
|
27 |
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Test whether current xsl:choose element has no content besides xsl:when and xsl:otherwise |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function isEmpty() |
|
68
|
|
|
{ |
|
69
|
18 |
|
return !$this->choose->evaluate('count(xsl:when/node() | xsl:otherwise/node())'); |
|
70
|
|
|
} |
|
71
|
18 |
|
|
|
72
|
|
|
/** |
|
73
|
18 |
|
* Test whether two elements have the same start tag |
|
74
|
|
|
* |
|
75
|
|
|
* @param Element $el1 |
|
76
|
|
|
* @param Element $el2 |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function isEqualTag(Element $el1, Element $el2) |
|
80
|
|
|
{ |
|
81
|
|
|
return ($el1->namespaceURI === $el2->namespaceURI && $el1->nodeName === $el2->nodeName && $this->getAttributes($el1) === $this->getAttributes($el2)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
15 |
|
*/ |
|
87
|
|
|
protected function normalizeElement(Element $element): void |
|
88
|
15 |
|
{ |
|
89
|
|
|
$this->choose = $element; |
|
90
|
|
|
$this->optimizeChoose(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Optimize the current xsl:choose element |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
5 |
|
abstract protected function optimizeChoose(); |
|
99
|
|
|
|
|
100
|
5 |
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function reset(): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->choose = null; |
|
106
|
36 |
|
parent::reset(); |
|
107
|
|
|
} |
|
108
|
|
|
} |