Merge pull request #14039 from annando/widget-features
[friendica.git/.git] / src / Module / Response.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\Capabilities\ICanCreateResponses;
25 use Friendica\Network\HTTPException\InternalServerErrorException;
26 use Psr\Http\Message\ResponseInterface;
27
28 class Response implements ICanCreateResponses
29 {
30         /**
31          * @var string[]
32          */
33         protected $headers = [];
34         /**
35          * @var string
36          */
37         protected $content = '';
38         /**
39          * @var string
40          */
41         protected $type = self::TYPE_HTML;
42
43         protected $status = 200;
44
45         protected $reason = null;
46
47         /**
48          * {@inheritDoc}
49          */
50         public function setHeader(?string $header = null, ?string $key = null): void
51         {
52                 if (!isset($header) && !empty($key)) {
53                         unset($this->headers[$key]);
54                 }
55
56                 if (isset($header)) {
57                         if (empty($key)) {
58                                 $this->headers[] = $header;
59                         } else {
60                                 $this->headers[$key] = $header;
61                         }
62                 }
63         }
64
65         /**
66          * {@inheritDoc}
67          */
68         public function addContent($content): void
69         {
70                 $this->content .= $content;
71         }
72
73         /**
74          * {@inheritDoc}
75          */
76         public function getHeaders(): array
77         {
78                 return $this->headers;
79         }
80
81         /**
82          * {@inheritDoc}
83          */
84         public function getContent()
85         {
86                 return $this->content;
87         }
88
89         /**
90          * {@inheritDoc}
91          */
92         public function setType(string $type = Response::TYPE_HTML, ?string $content_type = null): void
93         {
94                 if (!in_array($type, static::ALLOWED_TYPES)) {
95                         throw new InternalServerErrorException('wrong type');
96                 }
97
98                 switch ($type) {
99                         case static::TYPE_HTML:
100                                 $content_type = $content_type ?? 'text/html; charset=utf-8';
101                                 break;
102                         case static::TYPE_JSON:
103                                 $content_type = $content_type ?? 'application/json';
104                                 break;
105                         case static::TYPE_XML:
106                                 $content_type = $content_type ?? 'text/xml';
107                                 break;
108                         case static::TYPE_RSS:
109                                 $content_type = $content_type ?? 'application/rss+xml';
110                                 break;
111                         case static::TYPE_ATOM:
112                                 $content_type = $content_type ?? 'application/atom+xml';
113                                 break;
114                 }
115
116                 $this->setHeader($content_type, 'Content-type');
117
118                 $this->type = $type;
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         public function setStatus(int $status = 200, ?string $reason = null): void
125         {
126                 $this->status = $status;
127                 $this->reason = $reason;
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         public function getType(): string
134         {
135                 return $this->type;
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         public function generate(): ResponseInterface
142         {
143                 // Setting the response type as an X-header for direct usage
144                 $this->headers[static::X_HEADER] = $this->type;
145
146                 return new \GuzzleHttp\Psr7\Response($this->status, $this->headers, $this->content, '1.1', $this->reason);
147         }
148 }