@@ -58,7 +58,7 @@ public function encode($action_result, ServerRequestInterface $request, Response
58
58
59
59
return $ response ;
60
60
}
61
-
61
+
62
62
if ($ action_result instanceof ViewResponseInterface) {
63
63
return $ action_result ->render ($ response );
64
64
}
@@ -71,17 +71,11 @@ public function encode($action_result, ServerRequestInterface $request, Response
71
71
72
72
// Respond with a status code
73
73
} elseif ($ action_result instanceof StatusResponse) {
74
- $ response = $ this ->encodeStatus ($ action_result , $ response );
75
-
76
- if ($ action_result ->getHttpCode () >= 400 ) {
77
- $ response = $ response ->write (json_encode (['message ' => $ action_result ->getMessage ()]));
78
- }
79
-
80
- return $ response ;
74
+ return $ this ->encodeStatus ($ action_result , $ response );
81
75
82
76
// Array
83
77
} elseif (is_array ($ action_result )) {
84
- return $ response -> write ( json_encode ( $ action_result))-> withStatus ( 200 );
78
+ return $ this -> encodeArray ( $ action_result, $ response );
85
79
86
80
// Exception
87
81
} elseif ($ action_result instanceof Throwable || $ action_result instanceof Exception) {
@@ -104,26 +98,46 @@ protected function onNoEncoderApplied($action_result, ServerRequestInterface $re
104
98
return $ response ;
105
99
}
106
100
101
+ /**
102
+ * Encode regular array response, with status 200.
103
+ *
104
+ * @param array $action_result
105
+ * @param ResponseInterface $response
106
+ * @param int $status
107
+ * @return ResponseInterface
108
+ */
109
+ protected function encodeArray (array $ action_result , ResponseInterface $ response , $ status = 200 )
110
+ {
111
+ return $ response ->write (json_encode ($ action_result ))->withStatus ($ status );
112
+ }
113
+
107
114
/**
108
115
* Encode and return status response.
109
116
*
110
117
* @param StatusResponse $action_result
111
118
* @param ResponseInterface $response
112
119
* @return ResponseInterface
113
120
*/
114
- private function encodeStatus (StatusResponse $ action_result , ResponseInterface $ response )
121
+ protected function encodeStatus (StatusResponse $ action_result , ResponseInterface $ response )
115
122
{
116
- return $ response ->withStatus ($ action_result ->getHttpCode (), $ action_result ->getMessage ());
123
+ $ response = $ response ->withStatus ($ action_result ->getHttpCode (), $ action_result ->getMessage ());
124
+
125
+ if ($ action_result ->getHttpCode () >= 400 ) {
126
+ $ response = $ response ->write (json_encode (['message ' => $ action_result ->getMessage ()]));
127
+ }
128
+
129
+ return $ response ;
117
130
}
118
131
119
132
/**
120
133
* Encode and return exception.
121
134
*
122
135
* @param Throwable|Exception $exception
123
136
* @param ResponseInterface $response
137
+ * @param int $status
124
138
* @return ResponseInterface
125
139
*/
126
- private function encodeException ($ exception , ResponseInterface $ response )
140
+ protected function encodeException ($ exception , ResponseInterface $ response, $ status = 500 )
127
141
{
128
142
$ error = ['message ' => $ exception ->getMessage (), 'type ' => get_class ($ exception )];
129
143
@@ -142,6 +156,6 @@ private function encodeException($exception, ResponseInterface $response)
142
156
} while ($ exception = $ exception ->getPrevious ());
143
157
}
144
158
145
- return $ response ->write (json_encode ($ error ))->withStatus (500 );
159
+ return $ response ->write (json_encode ($ error ))->withStatus ($ status );
146
160
}
147
161
}
0 commit comments