Completed
Push — master ( 3479d6...be6b9d )
by Tom
03:47
created

BasicProperties::appendToIfNode()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 7
nc 3
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         0.9                                                             */
7
namespace Transphporm\Hook;
8
class BasicProperties {
9
	private $data;
10
	private $headers;
11
	private $formatter; 
12
13
	public function __construct($data, &$headers, Formatter $formatter) {
14
		$this->data = $data;
15
		$this->headers = &$headers;
16
		$this->formatter = $formatter;
17
	}
18
19
	public function content($value, $element, $rule) {
20
		$value = $this->formatter->format($value, $rule->getRules());
21
		if (!$this->processPseudo($value, $element, $rule)) {
22
			//Remove the current contents
23
			$this->removeAllChildren($element);
24
			//Now make a text node
25
			if ($this->getContentMode($rule->getRules()) === 'replace') $this->replaceContent($element, $value);
26
			else $this->appendContent($element, $value);
27
		}
28
	}
29
30
	private function getContentMode($rules) {
31
		return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append';
32
	}
33
34
	private function processPseudo($value, $element, $rule) {
35
		return $this->pseudoAttr($value, $element, $rule) || $this->pseudoHeader($value, $element, $rule) || $this->pseudoBefore($value, $element, $rule) || $this->pseudoAfter($value, $element, $rule);
36
	}
37
38
	private function pseudoAttr($value, $element, $rule) {
39
		if ($attr = $rule->getPseudoMatcher()->attr()) {
40
			$element->setAttribute($attr, implode('', $value));
41
			return true;
42
		}
43
	}
44
45
	private function pseudoHeader($value, $element, $rule) {
46
		if ($header = $rule->getPseudoMatcher()->header($element)) {
47
			$this->headers[] = [$header, implode('', $value)];
48
			return true;
49
		}
50
	}
51
52
	private function pseudoBefore($value, $element, $rule) {
53 View Code Duplication
		if (in_array('before', $rule->getPseudoMatcher()->getPseudo())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
			$element->firstChild->nodeValue = implode('', $value) . $element->firstChild->nodeValue;
55
			return true;
56
		}
57
	}
58
59
	private function pseudoAfter($value, $element, $rule) {
60 View Code Duplication
		 if (in_array('after', $rule->getPseudoMatcher()->getPseudo())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
		 	$element->firstChild->nodeValue .= implode('', $value);
62
		 	return true;
63
		 }		 
64
	}
65
66
	private function appendToIfNode($element, $content, $appendTo) {
67
		if (isset($content[0]) && $content[0] instanceof \DomNode) {
68
			foreach ($content as $node) {
69
				$node = $element->ownerDocument->importNode($node, true);
70
				$appendTo->appendChild($node);
71
			}
72
			return true;
73
		}
74
		return false;
75
	}
76
77
	private function replaceContent($element, $content) {
78 View Code Duplication
		if (!$this->appendToIfNode($element, $content, $element->parentNode)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
			$element->parentNode->appendChild($element->ownerDocument->createElement('span', implode('', $content)));
80
		}		
81
		$element->setAttribute('transphporm', 'remove');
82
	}
83
84
	private function appendContent($element, $content) {
85 View Code Duplication
		if (!$this->appendToIfNode($element, $content, $element)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
			$element->appendChild($element->ownerDocument->createTextNode(implode('', $content)));
87
		}
88
	}
89
90
	private function removeAllChildren($element) {
91
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
92
	}
93
94
	private function createHook($newRules, $rule) {
95
		$hook = new Rule($newRules, $rule->getPseudoMatcher(), $this->data);
96
		foreach ($rule->getProperties() as $obj) $hook->registerProperties($obj);
97
		return $hook;
98
	}
99
100
	public function repeat($value, $element, $rule) {
101
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
102
103
		foreach ($value as $key => $iteration) {
104
			$clone = $element->cloneNode(true);
105
			//Mark this node as having been added by transphporm
106
			$clone->setAttribute('transphporm', 'added');
107
			$this->data->bind($clone, $iteration, 'iteration');
108
			$this->data->bind($clone, $key, 'key');
109
			$element->parentNode->insertBefore($clone, $element);
110
111
			//Re-run the hook on the new element, but use the iterated data
112
			$newRules = $rule->getRules();
113
			//Don't run repeat on the clones element or it will loop forever
114
			unset($newRules['repeat']);
115
116
			$this->createHook($newRules, $rule)->run($clone);
117
		}
118
		//Flag the original element for removal
119
		$element->setAttribute('transphporm', 'remove');
120
		return false;
121
	}
122
123
	public function display($value, $element) {
124
		if (strtolower($value[0]) === 'none') $element->setAttribute('transphporm', 'remove');
125
		else $element->setAttribute('transphporm', 'show');
126
	}
127
128
	public function bind($value, $element) {
129
		$this->data->bind($element, $value);
130
	}
131
132
}