$node->append(new Node('child-1')); $node->append(new Node('child-2')); $node->append(new Node('child-3')); $notAChild = new Node('not-a-child'); check(count($node->children) === 3, 'Child count is not 3'); $validParent = true; foreach ($node->children as $childNode) { if ($childNode->parent !== $node) { $validParent = false; } } (yield check($validParent, 'Parent not correctly set')); }, 'prepend' => function () { $node = new Node('parent'); $node->append(new Node('child-1')); $node->prepend($secondChild = new Node('child-2')); $node->prepend($thirdChild = new Node('child-3')); $notAChild = new Node('not-a-child'); (yield check(count($node->children) === 3, 'Child count is not 3')); $validParent = true; foreach ($node->children as $childNode) { if ($childNode->parent !== $node) { $validParent = false; } } (yield check($node->indexOf($secondChild) === 1, 'Second child not 1')); (yield check($node->indexOf($thirdChild) === 0, 'Third child not 0')); (yield check($validParent, 'Parent not correctly set')); }, 'remove' => function () { $node = new Node('parent'); $node->append(new Node('child-1'));
protected function handleBlock(Node $node) { if (!$node->name || $node->mode === 'ignore') { //Will be handled through compileBlock when the loop encounters it return $this; } //Find all other blocks with that name foreach ($this->_blocks as $block) { if ($block === $node || $block->name !== $node->name) { continue; } $mode = $block->mode; //detach from parent $block->parent->remove($block); switch ($mode) { default: case 'replace': $node->children = []; //WANTED FALLTHROUGH! //WANTED FALLTHROUGH! case 'append': //Append to master block foreach ($block->children as $child) { $block->remove($child); $node->append($child); } break; case 'prepend': $last = null; foreach ($block->children as $child) { $block->remove($child); if (!$last) { $node->prepend($child); $last = $child; continue; } $node->insertAfter($last, $child); $last = $child; } break; } $block->mode = 'ignore'; } }