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