Skip to content

Added jsonp support #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Exception/ResourceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class ResourceException extends HttpException {

/**
* MessageBag errors.
*
*
* @var \Illuminate\Support\MessageBag
*/
protected $errors;

/**
* Create a new resource exception instance.
*
*
* @param int $statusCode
* @param string $message
* @param \Illuminate\Support\MessageBag|array $errors
Expand All @@ -35,12 +35,12 @@ public function __construct($message = null, $errors = null, Exception $previous
$this->errors = is_array($errors) ? new MessageBag($errors) : $errors;
}

parent::__construct(422, $message, $previous, $headers, $code);
parent::__construct(400, $message, $previous, $headers, $code);
}

/**
* Get the errors message bag.
*
*
* @return \Illuminate\Support\MessageBag
**/
public function errors()
Expand All @@ -50,7 +50,7 @@ public function errors()

/**
* Get the errors message bag.
*
*
* @return \Illuminate\Support\MessageBag
**/
public function getErrors()
Expand All @@ -60,7 +60,7 @@ public function getErrors()

/**
* Determine if message bag has any errors.
*
*
* @return bool
*/
public function hasErrors()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,67 @@
<?php namespace Dingo\Api\Http\ResponseFormat;

interface ResponseFormatInterface {
abstract class AbstractResponseFormat {

/**
* Format an Eloquent model.
*
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return string
*/
public function formatEloquentModel($model);
abstract public function formatEloquentModel($model);

/**
* Format an Eloquent collection.
*
*
* @param \Illuminate\Database\Eloquent\Collection $collection
* @return string
*/
public function formatEloquentCollection($collection);
abstract public function formatEloquentCollection($collection);

/**
* Format a string.
*
*
* @param string $string
* @return string
*/
public function formatString($string);
abstract public function formatString($string);

/**
* Format an array or instance implementing ArrayableInterface.
*
*
* @param \Illuminate\Support\Contracts\ArrayableInterface $response
* @return string
*/
public function formatArrayableInterface($response);
abstract public function formatArrayableInterface($response);

/**
* Format an instance implementing JsonableInterface.
*
*
* @param \Illuminate\Support\Contracts\JsonableInterface $response
* @return string
*/
public function formatJsonableInterface($response);
abstract public function formatJsonableInterface($response);

/**
* Format an unknown type.
*
*
* @param mixed $response
* @return string
*/
public function formatUnknown($response);
abstract public function formatUnknown($response);

/**
* Get the response content type.
*
*
* @return string
*/
public function getContentType();

abstract public function getContentType();

/**
* Set the response content type.
*
* @return string
*/
abstract public function setRequest($request);

}
33 changes: 23 additions & 10 deletions src/Http/ResponseFormat/JsonResponseFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@

use Illuminate\Support\Contracts\ArrayableInterface;

class JsonResponseFormat implements ResponseFormatInterface {
class JsonResponseFormat extends AbstractResponseFormat {

/**
* The original request
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected $request;

public function setRequest($request)
{
$this->request = $request;
}

/**
* Format an Eloquent model.
*
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return string
*/
Expand All @@ -19,7 +32,7 @@ public function formatEloquentModel($model)

/**
* Format an Eloquent collection.
*
*
* @param \Illuminate\Database\Eloquent\Collection $collection
* @return string
*/
Expand All @@ -37,7 +50,7 @@ public function formatEloquentCollection($collection)

/**
* Format a string.
*
*
* @param string $string
* @return string
*/
Expand All @@ -48,7 +61,7 @@ public function formatString($string)

/**
* Format an array or instance implementing ArrayableInterface.
*
*
* @param \Illuminate\Support\Contracts\ArrayableInterface $response
* @return string
*/
Expand All @@ -66,7 +79,7 @@ public function formatArrayableInterface($response)

/**
* Format an instance implementing JsonableInterface.
*
*
* @param \Illuminate\Support\Contracts\JsonableInterface $response
* @return string
*/
Expand All @@ -77,7 +90,7 @@ public function formatJsonableInterface($response)

/**
* Format an unknown type.
*
*
* @param mixed $response
* @return string
*/
Expand All @@ -88,7 +101,7 @@ public function formatUnknown($response)

/**
* Get the response content type.
*
*
* @return string
*/
public function getContentType()
Expand All @@ -98,7 +111,7 @@ public function getContentType()

/**
* Morph a value to an array.
*
*
* @param array|\Illuminate\Support\Contracts\ArrayableInterface
* @return array
*/
Expand All @@ -109,7 +122,7 @@ protected function morphToArray($value)

/**
* Encode the content to its JSON representation.
*
*
* @param string $content
* @return string
*/
Expand Down
61 changes: 61 additions & 0 deletions src/Http/ResponseFormat/JsonpResponseFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php namespace Dingo\Api\Http\ResponseFormat;

use Illuminate\Http\Request as IlluminateRequest;
use Dingo\Api\Http\ResponseFormat\JsonResponseFormat;

class JsonpResponseFormat extends JsonResponseFormat
{

/**
* Name of the parameter to check if we want to issue a jsonp response
* if its not present it will fallback to json
*
* @var string
*/
protected $callbackName = 'callback';

/**
* Has a callback parameter been specified in the request ?
* @return bool
*/
protected function hasValidCallback()
{
$this->callback = $this->request->input($this->callbackName);

if (!empty($this->callback)) {
return true;
}

return false;
}

/**
* 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->callback, json_encode($content));
}

return parent::encode($content);
}

}
Loading