diff --git a/src/Collection.php b/src/Collection.php index 38815d4..312a2b5 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -101,4 +101,49 @@ protected function getRootNodeId($root) return $root; } + + /** + * Build a list of nodes that retain the order that they were pulled from + * the database. + * + * @return Collection|static + */ + public function toFlattenedTree() + { + $tree = $this->toTree(); + + return $tree->flattenTree(); + } + + /** + * Flatten a tree into a non recursive array + */ + public function flattenTree() + { + $items = []; + + foreach ($this->items as $node) { + $items = array_merge($items, $this->flattenNode($node)); + } + + return new static($items); + } + + /** + * Flatten a single node + * + * @param $node + * @return array + */ + protected function flattenNode($node) + { + $items = []; + $items[] = $node; + + foreach ($node->children as $childNode) { + $items = array_merge($items, $this->flattenNode($childNode)); + } + + return $items; + } } \ No newline at end of file