Completed
Push — master ( b33be8...6037f4 )
by Tom
03:04
created

BasicProperties::createHook()   A

Complexity

Conditions 2
Paths 2

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.4286
cc 2
eloc 4
nc 2
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 $formatters = [];
11
	private $headers;
12
13
	public function __construct($data, &$headers) {
14
		$this->data = $data;
15
		$this->headers = &$headers;
16
	}
17
18
	public function content($value, $element, $rule) {
19
		$value = $this->format($value, $rule->getRules());
20
		if (!$this->processPseudo($value, $element, $rule)) {
21
			//Remove the current contents
22
			$this->removeAllChildren($element);
23
			//Now make a text node
24
			$this->appendContent($element, $value);
25
		}
26
	}
27
28
	private function processPseudo($value, $element, $rule) {
29
		return $this->pseudoAttr($value, $element, $rule) || $this->pseudoHeader($value, $element, $rule) || $this->pseudoBefore($value, $element, $rule) || $this->pseudoAfter($value, $element, $rule);
30
	}
31
32
	private function pseudoAttr($value, $element, $rule) {
33
		if ($attr = $rule->getPseudoMatcher()->attr()) {
34
			$element->setAttribute($attr, implode('', $value));
35
			return true;
36
		}
37
	}
38
39
	private function pseudoHeader($value, $element, $rule) {
40
		if ($header = $rule->getPseudoMatcher()->header($element)) {
41
			$this->headers[] = [$header, implode('', $value)];
42
			return true;
43
		}
44
	}
45
46
	private function pseudoBefore($value, $element, $rule) {
47 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...
48
			$element->firstChild->nodeValue = implode('', $value) . $element->firstChild->nodeValue;
49
			return true;
50
		}
51
	}
52
53
	private function pseudoAfter($value, $element, $rule) {
54 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...
55
		 	$element->firstChild->nodeValue .= implode('', $value);
56
		 	return true;
57
		 }		 
58
	}
59
60
	private function appendContent($element, $content) {
61
		if (isset($content[0]) && $content[0] instanceof \DomNode) {
62
			foreach ($content as $node) {
63
				$node = $element->ownerDocument->importNode($node, true);
64
				$element->appendChild($node);
65
			}
66
		}
67
		else $element->appendChild($element->ownerDocument->createTextNode(implode('', $content)));		
68
	}
69
70
	private function removeAllChildren($element) {
71
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
72
	}
73
74
	public function registerFormatter($formatter) {
75
		$this->formatters[] = $formatter;
76
	}
77
78
	private function format($value, $rules) {
79
		if (!isset($rules['format'])) return $value;
80
		$format = new \Transphporm\StringExtractor($rules['format']);
81
		$options = explode(' ', $format);
82
		$functionName = array_shift($options);
83
		foreach ($options as &$f) $f = trim($format->rebuild($f), '"');
84
85
		return $this->processFormat($options, $functionName, $value);		
86
	}
87
88
	private function processFormat($format, $functionName, $value) {
89
		foreach ($value as &$val) {
90
			foreach ($this->formatters as $formatter) {
91
				if (is_callable([$formatter, $functionName])) {
92
					$val = call_user_func_array([$formatter, $functionName], array_merge([$val], $format));
93
				}
94
			}
95
		}
96
		return $value;
97
	}
98
99
	private function createHook($newRules, $rule) {
100
		$hook = new Rule($newRules, $rule->getPseudoMatcher(), $this->data);
101
		foreach ($rule->getProperties() as $obj) $hook->registerProperties($obj);
102
		return $hook;
103
	}
104
105
	public function repeat($value, $element, $rule) {
106
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
107
108
		foreach ($value as $key => $iteration) {
109
			$clone = $element->cloneNode(true);
110
			//Mark this node as having been added by transphporm
111
			$clone->setAttribute('transphporm', 'added');
112
			$this->data->bind($clone, $iteration, 'iteration');
113
			$this->data->bind($clone, $key, 'key');
114
			$element->parentNode->insertBefore($clone, $element);
115
116
			//Re-run the hook on the new element, but use the iterated data
117
			$newRules = $rule->getRules();
118
			//Don't run repeat on the clones element or it will loop forever
119
			unset($newRules['repeat']);
120
						
121
			$this->createHook($newRules, $rule)->run($clone);
122
		}
123
		//Flag the original element for removal
124
		$element->setAttribute('transphporm', 'remove');
125
126
		return false;
127
	}
128
129
	public function display($value, $element) {
130
		if (strtolower($value[0]) === 'none') $element->setAttribute('transphporm', 'remove');
131
		else $element->setAttribute('transphporm', 'show');
132
	}
133
134
	public function bind($value, $element) {
135
		$this->data->bind($element, $value);
136
	}
137
138
}