<?php namespace Dingo\Api\Http\ResponseFormat;

class JsonpResponseFormat extends JsonResponseFormat
{
    /**
     * Name of JSONP callback paramater.
     *
     * @var string
     */
    protected $callbackName = 'callback';

    /**
     * Determine if a callback is valid.
     *
     * @return bool
     */
    protected function hasValidCallback()
    {
        return $this->request->query->has($this->callbackName);
    }

    /**
     * Get the callback from the query string.
     *
     * @return string
     */
    protected function getCallback()
    {
        return $this->request->query->get($this->callbackName);
    }

    /**
     * Get the response content type.
     *
     * @return string
     */
    public function getContentType()
    {
        if ($this->hasValidCallback()) {
            return 'application/javascript';
        }

        return parent::getContentType();
    }

    /**
     * Encode the content to its JSON representation.
     *
     * @param  string  $content
     * @return string
     */
    protected function encode($content)
    {
        if ($this->hasValidCallback()) {
            return sprintf('%s(%s);', $this->getCallback(), json_encode($content));
        }

        return parent::encode($content);
    }
}

<?php namespace Dingo\Api\Http\ResponseFormat;

class JsonpResponseFormat extends JsonResponseFormat
{
    /**
     * Name of JSONP callback paramater.
     *
     * @var string
     */
    protected $callbackName = 'callback';

    /**
     * Create a new JSONP response formatter instance.
     *
     * @param  string  $callbackName
     * @return void
     */
    public function __construct($callbackName = 'callback')
    {
        $this->callbackName = $callbackName;
    }

    /**
     * Determine if a callback is valid.
     *
     * @return bool
     */
    protected function hasValidCallback()
    {
        return $this->request->query->has($this->callbackName);
    }

    /**
     * Get the callback from the query string.
     *
     * @return string
     */
    protected function getCallback()
    {
        return $this->request->query->get($this->callbackName);
    }

    /**
     * Get the response content type.
     *
     * @return string
     */
    public function getContentType()
    {
        if ($this->hasValidCallback()) {
            return 'application/javascript';
        }

        return parent::getContentType();
    }

    /**
     * Encode the content to its JSON representation.
     *
     * @param  string  $content
     * @return string
     */
    protected function encode($content)
    {
        if ($this->hasValidCallback()) {
            return sprintf('%s(%s);', $this->getCallback(), json_encode($content));
        }

        return parent::encode($content);
    }
}

Allow the callback parameter name to be changed in the constructor.
Allow the callback parameter name to be changed in the constructor.

Signed-off-by: Jason Lewis <b136be6b8ecc2c62ceb9857ec62bf85489a45e0c@gmail.com>
