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

namespace VisualM\ManagementBundle\AdminList\Column;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyPath;

/**
 * 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
     * @param string  $itemTemplate   Item Template
     * @param string  $headerTemplate Header Template
     */
    public function __construct(
        $field,
        $title,
        $route,
        $parameters = [],
        $sortable = false,
        $itemTemplate = false,
        $headerTemplate = 'VisualMManagementBundle:AdminList:Column/PlainValueColumn/listHeader.html.twig'
    )
    {

        if (false === $itemTemplate) {
            $itemTemplate = null === $route
                ? 'VisualMManagementBundle:AdminList:Column/PlainValueColumn/listItem.html.twig'
                : 'VisualMManagementBundle:AdminList:Column/LinkColumn/listItem.html.twig';
        }

        parent::__construct(
            $field,
            $title,
            $sortable,
            $itemTemplate,
            $headerTemplate,
            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();

        } elseif (is_string($this->parameters)) {
            $accessor = PropertyAccess::createPropertyAccessor();
            $propertyPath = new PropertyPath($this->parameters);
            $key = $propertyPath->getLength() == 1 ? $this->parameters : 'id';
            return array( $key => $accessor->getValue($item, $propertyPath) );
        } elseif (is_callable($this->parameters)) {
            return call_user_func($this->parameters, $item);
        } else {
            return $this->parameters;
        }
    }
    

}