1 | <?php |
||
8 | class Repeat implements \Transphporm\Property { |
||
9 | private $data; |
||
10 | |||
11 | public function __construct($data) { |
||
14 | |||
15 | public function run($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
||
16 | if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element); |
||
17 | |||
18 | $count = 0; |
||
19 | foreach ($value as $key => $iteration) { |
||
20 | $clone = $element->cloneNode(true); |
||
21 | //Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed |
||
22 | if ($count++ > 0) $clone->setAttribute('transphporm', 'added'); |
||
23 | $this->data->bind($clone, $iteration, 'iteration'); |
||
24 | $this->data->bind($clone, $key, 'key'); |
||
25 | $element->parentNode->insertBefore($clone, $element); |
||
26 | |||
27 | //Re-run the hook on the new element, but use the iterated data |
||
28 | //Don't run repeat on the clones element or it will loop forever |
||
29 | unset($rules['repeat']); |
||
30 | $this->createHook($rules, $pseudoMatcher, $properties)->run($clone); |
||
|
|||
31 | } |
||
32 | //Remove the original element |
||
33 | $element->parentNode->removeChild($element); |
||
34 | return false; |
||
35 | } |
||
36 | |||
37 | private function createHook($newRules, $pseudoMatcher, $properties) { |
||
42 | } |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.