<?php
/**
 * Action
 * 
 * Action implementation, for usage in conjunction with the Actions Column
 * 
 * @author Visual Media 
 */

namespace VisualM\ManagementBundle\AdminList\Column\Action;

/**
 * Action
 *
 * @author Visual Media 
 */
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;
    }
    
}
