public static function buildTreeAndGetRoots(array $objects)
    {
        $nodeClass = get_called_class(); // Use late static binding so new nodes are created as the calling subclass.

        // Build a TreeNode for each object.
        $nodes = array();
        foreach ($objects as $object) {
            $nodes[] = new $nodeClass($object);
        }

        // Loop through each node and update set its parent and children.
        foreach ($nodes as $node) {
            $object = $node->getObject();

            foreach ($nodes as $potentialParentNode) {
                if ($potentialParentNode->getObject()->isParentOf($object)) {
                    $node->setParent($potentialParentNode);
                    $potentialParentNode->addChild($node);

                    break;
                }
            }
        }

        // Now find the root nodes and return them.
        $roots = array();
        foreach ($nodes as $potentialRoot) {
            if ($potentialRoot->isRootNode()) {
                $roots[] = $potentialRoot;
            }
        }

        return $roots;
    }
public static function buildTreeAndGetRoots(array $objects)
    {
        $nodeClass = get_called_class(); // Use late static binding so new nodes are created as the calling subclass.

        // Build a TreeNode for each object.
        $nodes = array();
        foreach ($objects as $object) {
            $nodes[] = new $nodeClass($object);
        }

        // Loop through each node and update set its parent and children.
        foreach ($nodes as $node) {
            $object = $node->getObject();

            foreach ($nodes as $potentialParentNode) {
                if ($potentialParentNode->getObject()->isParentOf($object)) {
                    $node->setParent($potentialParentNode);
                    $potentialParentNode->addChild($node);

                    break;
                }
            }
        }

        // Now find the root nodes and return them.
        $roots = array();
        foreach ($nodes as $potentialRoot) {
            if ($potentialRoot->isRootNode()) {
                $roots[] = $potentialRoot;
            }
        }

        return $roots;
    }
public static function buildTreeAndGetRoots ( array $ objects ) { $ nodeClass = get_called_class ( ) ; // Use late static binding so new nodes are created as the calling subclass. // Build a TreeNode for each object. $ nodes = array ( ) ; foreach ( $ objects as $ object ) { $ nodes [ ] = new $ nodeClass ( $ object ) ; } // Loop through each node and update set its parent and children. foreach ( $ nodes as $ node ) { $ object = $ node -> getObject ( ) ; foreach ( $ nodes as $ potentialParentNode ) { if ( $ potentialParentNode -> getObject ( ) -> isParentOf ( $ object ) ) { $ node -> setParent ( $ potentialParentNode ) ; $ potentialParentNode -> addChild ( $ node ) ; break ; } } } // Now find the root nodes and return them. $ roots = array ( ) ; foreach ( $ nodes as $ potentialRoot ) { if ( $ potentialRoot -> isRootNode ( ) ) { $ roots [ ] = $ potentialRoot ; } } return $ roots ; }
Build a tree from an array of TreeNodeObjectInterface objects.

@param array $objects An array of TreeNodeObjectInterface objects to make into a tree.

@return array An array of TreeNodes, each representing a root (i.e. a node for which no parent was found).
Build a tree from an array of TreeNodeObjectInterface objects .