]> git.mxchange.org Git - friendica.git/blob - src/Module/Response.php
Add missing copyright text
[friendica.git] / src / Module / Response.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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         /**
44          * {@inheritDoc}
45          */
46         public function setHeader(?string $header = null, ?string $key = null): void
47         {
48                 if (!isset($header) && !empty($key)) {
49                         unset($this->headers[$key]);
50                 }
51
52                 if (isset($header)) {
53                         if (empty($key)) {
54                                 $this->headers[] = $header;
55                         } else {
56                                 $this->headers[$key] = $header;
57                         }
58                 }
59         }
60
61         /**
62          * {@inheritDoc}
63          */
64         public function addContent($content): void
65         {
66                 $this->content .= $content;
67         }
68
69         /**
70          * {@inheritDoc}
71          */
72         public function getHeaders(): array
73         {
74                 return $this->headers;
75         }
76
77         /**
78          * {@inheritDoc}
79          */
80         public function getContent()
81         {
82                 return $this->content;
83         }
84
85         /**
86          * {@inheritDoc}
87          */
88         public function setType(string $type, ?string $content_type = null): void
89         {
90                 if (!in_array($type, static::ALLOWED_TYPES)) {
91                         throw new InternalServerErrorException('wrong type');
92                 }
93
94                 switch ($type) {
95                         case static::TYPE_JSON:
96                                 $content_type = $content_type ?? 'application/json';
97                                 break;
98                         case static::TYPE_XML:
99                                 $content_type = $content_type ?? 'text/xml';
100                                 break;
101                         case static::TYPE_RSS:
102                                 $content_type = $content_type ?? 'application/rss+xml';
103                                 break;
104                         case static::TYPE_ATOM:
105                                 $content_type = $content_type ?? 'application/atom+xml';
106                                 break;
107                 }
108
109                 $this->setHeader($content_type, 'Content-type');
110
111                 $this->type = $type;
112         }
113
114         /**
115          * {@inheritDoc}
116          */
117         public function getType(): string
118         {
119                 return $this->type;
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         public function generate(): ResponseInterface
126         {
127                 // Setting the response type as an X-header for direct usage
128                 $this->headers[static::X_HEADER] = $this->type;
129
130                 return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content);
131         }
132 }