]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Response.php
Merge pull request #13238 from annando/issue-13221
[friendica.git] / src / Module / Response.php
index 9bf9912360aa8bf811eb4cd2db35a6c44fccc62a..d38b330bb3bd57fa1a6c337354d05041b50b18bc 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Module;
 
@@ -19,7 +38,11 @@ class Response implements ICanCreateResponses
        /**
         * @var string
         */
-       protected $type = ICanCreateResponses::TYPE_HTML;
+       protected $type = self::TYPE_HTML;
+
+       protected $status = 200;
+
+       protected $reason = null;
 
        /**
         * {@inheritDoc}
@@ -68,25 +91,42 @@ class Response implements ICanCreateResponses
         */
        public function setType(string $type, ?string $content_type = null): void
        {
-               if (!in_array($type, ICanCreateResponses::ALLOWED_TYPES)) {
+               if (!in_array($type, static::ALLOWED_TYPES)) {
                        throw new InternalServerErrorException('wrong type');
                }
 
                switch ($type) {
+                       case static::TYPE_HTML:
+                               $content_type = $content_type ?? 'text/html; charset=utf-8';
+                               break;
                        case static::TYPE_JSON:
                                $content_type = $content_type ?? 'application/json';
                                break;
                        case static::TYPE_XML:
                                $content_type = $content_type ?? 'text/xml';
                                break;
+                       case static::TYPE_RSS:
+                               $content_type = $content_type ?? 'application/rss+xml';
+                               break;
+                       case static::TYPE_ATOM:
+                               $content_type = $content_type ?? 'application/atom+xml';
+                               break;
                }
 
-
                $this->setHeader($content_type, 'Content-type');
 
                $this->type = $type;
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       public function setStatus(int $status = 200, ?string $reason = null): void
+       {
+               $this->status = $status;
+               $this->reason = $reason;
+       }
+
        /**
         * {@inheritDoc}
         */
@@ -100,19 +140,9 @@ class Response implements ICanCreateResponses
         */
        public function generate(): ResponseInterface
        {
-               $headers = [];
-
-               foreach ($this->headers as $key => $header) {
-                       if (empty($key)) {
-                               $headers[] = $header;
-                       } else {
-                               $headers[] = "$key: $header";
-                       }
-               }
-
                // Setting the response type as an X-header for direct usage
-               $headers['X-RESPONSE-TYPE'] = $this->type;
+               $this->headers[static::X_HEADER] = $this->type;
 
-               return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content);
+               return new \GuzzleHttp\Psr7\Response($this->status, $this->headers, $this->content, '1.1', $this->reason);
        }
 }