PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Framework / Http / Response / Traits / ResponseTrait.php

ResponseTrait.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Framework/Http/Response/Traits/ResponseTrait.php

200 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\Http\Response\Traits;
4
5 use Exception;
6 use Give\Framework\Http\Response\Exceptions\HttpResponseException;
7 use Give\Vendors\Symfony\Component\HttpFoundation\Cookie;
8 use Give\Vendors\Symfony\Component\HttpFoundation\HeaderBag;
9
10 /**
11 * @since 2.18.0
12 */
13 trait ResponseTrait
14 {
15 /**
16 * The original content of the response.
17 *
18 * @var mixed
19 */
20 public $original;
21
22 /**
23 * The exception that triggered the error response (if applicable).
24 *
25 * @var Exception|null
26 */
27 public $exception;
28
29 /**
30 * Get the status code for the response.
31 *
32 * @since 2.18.0
33 *
34 * @return int
35 */
36 public function status()
37 {
38 return $this->getStatusCode();
39 }
40
41 /**
42 * Get the content of the response.
43 *
44 * @since 2.18.0
45 *
46 * @return string
47 */
48 public function content()
49 {
50 return $this->getContent();
51 }
52
53 /**
54 * Get the original response content.
55 *
56 * @since 2.18.0
57 *
58 * @return mixed
59 */
60 public function getOriginalContent()
61 {
62 $original = $this->original;
63
64 return $original instanceof self ? $original->{__FUNCTION__}() : $original;
65 }
66
67 /**
68 * Set a header on the Response.
69 *
70 * @since 2.18.0
71 *
72 * @param string $key
73 * @param array|string $values
74 * @param bool $replace
75 * @return $this
76 */
77 public function header($key, $values, $replace = true)
78 {
79 $this->headers->set($key, $values, $replace);
80
81 return $this;
82 }
83
84 /**
85 * Add an array of headers to the response.
86 *
87 * @since 2.18.0
88 *
89 * @param HeaderBag|array $headers
90 * @return $this
91 */
92 public function withHeaders($headers)
93 {
94 if ($headers instanceof HeaderBag) {
95 $headers = $headers->all();
96 }
97
98 foreach ($headers as $key => $value) {
99 $this->headers->set($key, $value);
100 }
101
102 return $this;
103 }
104
105 /**
106 * Add a cookie to the response.
107 *
108 * @since 2.18.0
109 *
110 * @param Cookie|mixed $cookie
111 * @return $this
112 */
113 public function cookie($cookie)
114 {
115 return $this->withCookie(...func_get_args());
116 }
117
118 /**
119 * Add a cookie to the response.
120 *
121 * @since 2.18.0
122 *
123 * @param Cookie|mixed $cookie
124 * @return $this
125 */
126 //phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
127 public function withCookie($cookie)
128 {
129 if (is_string($cookie) && function_exists('cookie')) {
130 $cookie = cookie(...func_get_args());
131 }
132
133 $this->headers->setCookie($cookie);
134
135 return $this;
136 }
137
138 /**
139 * Expire a cookie when sending the response.
140 *
141 * @since 2.18.0
142 *
143 * @param Cookie|mixed $cookie
144 * @param string|null $path
145 * @param string|null $domain
146 * @return $this
147 */
148 public function withoutCookie($cookie, $path = null, $domain = null)
149 {
150 if (is_string($cookie) && function_exists('cookie')) {
151 $cookie = cookie($cookie, null, -2628000, $path, $domain);
152 }
153
154 $this->headers->setCookie($cookie);
155
156 return $this;
157 }
158
159 /**
160 * Get the callback of the response.
161 *
162 * @since 2.18.0
163 *
164 * @return string|null
165 */
166 public function getCallback()
167 {
168 return isset($this->callback) ? $this->callback : null;
169 }
170
171 /**
172 * Set the exception to attach to the response.
173 *
174 * @since 2.18.0
175 *
176 * @param Exception $e
177 * @return $this
178 */
179 public function withException(Exception $e)
180 {
181 $this->exception = $e;
182
183 return $this;
184 }
185
186 /**
187 * Throws the response in a HttpResponseException instance.
188 *
189 * @since 2.18.0
190 *
191 * @return void
192 *
193 * @throws HttpResponseException
194 */
195 public function throwResponse()
196 {
197 throw new HttpResponseException($this);
198 }
199 }
200