Completed
Push — master ( 878197...378399 )
by Tom
07:30
created

Repeat::getMax()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
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 Repeat implements \Transphporm\Property {
9
	private $functionSet;
10
	private $elementData;
11
	private $line;
12
    private $filePath;
13
14
	public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$line, \Transphporm\FilePath $filePath) {
15
		$this->functionSet = $functionSet;
16
		$this->elementData = $elementData;
17
		$this->line = &$line;
18
        $this->filePath = $filePath;
19
	}
20
21
	public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
22
		$values = $this->fixEmpty($values);
23
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
24
25
		$contentMode = (isset($rules['content-mode'])) ? $rules['content-mode']->read() : 'replace';
26
27
		if ($contentMode == 'append') {
28
			$clone = $element->cloneNode(true);
29
			$clone->setAttribute('transphporm', 'immutable');
30
			$element->parentNode->insertBefore($clone, $element);
31
		}
32
33
		$max = $this->getMax($values);
34
		$count = 0;
35
		$repeat = $this->getRepeatValue($values, $max);
36
		//Don't run repeat on the cloned element or it will loop forever
37
		unset($rules['repeat']);
38
		$hook = $this->createHook($rules, $pseudoMatcher, $properties);
39
40
		foreach ($repeat as $key => $iteration) {
41
			if ($count+1 > $max) break;
42
			$clone = $this->cloneElement($element, $iteration, $key, $count++);
43
			//Re-run the hook on the new element, but use the iterated data
44
			$hook->run($clone);
45
		}
46
		//Remove the original element
47
		$element->parentNode->removeChild($element);
48
		return false;
49
	}
50
51
	private function getRepeatValue($values, &$max) {
52
		$mode = $this->getMode($values);
53
		$repeat = $values[0];
54
55
		if ($mode !== 'each') { // $mode === 'loop'
56
			$repeat = range($values[0], $max);
57
			$max++;
58
		}
59
		return $repeat;
60
	}
61
62
	private function getMode($args) {
63
		return isset($args[2]) ? $args[2] : 'each';
64
	}
65
66
	private function fixEmpty($value) {
67
		if (empty($value[0])) $value[0] = [];
68
		return $value;
69
	}
70
71
	private function cloneElement($element, $iteration, $key, $count) {
72
		$clone = $element->cloneNode(true);
73
		$this->tagElement($clone, $count);
74
75
		$this->elementData->bind($clone, $iteration, 'iteration');
76
		$this->elementData->bind($clone, $key, 'key');
77
		$element->parentNode->insertBefore($clone, $element);
78
		return $clone;
79
	}
80
81
	private function tagElement($element, $count) {
82
		//Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed
83
		if ($count > 0) $element->setAttribute('transphporm', 'added');
84
	}
85
86
	private function getMax($values) {
87
		return isset($values[1]) ? $values[1] : PHP_INT_MAX;
88
	}
89
90
	private function createHook($newRules, $pseudoMatcher, $properties) {
91
		$hook = new \Transphporm\Hook\PropertyHook($newRules, $this->line, null, $this->line, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet, $this->filePath);
92
		foreach ($properties as $name => $property) $hook->registerProperty($name, $property);
93
		return $hook;
94
	}
95
}
96