Completed
Push — master ( 7bb784...cfb9bb )
by Josh
04:24
created

TemplateNormalizer::normalizeTemplate()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2019 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license'); The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator;
9
10
use ArrayAccess;
11
use Iterator;
12
use s9e\TextFormatter\Configurator\Collections\TemplateNormalizationList;
13
use s9e\TextFormatter\Configurator\Helpers\TemplateLoader;
14
use s9e\TextFormatter\Configurator\Items\Tag;
15
use s9e\TextFormatter\Configurator\Traits\CollectionProxy;
16
17
/**
18
* @method mixed   add(mixed $value, null $void)
19
* @method mixed   append(mixed $value)
20
* @method array   asConfig()
21
* @method void    clear()
22
* @method bool    contains(mixed $value)
23
* @method integer count()
24
* @method mixed   current()
25
* @method void    delete(string $key)
26
* @method bool    exists(string $key)
27
* @method mixed   get(string $key)
28
* @method mixed   indexOf(mixed $value)
29
* @method mixed   insert(integer $offset, mixed $value)
30
* @method integer|string key()
31
* @method mixed   next()
32
* @method integer normalizeKey(mixed $key)
33
* @method AbstractNormalization normalizeValue(mixed $value)
34
* @method bool    offsetExists(string|integer $offset)
35
* @method mixed   offsetGet(string|integer $offset)
36
* @method void    offsetSet(mixed $offset, mixed $value)
37
* @method void    offsetUnset(string|integer $offset)
38
* @method string  onDuplicate(string|null $action)
39
* @method mixed   prepend(mixed $value)
40
* @method integer remove(mixed $value)
41
* @method void    rewind()
42
* @method mixed   set(string $key, mixed $value)
43
* @method bool    valid()
44
*/
45
class TemplateNormalizer implements ArrayAccess, Iterator
46
{
47
	use CollectionProxy;
48
49
	/**
50
	* @var TemplateNormalizationList Collection of TemplateNormalization instances
51
	*/
52
	protected $collection;
53
54
	/**
55
	* @var string[] Default list of normalizations
56
	*/
57
	protected $defaultNormalizations = [
58
		'PreserveSingleSpaces',
59
		'RemoveComments',
60
		'RemoveInterElementWhitespace',
61
		'NormalizeElementNames',
62
		'FixUnescapedCurlyBracesInHtmlAttributes',
63
		'EnforceHTMLOmittedEndTags',
64
		'InlineCDATA',
65
		'InlineElements',
66
		'InlineTextElements',
67
		'UninlineAttributes',
68
		'MinifyXPathExpressions',
69
		'NormalizeAttributeNames',
70
		'OptimizeConditionalAttributes',
71
		'FoldArithmeticConstants',
72
		'FoldConstantXPathExpressions',
73
		'InlineXPathLiterals',
74
		'OptimizeChooseText',
75
		'OptimizeConditionalValueOf',
76
		'OptimizeChoose',
77
		'InlineAttributes',
78
		'NormalizeUrls',
79
		'InlineInferredValues',
80
		'SetRelNoreferrerOnTargetedLinks',
81
		'MinifyInlineCSS'
82
	];
83
84
	/**
85
	* @var integer Maximum number of iterations over a given template
86
	*/
87
	protected $maxIterations = 100;
88
89
	/**
90
	* Constructor
91
	*
92
	* Will load the default normalization rules if no list is passed
93
	*
94
	* @param array $normalizations List of normalizations
95
	*/
96 30
	public function __construct(array $normalizations = null)
97
	{
98 30
		if (!isset($normalizations))
99
		{
100 29
			$normalizations = $this->defaultNormalizations;
101
		}
102
103 30
		$this->collection = new TemplateNormalizationList;
104 30
		foreach ($normalizations as $normalization)
105
		{
106 29
			$this->collection->append($normalization);
107
		}
108
	}
109
110
	/**
111
	* Normalize a tag's template
112
	*
113
	* @param  Tag  $tag Tag whose template will be normalized
114
	* @return void
115
	*/
116 2
	public function normalizeTag(Tag $tag)
117
	{
118 2
		if (isset($tag->template) && !$tag->template->isNormalized())
119
		{
120 1
			$tag->template->normalize($this);
121
		}
122
	}
123
124
	/**
125
	* Normalize a template
126
	*
127
	* @param  string $template Original template
128
	* @return string           Normalized template
129
	*/
130 24
	public function normalizeTemplate($template)
131
	{
132 24
		$dom = TemplateLoader::load($template);
133
134
		// Apply all the normalizations until no more change is made or we've reached the maximum
135
		// number of loops
136 24
		$i = 0;
137
		do
138
		{
139 24
			$old = $template;
140 24
			foreach ($this->collection as $k => $normalization)
141
			{
142 24
				$normalization->normalize($dom->documentElement);
143
			}
144 24
			$template = TemplateLoader::save($dom);
145
		}
146 24
		while (++$i < $this->maxIterations && $template !== $old);
147
148 24
		return $template;
149
	}
150
}