]> git.mxchange.org Git - friendica.git/blob - src/Module/Response.php
Introduce `Response` for Modules to create a testable way for module responses
[friendica.git] / src / Module / Response.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\Capabilities\ICanReadAndWriteToResponds;
6 use Friendica\Capabilities\IRespondToRequests;
7 use Friendica\Network\HTTPException\InternalServerErrorException;
8
9 class Response implements ICanReadAndWriteToResponds
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 = IRespondToRequests::TYPE_CONTENT;
23
24         /**
25          * {@inheritDoc}
26          */
27         public function addHeader(string $key, string $value)
28         {
29                 $this->headers[$key][] = $value;
30         }
31
32         /**
33          * {@inheritDoc}
34          */
35         public function addContent(string $content)
36         {
37                 $this->content .= $content;
38         }
39
40         /**
41          * {@inheritDoc}
42          */
43         public function getHeaders(): array
44         {
45                 return $this->headers;
46         }
47
48         /**
49          * {@inheritDoc}
50          */
51         public function getContent(): string
52         {
53                 return $this->content;
54         }
55
56         /**
57          * {@inheritDoc}
58          */
59         public function setType(string $type)
60         {
61                 if (!in_array($type, IRespondToRequests::ALLOWED_TYPES)) {
62                         throw new InternalServerErrorException('wrong type');
63                 }
64
65                 $this->type = $type;
66         }
67
68         /**
69          * {@inheritDoc}
70          */
71         public function getTyp(): string
72         {
73                 return $this->type;
74         }
75 }