<?php
/**
 * Action
 * 
 * Action implementation, for usage in conjunction with the Actions Column
 * 
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\ManagementBundle\AdminList\Column\Action;

/**
 * Action
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class Action implements ActionInterface
{

    private $route;
    private $parameters;
    private $glyphicon;
    private $class;
    private $title;
    private $extraAttr;

    /**
     * Constructor
     *  
     * @param string|callable       $route       Route
     * @param string|array|callable $parameters  Parameters
     * @param string|callable       $title       Title
     * @param string|callable       $class       Class
     * @param string|callable       $glyphicon   Glymphicon
     * @param array|callable        $extraAttr   Extra attributes
     */
    function __construct($route, $parameters, $title, $class = null, $glyphicon = null, $extraAttr = array())
    {
        $this->route = $route;
        $this->parameters = $parameters;
        $this->glyphicon = $glyphicon;
        $this->class = $class;
        $this->title = $title;
        $this->extraAttr = $extraAttr;
    }

    
    
    /**
     * {@inheritdoc}
     */
    public function getRoute($item)
    {
        if (is_callable($this->route)) {
            return call_user_func($this->route, $item);
        } else {
            return $this->route;
        }
    }

    /**
     * {@inheritdoc}
     */
    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;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getGlyphicon($item)
    {
        if (is_callable($this->glyphicon)) {
            return call_user_func($this->glyphicon, $item);
        } else {
            return $this->glyphicon;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getClass($item)
    {
        if (is_callable($this->class)) {
            return call_user_func($this->class, $item);
        } else {
            return $this->class;
        }
   }

    /**
     * {@inheritdoc}
     */
    public function getTitle($item)
    {
        if (is_callable($this->title)) {
            return call_user_func($this->title, $item);
        } else {
            return $this->title;
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getExtraAttr($item)
    {
        if (is_callable($this->extraAttr)) {
            return call_user_func($this->extraAttr, $item);
        } else {
            return $this->extraAttr;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function isPrimaryAction()
    {
        return false;
    }
    
}
