Completed
Push — master ( 6df852...f7b88b )
by Josh
13:26
created

UninlineAttributes   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 74
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 9 2
A normalizeElement() 0 9 2
B uninlineAttribute() 0 28 3
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 DOMAttr;
11
use DOMElement;
12
use DOMXPath;
13
use s9e\TextFormatter\Configurator\Helpers\AVTHelper;
14
use s9e\TextFormatter\Configurator\TemplateNormalization;
15
16
class UninlineAttributes extends TemplateNormalization
17
{
18
	/**
19
	* Uninline element attributes
20
	*
21
	* Will replace
22
	*     <a href="{@url}">...</a>
23
	* with
24
	*     <a><xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>...</a>
25
	*
26
	* @param  DOMElement $template <xsl:template/> node
27
	* @return void
28
	*/
29
	public function normalize(DOMElement $template)
30
	{
31
		$xpath = new DOMXPath($template->ownerDocument);
32
		$query = '//*[namespace-uri() != "' . self::XMLNS_XSL . '"]';
33
		foreach ($xpath->query($query) as $element)
34
		{
35
			$this->normalizeElement($element);
36
		}
37
	}
38
39
	/**
40
	* Uninline an element's attributes
41
	*
42
	* @param  DOMElement $element
43
	* @return void
44
	*/
45
	protected function normalizeElement(DOMElement $element)
46
	{
47
		$firstChild = $element->firstChild;
48
		while ($element->attributes->length > 0)
49
		{
50
			$attribute = $element->attributes->item(0);
51
			$element->insertBefore($this->uninlineAttribute($attribute), $firstChild);
52
		}
53
	}
54
55
	/**
56
	* Remove an attribute from its parent element and return its content as an xsl:attribute
57
	*
58
	* @param  DOMAttr    $attribute Attribute node
59
	* @return DOMElement            xsl:attribute element
60
	*/
61
	protected function uninlineAttribute(DOMAttr $attribute)
62
	{
63
		$ownerDocument = $attribute->ownerDocument;
64
		$xslAttribute  = $ownerDocument->createElementNS(self::XMLNS_XSL, 'xsl:attribute');
65
		$xslAttribute->setAttribute('name', $attribute->nodeName);
66
67
		// Build the content of the xsl:attribute element
68
		foreach (AVTHelper::parse($attribute->value) as list($type, $content))
69
		{
70
			if ($type === 'expression')
71
			{
72
				$childNode = $ownerDocument->createElementNS(self::XMLNS_XSL, 'xsl:value-of');
73
				$childNode->setAttribute('select', $content);
74
			}
75
			else
76
			{
77
				$childNode = $ownerDocument->createElementNS(self::XMLNS_XSL, 'xsl:text');
78
				$childNode->appendChild($ownerDocument->createTextNode($content));
79
			}
80
81
			$xslAttribute->appendChild($childNode);
82
		}
83
84
		// Remove the attribute from its parent element
85
		$attribute->parentNode->removeAttributeNode($attribute);
86
87
		return $xslAttribute;
88
	}
89
}