<?php
/**
 * Link Column
 * 
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\ManagementBundle\AdminList\Column;

/**
 * Link Column
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class LinkColumn extends ColumnConfigurationAbstract implements ColumnConfigurationInterface
{

    /**
     * Route or callable that returns route
     * @var string|callable
     */
    private $route;

    /**
     * Parameter value, array with parameters or callable that returns parameters
     * @var string|array|callable
     */
    private $parameters;

    /**
     * Constructor
     * 
     * @param string                $field      Field (null for no field binding)
     * @param string                $title      Title
     * @param string|callable       $route      Route or callable that returns route
     * @param string|array|callable $parameters Parameter value, array with parameters or callable that returns parameters
     * @param boolean               $sortable   Sortable
     */
    public function __construct($field, $title, $route, $parameters = [], $sortable = false)
    {
        parent::__construct(
            $field,
            $title,
            $sortable,
            'VisualMManagementBundle:AdminList:Column/LinkColumn/listItem.html.twig',
            'VisualMManagementBundle:AdminList:Column/PlainValueColumn/listHeader.html.twig',
            null
        );

        $this->route = $route;
        $this->parameters = $parameters;
    }

    
    /**
     * Get Route for Action
     *
     * @param object $item Item
     *
     * @return string Route
     */
    public function getRoute($item)
    {
        if (is_callable($this->route)) {
            return call_user_func($this->route, $item);
        } else {
            return $this->route;
        }
    }

    /**
     * Get Parameters for Action
     *
     * @param object $item Item
     *
     * @return array Parameters
     */
    public function getParameters($item)
    {
        if ($this->parameters === null) {
            return array();
        } else if (is_string($this->parameters)) {
            return array($this->parameters => $item->{'get' . ucfirst($this->parameters)}());
        } else if (is_callable($this->parameters)) {
            return call_user_func($this->parameters, $item);
        } else {
            return $this->parameters;
        }
    }
    

}