Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit 4d9a9d5

Browse files
committed
ControllerResponse refactored to be extended out of the class itself
1 parent 92c6b9d commit 4d9a9d5

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

src/ControllerResponse.php

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,71 @@ final class ControllerResponse
1515
private $statusCode;
1616
private $metaData;
1717

18-
public function __construct(int $returnType, int $statusCode, array $headers, array $data, array $metaData)
18+
public function __construct(int $returnType, int $statusCode, array $data)
1919
{
2020
$this->returnType = $returnType;
2121
$this->statusCode = $statusCode;
22-
$this->headers = $headers;
22+
$this->headers = [];
2323
$this->data = $data;
24-
$this->metaData = $metaData;
24+
$this->metaData = [];
2525
}
2626

27-
public static function CUSTOM(int $statusCode, array $data, array $metaData, array $headers) : self
27+
public function withHeaders(array $headers) : self
2828
{
29-
return new self(Router::CUSTOM, $statusCode, $headers, $data, $metaData);
29+
$new = clone $this;
30+
$new->headers = $headers;
31+
return $new;
3032
}
3133

32-
public static function EMPTY(int $statusCode, array $data, array $metaData, array $headers) : self
34+
public function withMetaData(array $metaData) : self
3335
{
34-
return new self(Router::EMPTY, $statusCode, $headers, $data, $metaData);
36+
$new = clone $this;
37+
$new->metaData = $metaData;
38+
return $new;
3539
}
3640

37-
public static function HTML(int $statusCode, array $data, ?array $metaData = []) : self
41+
public static function CUSTOM(int $statusCode, array $data, ?array $metaData=[], ?array $headers =[]) : self
3842
{
39-
return new self(Router::HTML, $statusCode, [], $data, $metaData);
43+
return (new self(Router::CUSTOM, $statusCode, $data))
44+
->withHeaders($headers)
45+
->withMetaData($metaData);
4046
}
4147

42-
public static function JSON(int $statusCode, array $data, ?array $metaData = []) : self
48+
public static function EMPTY(int $statusCode, array $data, ?array $metaData=[], ?array $headers=[]) : self
4349
{
44-
return new self(Router::JSON, $statusCode, [], $data, $metaData);
50+
return (new self(Router::EMPTY, $statusCode, $data))
51+
->withHeaders($headers)
52+
->withMetaData($metaData);
4553
}
4654

47-
public static function TEXT(int $statusCode, array $data, ?array $metaData = []) : self
55+
public static function HTML(int $statusCode, array $data, ?array $metaData = [], ?array $headers=[]) : self
4856
{
49-
return new self(Router::TEXT, $statusCode, [], $data, $metaData);
57+
return (new self(Router::HTML, $statusCode, $data))
58+
->withHeaders($headers)
59+
->withMetaData($metaData);
5060
}
5161

52-
public static function XML(int $statusCode, array $xmlData, ?array $metaData = []) : self
62+
public static function JSON(int $statusCode, array $data, ?array $headers=[]) : self
5363
{
54-
return new self(Router::XML, $statusCode, [], $xmlData, $metaData);
64+
return (new self(Router::JSON, $statusCode, $data))
65+
->withHeaders($headers);
66+
}
67+
68+
public static function TEXT(int $statusCode, array $data) : self
69+
{
70+
return new self(Router::TEXT, $statusCode, $data);
71+
}
72+
73+
public static function XML(int $statusCode, array $xmlData, ?array $headers=[]) : self
74+
{
75+
return (new self(Router::XML, $statusCode, $xmlData ))
76+
->withHeaders($headers);
5577
}
5678

5779
public static function REDIRECT(int $statusCode, string $redirectUrl) : self
5880
{
59-
return new self(Router::REDIRECT, $statusCode, [], [], ['uri' => $redirectUrl]);
81+
return (new self(Router::REDIRECT, $statusCode, []))
82+
->withHeaders(['uri' => $redirectUrl]);
6083
}
6184

6285
public static function DOWNLOAD(int $statusCode, string $filePath, ?string $fileName = null) : self
@@ -72,7 +95,9 @@ public static function DOWNLOAD(int $statusCode, string $filePath, ?string $file
7295
'Cache-Control' => 'must-revalidate',
7396
'Content-Length' => (string) $stream->getSize()
7497
];
75-
return new self(Router::DOWNLOAD, $statusCode, $headers, [], ['stream' => $stream]);
98+
return (new self(Router::DOWNLOAD, $statusCode, []))
99+
->withHeaders($headers)
100+
->withMetaData(['stream' => $stream]);
76101
}
77102

78103
public function getReturnType() : int

0 commit comments

Comments
 (0)