Skip to content

Commit

Permalink
Fixed RestAPI (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
emulgeator authored Dec 14, 2017
1 parent d7dea8b commit 0206528
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Controller/RestApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ abstract class RestApiController extends HttpController
/** @var string */
private $requestBody;

/** @var array|null */
private $requestData;
/** @var array */
private $requestData = [];

public function __construct(IRequest $request, IResponse $response)
{
Expand All @@ -45,22 +45,22 @@ public function __construct(IRequest $request, IResponse $response)
public function run($action)
{
try {
$this->response->setContentType(MimeType::JSON);

$this->requestBody = file_get_contents('php://input');

if (!empty($this->requestBody)) {
$this->requestData = json_decode($this->requestBody, true);
$requestData = json_decode($this->requestBody, true);

if (false === $this->requestData) {
if ($requestData === null && !empty(json_last_error())) {
throw new RestException(
RestException::CODE_REQUEST_ERROR,
'Failed to decode the request as valid JSON. JSON decode error: ' . json_last_error_msg()
);
}
$this->requestData = (array)$requestData;
}

// Initialize the content type to JSON
$this->response->setContentType(MimeType::JSON);

$actionPrefix = $this->getActionPrefix();
$methodName = $actionPrefix . $action;
if (!method_exists($this, $methodName)) {
Expand Down Expand Up @@ -194,7 +194,7 @@ protected function getRequestBody(): string
return $this->requestBody;
}

protected function getRequestData(): ?array
protected function getRequestData(): array
{
return $this->requestData;
}
Expand Down

0 comments on commit 0206528

Please sign in to comment.