FluentDOM\Nodes::spawn PHP Method

spawn() public method

Create a new instance of the same class with $this as the parent. This is used for the chaining.
public spawn ( array | Traversable | DOMNode | Nodes $elements = NULL ) : Nodes
$elements array | Traversable | DOMNode | Nodes
return Nodes
    public function spawn($elements = NULL)
    {
        $result = clone $this;
        $result->_parent = $this;
        $result->_document = $this->getDocument();
        $result->_xpath = $this->getXpath();
        $result->_nodes = array();
        $result->_loadingContext = $this->_loadingContext;
        if (isset($elements)) {
            $result->push($elements);
        }
        return $result;
    }

Usage Example

Example #1
0
 public function groupByCallable(Nodes $nodes, callable $fn)
 {
     $grouping = [];
     foreach ($nodes as $node) {
         $key = $fn($node);
         if (!isset($grouping[$key])) {
             $grouping[$key] = [];
         }
         $grouping[$key][] = $node;
     }
     foreach ($grouping as $key => &$nodeList) {
         $nodeList = $nodes->spawn($nodeList);
     }
     return $grouping;
 }