@@ -15,48 +15,71 @@ final class ControllerResponse
15
15
private $ statusCode ;
16
16
private $ metaData ;
17
17
18
- public function __construct (int $ returnType , int $ statusCode , array $ headers , array $ data, array $ metaData )
18
+ public function __construct (int $ returnType , int $ statusCode , array $ data )
19
19
{
20
20
$ this ->returnType = $ returnType ;
21
21
$ this ->statusCode = $ statusCode ;
22
- $ this ->headers = $ headers ;
22
+ $ this ->headers = [] ;
23
23
$ this ->data = $ data ;
24
- $ this ->metaData = $ metaData ;
24
+ $ this ->metaData = [] ;
25
25
}
26
26
27
- public static function CUSTOM ( int $ statusCode , array $ data , array $ metaData , array $ headers ) : self
27
+ public function withHeaders ( array $ headers ) : self
28
28
{
29
- return new self (Router::CUSTOM , $ statusCode , $ headers , $ data , $ metaData );
29
+ $ new = clone $ this ;
30
+ $ new ->headers = $ headers ;
31
+ return $ new ;
30
32
}
31
33
32
- public static function EMPTY ( int $ statusCode , array $ data , array $ metaData, array $ headers ) : self
34
+ public function withMetaData ( array $ metaData ) : self
33
35
{
34
- return new self (Router::EMPTY , $ statusCode , $ headers , $ data , $ metaData );
36
+ $ new = clone $ this ;
37
+ $ new ->metaData = $ metaData ;
38
+ return $ new ;
35
39
}
36
40
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
38
42
{
39
- return new self (Router::HTML , $ statusCode , [], $ data , $ metaData );
43
+ return (new self (Router::CUSTOM , $ statusCode , $ data ))
44
+ ->withHeaders ($ headers )
45
+ ->withMetaData ($ metaData );
40
46
}
41
47
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
43
49
{
44
- return new self (Router::JSON , $ statusCode , [], $ data , $ metaData );
50
+ return (new self (Router::EMPTY , $ statusCode , $ data ))
51
+ ->withHeaders ($ headers )
52
+ ->withMetaData ($ metaData );
45
53
}
46
54
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
48
56
{
49
- return new self (Router::TEXT , $ statusCode , [], $ data , $ metaData );
57
+ return (new self (Router::HTML , $ statusCode , $ data ))
58
+ ->withHeaders ($ headers )
59
+ ->withMetaData ($ metaData );
50
60
}
51
61
52
- public static function XML (int $ statusCode , array $ xmlData , ?array $ metaData = []) : self
62
+ public static function JSON (int $ statusCode , array $ data , ?array $ headers = []) : self
53
63
{
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 );
55
77
}
56
78
57
79
public static function REDIRECT (int $ statusCode , string $ redirectUrl ) : self
58
80
{
59
- return new self (Router::REDIRECT , $ statusCode , [], [], ['uri ' => $ redirectUrl ]);
81
+ return (new self (Router::REDIRECT , $ statusCode , []))
82
+ ->withHeaders (['uri ' => $ redirectUrl ]);
60
83
}
61
84
62
85
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
72
95
'Cache-Control ' => 'must-revalidate ' ,
73
96
'Content-Length ' => (string ) $ stream ->getSize ()
74
97
];
75
- return new self (Router::DOWNLOAD , $ statusCode , $ headers , [], ['stream ' => $ stream ]);
98
+ return (new self (Router::DOWNLOAD , $ statusCode , []))
99
+ ->withHeaders ($ headers )
100
+ ->withMetaData (['stream ' => $ stream ]);
76
101
}
77
102
78
103
public function getReturnType () : int
0 commit comments