Completed
Pull Request — master (#116)
by Richard
02:55
created

Content::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
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;
0 ignored issues
show
Unused Code introduced by
The property $data is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
	private $headers;
11
	private $formatter;
12
13
	public function __construct(&$headers, \Transphporm\Hook\Formatter $formatter) {
14
		$this->headers = &$headers;
15
		$this->formatter = $formatter;
16
	}
17
18
	public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
19
		if (!$this->shouldRun($element)) return false;
20
		$values = $this->formatter->format($values, $rules);
21
22
		if (!$this->processPseudo($values, $element, $pseudoMatcher)) {
23
			//Remove the current contents
24
			$this->removeAllChildren($element);
25
			//Now make a text node
26
			if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $values);
27
			else $this->appendContent($element, $values);
28
		}
29
	}
30
31
	private function shouldRun($element) {
32
		do {
33
			if ($element->getAttribute('transphporm') == 'includedtemplate') return false;
34
		}
35
		while (($element = $element->parentNode) instanceof \DomElement);
36
		return true;
37
	}
38
39
	private function getContentMode($rules) {
40
		return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append';
41
	}
42
43
	private function processPseudo($value, $element, $pseudoMatcher) {
44
		$pseudoContent = ['attr', 'header', 'before', 'after'];
45
		foreach ($pseudoContent as $pseudo) {
46
			if ($pseudoMatcher->hasFunction($pseudo)) {
47
				$this->$pseudo($value, $pseudoMatcher->getFuncArgs($pseudo, $element)[0], $element);
48
				return true;
49
			}
50
		}
51
		return false;
52
	}
53
54
	private function getNode($node, $document) {
55
		foreach ($node as $n) {
56
			if (is_array($n)) {
57
				foreach ($this->getNode($n, $document) as $new) yield $new;
58
			}
59
			else {
60
				if ($n instanceof \DomElement) {
61
					$new = $document->importNode($n, true);
62
					//Removing this might cause problems with caching...
63
					//$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...
64
				}
65
				else {
66
					if ($n instanceof \DomText) $n = $n->nodeValue;
67
					$new = $document->createElement('text');
68
69
					$new->appendChild($document->createTextNode($n));
70
					$new->setAttribute('transphporm', 'text');
71
				}
72
				yield $new;
73
			}
74
		}
75
	}
76
77
	/** Functions for writing to pseudo elements, attr, before, after, header */
78
	private function attr($value, $pseudoArgs, $element) {
79
		$element->setAttribute($pseudoArgs, implode('', $value));
80
	}
81
82
	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...
83
		$this->headers[] = [$pseudoArgs, implode('', $value)];
84
	}
85
86
	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...
87
		foreach ($this->getNode($value, $element->ownerDocument) as $node) {
88
			$element->insertBefore($node, $element->firstChild);
89
		}
90
		return true;
91
	}
92
93
	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...
94
		 foreach ($this->getNode($value, $element->ownerDocument) as $node) {
95
		 		$element->appendChild($node);
96
		}
97
	}
98
99
	private function replaceContent($element, $content) {
100
		//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again.
101
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
102
			$element->parentNode->insertBefore($node, $element);
103
		}
104
		$element->setAttribute('transphporm', 'remove');
105
	}
106
107
	private function appendContent($element, $content) {
108
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
109
			$element->appendChild($node);
110
		}
111
	}
112
113
	private function removeAllChildren($element) {
114
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
115
	}
116
}
117