Skip to content

Commit b708bc9

Browse files
committed
Properly encode PHP scalar values
1 parent 5a0d294 commit b708bc9

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/ResultEncoder/ResultEncoder.php

+17
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public function encode($action_result, ServerRequestInterface $request, Response
7777
} elseif (is_array($action_result)) {
7878
return $this->encodeArray($action_result, $response);
7979

80+
// Scalar
81+
} elseif (is_scalar($action_result)) {
82+
return $this->encodeScalar($action_result, $response);
83+
8084
// Exception
8185
} elseif ($action_result instanceof Throwable || $action_result instanceof Exception) {
8286
return $this->encodeException($action_result, $response);
@@ -111,6 +115,19 @@ protected function encodeArray(array $action_result, ResponseInterface $response
111115
return $response->write(json_encode($action_result))->withStatus($status);
112116
}
113117

118+
/**
119+
* Encode scalar value, with status 200.
120+
*
121+
* @param array $action_result
122+
* @param ResponseInterface $response
123+
* @param int $status
124+
* @return ResponseInterface
125+
*/
126+
protected function encodeScalar(array $action_result, ResponseInterface $response, $status = 200)
127+
{
128+
return $response->write(json_encode($action_result))->withStatus($status);
129+
}
130+
114131
/**
115132
* Encode and return status response.
116133
*

0 commit comments

Comments
 (0)