Skip to content

Commit 5a0d294

Browse files
committed
Make array encoder reusable
1 parent 5122067 commit 5a0d294

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/ResultEncoder/ResultEncoder.php

+27-13
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function encode($action_result, ServerRequestInterface $request, Response
5858

5959
return $response;
6060
}
61-
61+
6262
if ($action_result instanceof ViewResponseInterface) {
6363
return $action_result->render($response);
6464
}
@@ -71,17 +71,11 @@ public function encode($action_result, ServerRequestInterface $request, Response
7171

7272
// Respond with a status code
7373
} 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);
8175

8276
// Array
8377
} elseif (is_array($action_result)) {
84-
return $response->write(json_encode($action_result))->withStatus(200);
78+
return $this->encodeArray($action_result, $response);
8579

8680
// Exception
8781
} elseif ($action_result instanceof Throwable || $action_result instanceof Exception) {
@@ -104,26 +98,46 @@ protected function onNoEncoderApplied($action_result, ServerRequestInterface $re
10498
return $response;
10599
}
106100

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+
107114
/**
108115
* Encode and return status response.
109116
*
110117
* @param StatusResponse $action_result
111118
* @param ResponseInterface $response
112119
* @return ResponseInterface
113120
*/
114-
private function encodeStatus(StatusResponse $action_result, ResponseInterface $response)
121+
protected function encodeStatus(StatusResponse $action_result, ResponseInterface $response)
115122
{
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;
117130
}
118131

119132
/**
120133
* Encode and return exception.
121134
*
122135
* @param Throwable|Exception $exception
123136
* @param ResponseInterface $response
137+
* @param int $status
124138
* @return ResponseInterface
125139
*/
126-
private function encodeException($exception, ResponseInterface $response)
140+
protected function encodeException($exception, ResponseInterface $response, $status = 500)
127141
{
128142
$error = ['message' => $exception->getMessage(), 'type' => get_class($exception)];
129143

@@ -142,6 +156,6 @@ private function encodeException($exception, ResponseInterface $response)
142156
} while ($exception = $exception->getPrevious());
143157
}
144158

145-
return $response->write(json_encode($error))->withStatus(500);
159+
return $response->write(json_encode($error))->withStatus($status);
146160
}
147161
}

0 commit comments

Comments
 (0)