<?php
/*
 * This file is part of the sfPropelActAsNestedSetBehavior package.
 * 
 * (c) 2006-2007 Tristan Rivoallan <tristan@rivoallan.net>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
 
/**
 * Helper functions for working with nested sets.
 */
class sfPropelActAsNestedSetBehaviorUtils
{
  
  /**
   * Dumps tree below given node.
   * 
   * @param     BaseObject    $root_node
   * @param     string        $indent_string
   * @param     string        $display_method
   */
  public static function dumpTree(BaseObject $root_node, $indent_string = "\t", $display_method = '__toString')
  {
    $tree_repr = array((string)$root_node);
    foreach ($root_node->getDescendants() as $node)
    {
      if (method_exists($node, $display_method))
      {
        $node_repr = $node->$display_method();
      }
      else
      {
        $node_repr = (string)$node;
      }
      $tree_repr[] = sprintf("%s%s", str_repeat($indent_string, $node->getLevel()), $node_repr);
    }
    
    return implode("\n", $tree_repr);
  }
  
}