ResponseTrait.php
3.8 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Illuminate\Http;
use Illuminate\Http\Exceptions\HttpResponseException;
use Symfony\Component\HttpFoundation\HeaderBag;
use Throwable;
trait ResponseTrait
{
/**
* The original content of the response.
*
* @var mixed
*/
public $original;
/**
* The exception that triggered the error response (if applicable).
*
* @var \Throwable|null
*/
public $exception;
/**
* Get the status code for the response.
*
* @return int
*/
public function status()
{
return $this->getStatusCode();
}
/**
* Get the status text for the response.
*
* @return string
*/
public function statusText()
{
return $this->statusText;
}
/**
* Get the content of the response.
*
* @return string
*/
public function content()
{
return $this->getContent();
}
/**
* Get the original response content.
*
* @return mixed
*/
public function getOriginalContent()
{
$original = $this->original;
return $original instanceof self ? $original->{__FUNCTION__}() : $original;
}
/**
* Set a header on the Response.
*
* @param string $key
* @param array|string $values
* @param bool $replace
* @return $this
*/
public function header($key, $values, $replace = true)
{
$this->headers->set($key, $values, $replace);
return $this;
}
/**
* Add an array of headers to the response.
*
* @param \Symfony\Component\HttpFoundation\HeaderBag|array $headers
* @return $this
*/
public function withHeaders($headers)
{
if ($headers instanceof HeaderBag) {
$headers = $headers->all();
}
foreach ($headers as $key => $value) {
$this->headers->set($key, $value);
}
return $this;
}
/**
* Add a cookie to the response.
*
* @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie
* @return $this
*/
public function cookie($cookie)
{
return $this->withCookie(...func_get_args());
}
/**
* Add a cookie to the response.
*
* @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie
* @return $this
*/
public function withCookie($cookie)
{
if (is_string($cookie) && function_exists('cookie')) {
$cookie = cookie(...func_get_args());
}
$this->headers->setCookie($cookie);
return $this;
}
/**
* Expire a cookie when sending the response.
*
* @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie
* @param string|null $path
* @param string|null $domain
* @return $this
*/
public function withoutCookie($cookie, $path = null, $domain = null)
{
if (is_string($cookie) && function_exists('cookie')) {
$cookie = cookie($cookie, null, -2628000, $path, $domain);
}
$this->headers->setCookie($cookie);
return $this;
}
/**
* Get the callback of the response.
*
* @return string|null
*/
public function getCallback()
{
return $this->callback ?? null;
}
/**
* Set the exception to attach to the response.
*
* @param \Throwable $e
* @return $this
*/
public function withException(Throwable $e)
{
$this->exception = $e;
return $this;
}
/**
* Throws the response in a HttpResponseException instance.
*
* @return void
*
* @throws \Illuminate\Http\Exceptions\HttpResponseException
*/
public function throwResponse()
{
throw new HttpResponseException($this);
}
}