<?php
/**
 * Table Configuration
 * 
 * @author Visual Media 
 */

namespace VisualM\ManagementBundle\AdminList;

use Exception;

/**
 * Table Configuration
 *
 * @author Visual Media 
 */
class TableConfiguration
{
    /**
     * ColumnConfigurations
     * 
     * @var array
     */
    private $columns;
    
    /**
     * Default Order Field
     * @var string
     */
    private $defaultOrderField = null;
    
    /**
     * Default Order Field
     * @var string
     */
    private $defaultOrderDir = null;
    
    /**
     * Constructor
     * 
     * @param array       $columns           ColumnConfigurations
     * @param string|null $defaultOrderField Default Order Field
     * @param string|null $defaultOrderDir   Default Order Direction
     */
    public function __construct(array $columns, $defaultOrderField = null, $defaultOrderDir = null)
    {
		$this->columns = array();
        foreach($columns as $column) {

			if ($column === null) {
                continue;
            } elseif ($column instanceof Column\Decorator\ColumnDecoratorInterface) {
                $this->columns[] = $column;
            } elseif ($column instanceof Column\ActionsColumn) {
                $this->columns[] = new Column\Decorator\ActionsDecorator('0*', $column);
            } elseif ($column instanceof Column\ColumnConfigurationInterface) {
                $this->columns[] = new Column\Decorator\NullDecorator($column);
            } else {
                throw new Exception(sprintf(
                    "Column configuration must be an implementation of ColumnDecoratorInterface or ColumnConfigurationInterface, %s given",
                    is_object($column) ? get_class($column) : gettype($column)
                ));
            }

        }
        $this->defaultOrderField = $defaultOrderField;
        $this->defaultOrderDir = $defaultOrderDir;
    }

    /**
     * Get Column Configurations
     * 
     * @return array Column Configurations
     */
    public function getColumns()
    {
        return $this->columns;
    }
    
    /**
     * Get Default Order Field
     * 
     * @return array Default Order Field
     */
    public function getDefaultOrderField()
    {
        return $this->defaultOrderField;
    }
    
    /**
     * Get Default Ordering Direction
     * 
     * @return array Default Order Direction
     */ 
    public function getDefaultOrderDir()
    {
        return $this->defaultOrderDir;
    }
    


}