<?php

namespace idoit\Module\Pro\Algorithm;

/**
 * This `FlatTree` is an alternative to `isys_tree` to retrieve a flat array when calling `toArray()`.
 *
 * @package     i-doit
 * @subpackage  General
 * @since       1.13.1
 * @copyright   synetics GmbH
 * @license     http://www.i-doit.com/license
 */
class FlatTree extends \isys_tree
{
    /**
     * Convert tree structure into flat (!!) array.
     *
     * @return  array
     */
    public function toArray()
    {
        $root = $this->m_children[0];
        $return = [$root->get_data('id') => $root->get_data()];

        foreach ($root->all_nodes() as $nodeCollection) {
            $return[$nodeCollection->get_data('id')] = $nodeCollection->get_data();

            foreach ($nodeCollection->toArray() as $node) {
                $return[$node['id']] = $node;
            }
        }

        return array_values($return);
    }
}
