Completed
Push — master ( 9d00eb...326616 )
by Tom
02:46
created

Repeat   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 12
Bugs 0 Features 0
Metric Value
wmc 12
c 12
b 0
f 0
lcom 1
cbo 3
dl 0
loc 51
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 16 4
A cloneElement() 0 9 1
A tagElement() 0 4 2
A getMax() 0 3 2
A createHook() 0 5 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         1.0                                                             */
7
namespace Transphporm\Property;
8
class Repeat implements \Transphporm\Property {
9
	private $functionSet;
10
	private $elementData;
11
12
	public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData) {
13
		$this->functionSet = $functionSet;
14
		$this->elementData = $elementData;		
15
	}
16
17
	public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
18
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
19
		$max = $this->getMax($values);
20
		$count = 0;
21
		foreach ($values[0] as $key => $iteration) {
22
			if ($count+1 > $max) break;
23
			$clone = $this->cloneElement($element, $iteration, $key, $count++);
24
			//Re-run the hook on the new element, but use the iterated data
25
			//Don't run repeat on the clones element or it will loop forever
26
			unset($rules['repeat']);
27
			$this->createHook($rules, $pseudoMatcher, $properties)->run($clone);
28
		}
29
		//Remove the original element
30
		$element->parentNode->removeChild($element);
31
		return false;
32
	}
33
34
	private function cloneElement($element, $iteration, $key, $count) {
35
		$clone = $element->cloneNode(true);	
36
		$this->tagElement($clone, $count);
37
38
		$this->elementData->bind($clone, $iteration, 'iteration');
39
		$this->elementData->bind($clone, $key, 'key');
40
		$element->parentNode->insertBefore($clone, $element);
41
		return $clone;
42
	}
43
44
	private function tagElement($element, $count) {
45
		//Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed
46
		if ($count > 0) $element->setAttribute('transphporm', 'added');
47
	}
48
49
	private function getMax($values) {
50
		return isset($values[1]) ? $values[1][0] : PHP_INT_MAX;
51
	}
52
53
	private function createHook($newRules, $pseudoMatcher, $properties) {
54
		$hook = new \Transphporm\Hook\PropertyHook($newRules, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet));
55
		foreach ($properties as $name => $property) $hook->registerProperty($name, $property);
56
		return $hook;
57
	}
58
}