Completed
Push — master ( 664dc7...fbc1f5 )
by Tom
03:03
created

BasicProperties::repeat()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 27
rs 8.5806
cc 4
eloc 15
nc 4
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 $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
	public function repeat($value, $element, $rule) {
100
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
101
102
		foreach ($value as $key => $iteration) {
103
			$clone = $element->cloneNode(true);
104
			//Mark this node as having been added by transphporm
105
			$clone->setAttribute('transphporm', 'added');
106
			$this->data->bind($clone, $iteration, 'iteration');
107
			$this->data->bind($clone, $key, 'key');
108
			$element->parentNode->insertBefore($clone, $element);
109
110
			//Re-run the hook on the new element, but use the iterated data
111
			$newRules = $rule->getRules();
112
113
			//Don't run repeat on the clones element or it will loop forever
114
			unset($newRules['repeat']);
115
116
			$hook = new Rule($newRules, $rule->getPseudoMatcher(), $this->data);
117
			foreach ($rule->getProperties() as $obj) $hook->registerProperties($obj);
118
			$hook->run($clone);
119
		}
120
121
		//Flag the original element for removal
122
		$element->setAttribute('transphporm', 'remove');
123
124
		return false;
125
	}
126
127
	public function display($value, $element) {
128
		if (strtolower($value[0]) === 'none') $element->setAttribute('transphporm', 'remove');
129
		else $element->setAttribute('transphporm', 'show');
130
	}
131
132
	public function bind($value, $element) {
133
		$this->data->bind($element, $value);
134
	}
135
136
}