Completed
Branch TemplateNormalizations (ae42e4)
by Josh
33:16
created

MinifyXPathExpressions::normalize()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 1
dl 0
loc 34
rs 8.5806
c 0
b 0
f 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 DOMAttr;
11
use s9e\TextFormatter\Configurator\Helpers\AVTHelper;
12
use s9e\TextFormatter\Configurator\Helpers\XPathHelper;
13
14
class MinifyXPathExpressions extends AbstractNormalization
15
{
16
	/**
17
	* {@inheritdoc}
18
	*/
19
	protected $queries = ['//@*[contains(., " ")]'];
20
21
	/**
22
	* {@inheritdoc}
23
	*/
24
	protected function normalizeAttribute(DOMAttr $attribute)
25
	{
26
		$element = $attribute->parentNode;
27
		if (!$this->isXsl($element))
28
		{
29
			// Replace XPath expressions in non-XSL elements
30
			$this->replaceAVT($attribute);
31
		}
32
		elseif (in_array($attribute->nodeName, ['match', 'select', 'test'], true))
33
		{
34
			// Replace the content of match, select and test attributes of an XSL element
35
			$element->setAttribute(
36
				$attribute->nodeName,
37
				XPathHelper::minify($attribute->nodeValue)
38
			);
39
		}
40
	}
41
42
	/**
43
	* Minify XPath expressions in given attribute
44
	*
45
	* @param  DOMAttr $attribute
46
	* @return void
47
	*/
48
	protected function replaceAVT(DOMAttr $attribute)
49
	{
50
		AVTHelper::replace(
51
			$attribute,
52
			function ($token)
53
			{
54
				if ($token[0] === 'expression')
55
				{
56
					$token[1] = XPathHelper::minify($token[1]);
57
				}
58
59
				return $token;
60
			}
61
		);
62
	}
63
}