Repository navigation
Expand file tree
/
Copy pathResponse.php
More file actions
115 lines (101 loc) · 2.98 KB
/
Copy pathResponse.php
File metadata and controls
115 lines (101 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php namespace Dingo\Api\Http;
use RuntimeException;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Contracts\JsonableInterface;
use Illuminate\Support\Contracts\ArrayableInterface;
use Illuminate\Http\Response as IlluminateResponse;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
class Response extends IlluminateResponse {
/**
* Array of registered formatters.
*
* @var array
*/
protected static $formatters = [];
/**
* Make an API response from an existing Illuminate response.
*
* @param \Illuminate\Http\Response $response
* @return \Dingo\Api\Http\Response
*/
public static function makeFromExisting(\Illuminate\Http\Response $response)
{
return new static($response->getOriginalContent(), $response->getStatusCode(), $response->headers->all());
}
/**
* Process the API response.
*
* @return \Dingo\Api\Http\Response
*/
public function morph($format = 'json')
{
$formatter = static::getFormatter($format);
$response = $this->original;
// First we'll attempt to format the response if it's either an Eloquent
// model or an Eloquent collection.
if ($response instanceof EloquentModel)
{
$response = $formatter->formatEloquentModel($response);
}
elseif ($response instanceof EloquentCollection)
{
$response = $formatter->formatEloquentCollection($response);
}
else
{
// Next we'll attempt to format the response if it's a string,
// an array or an object implementing ArrayableInterface,
// an object implementing JsonableInterface or an
// unknown type.
if (is_string($response))
{
$response = $formatter->formatString($response);
}
elseif (is_array($response) or $response instanceof ArrayableInterface)
{
$response = $formatter->formatArrayableInterface($response);
}
elseif ($response instanceof JsonableInterface)
{
$response = $formatter->formatJsonableInterface($response);
}
else
{
$response = $formatter->formatUnknown($response);
}
}
// Set the "Content-Type" header of the response to that which
// is defined by the formatter being used.
$this->headers->set('content-type', $formatter->getContentType());
// Directly set the property because using setContent results in
// the original content also being updated.
$this->content = $response;
return $this;
}
/**
* Get the formatter based on the requested format type.
*
* @param string $format
* @return \Dingo\Api\Http\Format\FormatInterface
* @throws \RuntimeException
*/
public static function getFormatter($format)
{
if ( ! isset(static::$formatters[$format]))
{
throw new RuntimeException('Response formatter "'.$format.'" has not been registered.');
}
return static::$formatters[$format];
}
/**
* Set the response formatters.
*
* @param array $formatters
* @return void
*/
public static function setFormatters(array $formatters)
{
static::$formatters = $formatters;
}
}