Ejemplo n.º 1
0
 public function replaceWith($nodes)
 {
     if (!$this->parent) {
         return $this;
     }
     if ($nodes instanceof Node) {
         if ($nodes === $this) {
             return $this;
         }
         $nodes->remove();
         $this->parent->replaceChild($this, $nodes);
     } elseif ($nodes instanceof NodeCollection || is_array($nodes)) {
         $first = TRUE;
         $insert_after = NULL;
         /** @var Node $node */
         foreach ($nodes as $node) {
             if ($first) {
                 if ($node !== $this) {
                     $node->remove();
                     $this->parent->replaceChild($this, $node);
                 }
                 $insert_after = $node;
                 $first = FALSE;
             } else {
                 $node->remove();
                 $insert_after->parent->insertAfterChild($insert_after, $node);
                 $insert_after = $node;
             }
         }
     } elseif (is_callable($nodes)) {
         $this->replaceWith($nodes($this));
     } else {
         throw new \InvalidArgumentException();
     }
     return $this;
 }