Completed
Push — master ( a7088b...77f96d )
by Josh
22:56
created

RulesGenerator::generateRootInspector()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 24
rs 8.9713
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;
9
10
use ArrayAccess;
11
use DOMDocument;
12
use Iterator;
13
use s9e\TextFormatter\Configurator\Collections\RulesGeneratorList;
14
use s9e\TextFormatter\Configurator\Collections\TagCollection;
15
use s9e\TextFormatter\Configurator\Helpers\TemplateInspector;
16
use s9e\TextFormatter\Configurator\RulesGenerators\Interfaces\BooleanRulesGenerator;
17
use s9e\TextFormatter\Configurator\RulesGenerators\Interfaces\TargetedRulesGenerator;
18
use s9e\TextFormatter\Configurator\Traits\CollectionProxy;
19
20
/**
21
* @method mixed   add(mixed $value, null $void)
22
* @method mixed   append(mixed $value)
23
* @method array   asConfig()
24
* @method void    clear()
25
* @method bool    contains(mixed $value)
26
* @method integer count()
27
* @method mixed   current()
28
* @method void    delete(string $key)
29
* @method bool    exists(string $key)
30
* @method mixed   get(string $key)
31
* @method mixed   indexOf(mixed $value)
32
* @method mixed   insert(integer $offset, mixed $value)
33
* @method integer|string key()
34
* @method mixed   next()
35
* @method integer normalizeKey(mixed $key)
36
* @method BooleanRulesGenerator|TargetedRulesGenerator normalizeValue(string|BooleanRulesGenerator|TargetedRulesGenerator $generator)
37
* @method bool    offsetExists(string|integer $offset)
38
* @method mixed   offsetGet(string|integer $offset)
39
* @method void    offsetSet(mixed $offset, mixed $value)
40
* @method void    offsetUnset(string|integer $offset)
41
* @method string  onDuplicate(string|null $action)
42
* @method mixed   prepend(mixed $value)
43
* @method integer remove(mixed $value)
44
* @method void    rewind()
45
* @method mixed   set(string $key, mixed $value)
46
* @method bool    valid()
47
*/
48
class RulesGenerator implements ArrayAccess, Iterator
49
{
50
	use CollectionProxy;
51
52
	/**
53
	* @var RulesGeneratorList Collection of objects
54
	*/
55
	protected $collection;
56
57
	/**
58
	* Constructor
59
	*
60
	* Will load the default rule generators
61
	*/
62
	public function __construct()
63
	{
64
		$this->collection = new RulesGeneratorList;
65
		$this->collection->append('AutoCloseIfVoid');
66
		$this->collection->append('AutoReopenFormattingElements');
67
		$this->collection->append('BlockElementsFosterFormattingElements');
68
		$this->collection->append('DisableAutoLineBreaksIfNewLinesArePreserved');
69
		$this->collection->append('EnforceContentModels');
70
		$this->collection->append('EnforceOptionalEndTags');
71
		$this->collection->append('IgnoreTagsInCode');
72
		$this->collection->append('IgnoreTextIfDisallowed');
73
		$this->collection->append('IgnoreWhitespaceAroundBlockElements');
74
		$this->collection->append('TrimFirstLineInCodeBlocks');
75
	}
76
77
	/**
78
	* Generate rules for given tag collection
79
	*
80
	* @param  TagCollection $tags Tags collection
81
	* @return array
82
	*/
83
	public function getRules(TagCollection $tags)
84
	{
85
		// Create a proxy for the parent markup so that we can determine which tags are allowed at
86
		// the root of the text (IOW, with no parent) or even disabled altogether
87
		$rootInspector = new TemplateInspector('<div><xsl:apply-templates/></div>');
88
89
		// Study the tags
90
		$templateInspector = [];
91
		foreach ($tags as $tagName => $tag)
92
		{
93
			// Use the tag's template if applicable or XSLT's implicit default otherwise
94
			$template = (isset($tag->template)) ? $tag->template : '<xsl:apply-templates/>';
95
			$templateInspector[$tagName] = new TemplateInspector($template);
96
		}
97
98
		// Generate a full set of rules
99
		$rules = $this->generateRulesets($templateInspector, $rootInspector);
100
101
		// Remove root rules that wouldn't be applied anyway
102
		unset($rules['root']['autoClose']);
103
		unset($rules['root']['autoReopen']);
104
		unset($rules['root']['breakParagraph']);
105
		unset($rules['root']['closeAncestor']);
106
		unset($rules['root']['closeParent']);
107
		unset($rules['root']['fosterParent']);
108
		unset($rules['root']['ignoreSurroundingWhitespace']);
109
		unset($rules['root']['isTransparent']);
110
		unset($rules['root']['requireAncestor']);
111
		unset($rules['root']['requireParent']);
112
113
		return $rules;
114
	}
115
116
	/**
117
	* Generate and return rules based on a set of TemplateInspector
118
	*
119
	* @param  array             $templateInspector Array of [tagName => TemplateInspector]
120
	* @param  TemplateInspector $rootInspector     TemplateInspector for the root of the text
121
	* @return array
122
	*/
123
	protected function generateRulesets(array $templateInspector, TemplateInspector $rootInspector)
124
	{
125
		$rules = [
126
			'root' => $this->generateRuleset($rootInspector, $templateInspector),
127
			'tags' => []
128
		];
129
		foreach ($templateInspector as $tagName => $src)
130
		{
131
			$rules['tags'][$tagName] = $this->generateRuleset($src, $templateInspector);
132
		}
133
134
		return $rules;
135
	}
136
137
	/**
138
	* Generate a set of rules for a single TemplateInspector instance
139
	*
140
	* @param  TemplateInspector $src     Source of the rules
141
	* @param  array             $targets Array of [tagName => TemplateInspector]
142
	* @return array
143
	*/
144
	protected function generateRuleset(TemplateInspector $src, array $targets)
145
	{
146
		$rules = [];
147
		foreach ($this->collection as $rulesGenerator)
148
		{
149
			if ($rulesGenerator instanceof BooleanRulesGenerator)
150
			{
151
				foreach ($rulesGenerator->generateBooleanRules($src) as $ruleName => $bool)
152
				{
153
					$rules[$ruleName] = $bool;
154
				}
155
			}
156
157
			if ($rulesGenerator instanceof TargetedRulesGenerator)
158
			{
159
				foreach ($targets as $tagName => $trg)
160
				{
161
					foreach ($rulesGenerator->generateTargetedRules($src, $trg) as $ruleName)
162
					{
163
						$rules[$ruleName][] = $tagName;
164
					}
165
				}
166
			}
167
		}
168
169
		return $rules;
170
	}
171
}