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
|
|
|
$rule = $rules['repeat']; |
|
|
|
|
20
|
|
|
|
21
|
|
|
$max = $this->getMax($values); |
22
|
|
|
$count = 0; |
23
|
|
|
foreach ($values[0] as $key => $iteration) { |
24
|
|
|
$clone = $element->cloneNode(true); |
25
|
|
|
//Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed |
26
|
|
|
if ($count++ > 0) $clone->setAttribute('transphporm', 'added'); |
27
|
|
|
if ($count > $max) break; |
28
|
|
|
|
29
|
|
|
$this->elementData->bind($clone, $iteration, 'iteration'); |
30
|
|
|
$this->elementData->bind($clone, $key, 'key'); |
31
|
|
|
$element->parentNode->insertBefore($clone, $element); |
32
|
|
|
|
33
|
|
|
//Re-run the hook on the new element, but use the iterated data |
34
|
|
|
//Don't run repeat on the clones element or it will loop forever |
35
|
|
|
unset($rules['repeat']); |
36
|
|
|
$this->createHook($rules, $pseudoMatcher, $properties)->run($clone); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
//Remove the original element |
39
|
|
|
$element->parentNode->removeChild($element); |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function getMax($values) { |
44
|
|
|
return isset($values[1]) ? $values[1][0] : PHP_INT_MAX; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function createHook($newRules, $pseudoMatcher, $properties) { |
48
|
|
|
$hook = new \Transphporm\Hook\PropertyHook($newRules, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet)); |
49
|
|
|
foreach ($properties as $name => $property) $hook->registerProperty($name, $property); |
50
|
|
|
return $hook; |
51
|
|
|
} |
52
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.