|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Lenevor Framework |
|
5
|
|
|
* |
|
6
|
|
|
* LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
|
9
|
|
|
* with this package in the file license.md. |
|
10
|
|
|
* It is also available through the world-wide-web at this URL: |
|
11
|
|
|
* https://lenevor.com/license |
|
12
|
|
|
* If you did not receive a copy of the license and are unable to |
|
13
|
|
|
* obtain it through the world-wide-web, please send an email |
|
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Lenevor |
|
17
|
|
|
* @subpackage Base |
|
18
|
|
|
* @link https://lenevor.com |
|
19
|
|
|
* @copyright Copyright (c) 2019 - 2023 Alexander Campo <[email protected]> |
|
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace Syscodes\Components\Http\Resources; |
|
24
|
|
|
|
|
25
|
|
|
use Throwable; |
|
26
|
|
|
use Syscodes\Components\Http\Exceptions\HttpResponseException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Loads the response trait of headers, status code and content message. |
|
30
|
|
|
*/ |
|
31
|
|
|
trait HttpResponse |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* The content of the response. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string $content |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $content = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The exception that triggered the error response (if applicable). |
|
42
|
|
|
* |
|
43
|
|
|
* @var \Exception|null $exception |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $exception; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The Headers class instance. |
|
49
|
|
|
* |
|
50
|
|
|
* @var \Syscodes\Components\Http\ResponseHeaders $headers |
|
51
|
|
|
*/ |
|
52
|
|
|
public $headers; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The HTTP protocol version. |
|
56
|
|
|
* |
|
57
|
|
|
* @var string $version |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $version; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Gets the content of the response. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function content(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->getContent(); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Gets the status code for the response. |
|
73
|
|
|
* |
|
74
|
|
|
* @return int |
|
75
|
|
|
*/ |
|
76
|
|
|
public function status(): int |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->getStatusCode(); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Sets a header on the response. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $key The header name |
|
85
|
|
|
* @param string $values The value or an array of values |
|
86
|
|
|
* @param bool $replace If you want to replace the value exists by the header |
|
87
|
|
|
* |
|
88
|
|
|
* @return static |
|
89
|
|
|
*/ |
|
90
|
|
|
public function header($key, $values, $replace = true): static |
|
91
|
|
|
{ |
|
92
|
|
|
$this->headers->set($key, $values, $replace); |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Gets the Http protocol version. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getProtocolVersion(): string |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->version; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Sets the Http protocol version. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setProtocolVersion(string $version): void |
|
113
|
|
|
{ |
|
114
|
|
|
$this->version = $version; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Sets the exception to the response. |
|
119
|
|
|
* |
|
120
|
|
|
* @param \Throwable $e |
|
121
|
|
|
* |
|
122
|
|
|
* @return static |
|
123
|
|
|
*/ |
|
124
|
|
|
public function withException(Throwable $e): static |
|
125
|
|
|
{ |
|
126
|
|
|
$this->exception = $e; |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Throws the response in a HttpResponseException instance. |
|
133
|
|
|
* |
|
134
|
|
|
* @throws \Syscodes\Components\Http\Exceptions\HttpResponseException |
|
135
|
|
|
*/ |
|
136
|
|
|
public function throwResponse() |
|
137
|
|
|
{ |
|
138
|
|
|
throw new HttpResponseException($this); |
|
139
|
|
|
} |
|
140
|
|
|
} |