Completed
Pull Request — master (#64)
by Tom
02:17
created

Content::before()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2015 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.0                                                             */
7
namespace Transphporm\Property;
8
class Content implements \Transphporm\Property {
9
	private $data;
10
	private $headers;
11
	private $formatter;
12
13
14
	public function __construct($data, &$headers, \Transphporm\Hook\Formatter $formatter) {
15
		$this->data = $data;
16
		$this->headers = &$headers;
17
		$this->formatter = $formatter;
18
	}
19
20
	public function run($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
21
		if (!$this->shouldRun($element)) return;
22
	
23
		$value = $this->formatter->format($value, $rules);
24
		if (!$this->processPseudo($value, $element, $pseudoMatcher)) {
25
			//Remove the current contents
26
			$this->removeAllChildren($element);
27
			//Now make a text node
28
			if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $value);
29
			else $this->appendContent($element, $value);
30
		}
31
	}
32
33
	private function shouldRun($element) {
34
		if ($this->isIncludedTemplate($element)) return false;
35
		if ($element->getAttribute('transphporm') === 'remove') return false;
36
		return true;
37
	}
38
	private function isIncludedTemplate($element) {
39
		do {
40
			if ($element instanceof \DomDocument) return false;
41
			if ($element->getAttribute('transphporm') == 'includedtemplate') return true;
42
		}
43
		while ($element = $element->parentNode);
44
		return false;
45
	}
46
	private function getContentMode($rules) {
47
		return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append';
48
	}
49
50
	private function processPseudo($value, $element, $pseudoMatcher) {
51
		$pseudoContent = ['attr', 'header', 'before', 'after'];
52
		foreach ($pseudoContent as $pseudo) {
53
			if ($pseudoMatcher->hasFunction($pseudo)) {
54
				$this->$pseudo($value, $pseudoMatcher->getFuncArgs($pseudo), $element);
55
				return true;
56
			}
57
		}
58
		return false;
59
	}
60
	
61
	private function getNode($node, $document) {
62
		foreach ($node as $n) {
63
			if ($n instanceof \DomElement) {
64
				$new = $document->importNode($n, true);
65
				//Removing this might cause problems with caching... 
66
				//$new->setAttribute('transphporm', 'added');
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
			}
68
			else {
69
				if ($n instanceof \DomText) $n = $n->nodeValue;
70
				$new = $document->createElement('text');
71
				$new->appendChild($document->createTextNode($n));
72
				$new->setAttribute('transphporm', 'text');
73
			}
74
			yield $new;
75
		}
76
	}
77
78
	/** Functions for writing to pseudo elements, attr, before, after, header */
79
	private function attr($value, $pseudoArgs, $element) {
80
		$element->setAttribute($pseudoArgs, implode('', $value));
81
	}
82
83
	private function header($value, $pseudoArgs, $element) {
0 ignored issues
show
Unused Code introduced by
The parameter $element is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
		$this->headers[] = [$pseudoArgs, implode('', $value)];
85
	}
86
87
	private function before($value, $pseudoArgs, $element) {
0 ignored issues
show
Unused Code introduced by
The parameter $pseudoArgs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
		foreach ($this->getNode($value, $element->ownerDocument) as $node) {
89
			$element->insertBefore($node, $element->firstChild);	
90
		}
91
		return true;
92
	}
93
94
	private function after($value, $pseudoArgs, $element) {
0 ignored issues
show
Unused Code introduced by
The parameter $pseudoArgs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
		 foreach ($this->getNode($value, $element->ownerDocument) as $node) {
96
		 		$element->appendChild($node);
97
		}			 
98
	}
99
100
	private function removeAdded($e) {
101
		$remove = [];
102
		while ($e = $e->previousSibling && !in_array($e->getAttribute('transphporm'), [null, 'remove'])) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $e = ($e->previousSiblin...array(null, 'remove'))), Probably Intended Meaning: ($e = $e->previousSiblin... array(null, 'remove'))
Loading history...
103
			$remove[] = $e;
104
		}
105
		foreach ($remove as $r) $r->parentNode->removeChild($r);
106
	}
107
108
	private function replaceContent($element, $content) {
109
		//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again.
110
		$this->removeAdded($element);
111
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
112
			$element->parentNode->insertBefore($node, $element);
113
		}		
114
		$element->setAttribute('transphporm', 'remove');
115
	}
116
117
	private function appendContent($element, $content) {
118
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
119
			$element->appendChild($node);
120
		}
121
	}
122
	
123
	private function removeAllChildren($element) {
124
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
125
	}
126
}