]> git.mxchange.org Git - friendica.git/blob - src/Module/Response.php
Replace IRespondToRequests with PSR-7 ResponseInterface
[friendica.git] / src / Module / Response.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\Capabilities\ICanCreateResponses;
6 use Friendica\Network\HTTPException\InternalServerErrorException;
7 use Psr\Http\Message\ResponseInterface;
8
9 class Response implements ICanCreateResponses
10 {
11         /**
12          * @var string[]
13          */
14         protected $headers = [];
15         /**
16          * @var string
17          */
18         protected $content = '';
19         /**
20          * @var string
21          */
22         protected $type = ICanCreateResponses::TYPE_HTML;
23
24         /**
25          * {@inheritDoc}
26          */
27         public function setHeader(?string $header = null, ?string $key = null): void
28         {
29                 if (!isset($header) && !empty($key)) {
30                         unset($this->headers[$key]);
31                 }
32
33                 if (isset($header)) {
34                         if (empty($key)) {
35                                 $this->headers[] = $header;
36                         } else {
37                                 $this->headers[$key] = $header;
38                         }
39                 }
40         }
41
42         /**
43          * {@inheritDoc}
44          */
45         public function addContent($content): void
46         {
47                 $this->content .= $content;
48         }
49
50         /**
51          * {@inheritDoc}
52          */
53         public function getHeaders(): array
54         {
55                 return $this->headers;
56         }
57
58         /**
59          * {@inheritDoc}
60          */
61         public function getContent()
62         {
63                 return $this->content;
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         public function setType(string $type, ?string $content_type = null): void
70         {
71                 if (!in_array($type, ICanCreateResponses::ALLOWED_TYPES)) {
72                         throw new InternalServerErrorException('wrong type');
73                 }
74
75                 switch ($type) {
76                         case static::TYPE_JSON:
77                                 $content_type = $content_type ?? 'application/json';
78                                 break;
79                         case static::TYPE_XML:
80                                 $content_type = $content_type ?? 'text/xml';
81                                 break;
82                 }
83
84
85                 $this->setHeader($content_type, 'Content-type');
86
87                 $this->type = $type;
88         }
89
90         /**
91          * {@inheritDoc}
92          */
93         public function getType(): string
94         {
95                 return $this->type;
96         }
97
98         /**
99          * {@inheritDoc}
100          */
101         public function generate(): ResponseInterface
102         {
103                 $headers = [];
104
105                 foreach ($this->headers as $key => $header) {
106                         if (empty($key)) {
107                                 $headers[] = $header;
108                         } else {
109                                 $headers[] = "$key: $header";
110                         }
111                 }
112
113                 // Setting the response type as an X-header for direct usage
114                 $headers['X-RESPONSE-TYPE'] = $this->type;
115
116                 return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content);
117         }
118 }