Completed
Push — master ( 8ef8f1...b6a30a )
by Tom
04:29
created

Content::replaceCachedContent()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
7
namespace Transphporm\Property;
8
class Content implements \Transphporm\Property {
9
	private $contentPseudo = [];
10
	private $formatter;
11
12
	public function __construct(\Transphporm\Hook\Formatter $formatter) {
13
		$this->formatter = $formatter;
14
	}
15
16
	public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
17
		if (!$this->shouldRun($element)) return false;
18
19
		$values = $this->formatter->format($values, $rules);
20
21
		if (!$this->processPseudo($values, $element, $pseudoMatcher)) {
22
			//Remove the current contents
23
			$this->removeAllChildren($element);
24
			//Now make a text node
25
			if ($this->getContentMode($rules) === 'replace') {
26
				$contentReplace = new ContentReplace($this);
27
				$contentReplace->replaceContent($element, $values);
28
			}
29
			else $this->appendContent($element, $values);
30
		}
31
	}
32
33
	private function shouldRun($element) {
34
		do {
35
			if ($element->getAttribute('transphporm') == 'includedtemplate') return false;
36
		}
37
		while (($element = $element->parentNode) instanceof \DomElement);
38
		return true;
39
	}
40
41
	private function getContentMode($rules) {
42
		return (isset($rules['content-mode'])) ? $rules['content-mode']->read() : 'append';
43
	}
44
45
	public function addContentPseudo($name, ContentPseudo $contentPseudo) {
46
		$this->contentPseudo[$name] = $contentPseudo;
47
	}
48
49
	private function processPseudo($value, $element, $pseudoMatcher) {
50
		foreach ($this->contentPseudo as $pseudoName => $pseudoFunction) {
51
			if ($pseudoMatcher->hasFunction($pseudoName)) {
52
				$pseudoFunction->run($value, $pseudoMatcher->getFuncArgs($pseudoName, $element)[0], $element, $pseudoMatcher);
53
				return true;
54
			}
55
		}
56
		return false;
57
	}
58
59
	public function getNode($node, $document) {
60
		foreach ($node as $n) {
61
			if (is_array($n)) {
62
				foreach ($this->getNode($n, $document) as $new) yield $new;
63
			}
64
			else {
65
				yield $this->convertNode($n, $document);
66
			}
67
		}
68
	}
69
70
	private function convertNode($node, $document) {
71
		if ($node instanceof \DomElement || $node instanceof \DOMComment) {
72
			$new = $document->importNode($node, true);
73
		}
74
		else {
75
			if ($node instanceof \DomText) $node = $node->nodeValue;
76
			$new = $document->createElement('text');
77
78
			$new->appendChild($document->createTextNode($node));
79
			$new->setAttribute('transphporm', 'text');
80
		}
81
		return $new;
82
	}
83
84
85
	private function appendContent($element, $content) {
86
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
87
			$element->appendChild($node);
88
		}
89
	}
90
91
	private function removeAllChildren($element) {
92
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
93
	}
94
}