Completed
Push — master ( 6037f4...be4263 )
by Tom
04:40
created

BasicProperties::bind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
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         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
			$this->appendContent($element, $value);
26
		}
27
	}
28
29
	private function processPseudo($value, $element, $rule) {
30
		return $this->pseudoAttr($value, $element, $rule) || $this->pseudoHeader($value, $element, $rule) || $this->pseudoBefore($value, $element, $rule) || $this->pseudoAfter($value, $element, $rule);
31
	}
32
33
	private function pseudoAttr($value, $element, $rule) {
34
		if ($attr = $rule->getPseudoMatcher()->attr()) {
35
			$element->setAttribute($attr, implode('', $value));
36
			return true;
37
		}
38
	}
39
40
	private function pseudoHeader($value, $element, $rule) {
41
		if ($header = $rule->getPseudoMatcher()->header($element)) {
42
			$this->headers[] = [$header, implode('', $value)];
43
			return true;
44
		}
45
	}
46
47
	private function pseudoBefore($value, $element, $rule) {
48 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...
49
			$element->firstChild->nodeValue = implode('', $value) . $element->firstChild->nodeValue;
50
			return true;
51
		}
52
	}
53
54
	private function pseudoAfter($value, $element, $rule) {
55 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...
56
		 	$element->firstChild->nodeValue .= implode('', $value);
57
		 	return true;
58
		 }		 
59
	}
60
61
	private function appendContent($element, $content) {
62
		if (isset($content[0]) && $content[0] instanceof \DomNode) {
63
			foreach ($content as $node) {
64
				$node = $element->ownerDocument->importNode($node, true);
65
				$element->appendChild($node);
66
			}
67
		}
68
		else $element->appendChild($element->ownerDocument->createTextNode(implode('', $content)));		
69
	}
70
71
	private function removeAllChildren($element) {
72
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
73
	}
74
75
	private function createHook($newRules, $rule) {
76
		$hook = new Rule($newRules, $rule->getPseudoMatcher(), $this->data);
77
		foreach ($rule->getProperties() as $obj) $hook->registerProperties($obj);
78
		return $hook;
79
	}
80
81
	public function repeat($value, $element, $rule) {
82
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
83
84
		foreach ($value as $key => $iteration) {
85
			$clone = $element->cloneNode(true);
86
			//Mark this node as having been added by transphporm
87
			$clone->setAttribute('transphporm', 'added');
88
			$this->data->bind($clone, $iteration, 'iteration');
89
			$this->data->bind($clone, $key, 'key');
90
			$element->parentNode->insertBefore($clone, $element);
91
92
			//Re-run the hook on the new element, but use the iterated data
93
			$newRules = $rule->getRules();
94
			//Don't run repeat on the clones element or it will loop forever
95
			unset($newRules['repeat']);
96
97
			$this->createHook($newRules, $rule)->run($clone);
98
		}
99
		//Flag the original element for removal
100
		$element->setAttribute('transphporm', 'remove');
101
102
		return false;
103
	}
104
105
	public function display($value, $element) {
106
		if (strtolower($value[0]) === 'none') $element->setAttribute('transphporm', 'remove');
107
		else $element->setAttribute('transphporm', 'show');
108
	}
109
110
	public function bind($value, $element) {
111
		$this->data->bind($element, $value);
112
	}
113
114
}