Completed
Branch master (3d6b53)
by Tom
02:56
created

Content::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
rs 9.4285
cc 1
eloc 4
nc 1
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(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
21
		if (!$this->shouldRun($element)) return false;
22
		$values = $this->fixValues($this->formatter->format($values, $rules));
23
24
		if (!$this->processPseudo($values, $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, $values);
29
			else $this->appendContent($element, $values);
30
		}
31
	}
32
33
	//TODO: Why is $values sometimes a multidimensional array and other times a single dimensional array?
34
	//Need to backtrace this and work out where $values[0] is getting set and fix it there.
35
	private function fixValues($values) {
36
		return is_array($values[0]) ? $values[0] : $values;
37
	}
38
39
	private function shouldRun($element) {
40
		do {
41
			if ($element->getAttribute('transphporm') == 'includedtemplate') return false;
42
		}
43
		while (($element = $element->parentNode) instanceof \DomElement);
44
		return true;
45
	}
46
47
	private function getContentMode($rules) {
48
		return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append';
49
	}
50
51
	private function processPseudo($value, $element, $pseudoMatcher) {
52
		$pseudoContent = ['attr', 'header', 'before', 'after'];
53
		foreach ($pseudoContent as $pseudo) {
54
			if ($pseudoMatcher->hasFunction($pseudo)) {
55
				$this->$pseudo($value, $pseudoMatcher->getFuncArgs($pseudo), $element);
56
				return true;
57
			}
58
		}
59
		return false;
60
	}
61
	
62
	private function getNode($node, $document) {
63
		foreach ($node as $n) {
64
			if ($n instanceof \DomElement) {
65
				$new = $document->importNode($n, true);
66
				//Removing this might cause problems with caching... 
67
				//$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...
68
			}
69
			else {
70
				if ($n instanceof \DomText) $n = $n->nodeValue;
71
				$new = $document->createElement('text');
72
				
73
				$new->appendChild($document->createTextNode($n));
74
				$new->setAttribute('transphporm', 'text');
75
			}
76
			yield $new;
77
		}
78
	}
79
80
	/** Functions for writing to pseudo elements, attr, before, after, header */
81
	private function attr($value, $pseudoArgs, $element) {
82
		$element->setAttribute($pseudoArgs, implode('', $value));
83
	}
84
85
	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...
86
		$this->headers[] = [$pseudoArgs, implode('', $value)];
87
	}
88
89
	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...
90
		foreach ($this->getNode($value, $element->ownerDocument) as $node) {
91
			$element->insertBefore($node, $element->firstChild);	
92
		}
93
		return true;
94
	}
95
96
	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...
97
		 foreach ($this->getNode($value, $element->ownerDocument) as $node) {
98
		 		$element->appendChild($node);
99
		}			 
100
	}
101
102
	private function replaceContent($element, $content) {
103
		//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again.
104
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
105
			$element->parentNode->insertBefore($node, $element);
106
		}		
107
		$element->setAttribute('transphporm', 'remove');
108
	}
109
110
	private function appendContent($element, $content) {
111
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
112
			$element->appendChild($node);
113
		}
114
	}
115
	
116
	private function removeAllChildren($element) {
117
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
118
	}
119
}