Skip to content

Commit

Permalink
fixing returned response
Browse files Browse the repository at this point in the history
  • Loading branch information
malfasih committed Jul 4, 2018
1 parent 8d076c4 commit 682d20b
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/JsonParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

namespace Malfasih\Slim;

use Psr\Http\Message\ResponseInterface;

class JsonParser {

/**
* @var string
* Contains response data
* @var object
*/
private $rawData;
private $buffer;


/**
Expand Down Expand Up @@ -82,11 +85,6 @@ class JsonParser {
private $cors_headers = "GET, POST, PUT, DELETE, PATCH, OPTIONS";


public function __construct($response) {
$this->rawData = $response;
}


/**
* Allow user to change CORS settings.
* @var string
Expand Down Expand Up @@ -125,24 +123,23 @@ public function setCORS($domain = '', $methods = '', $headers = '') {
* Return the JSON parsed of given params
* @return string
*/
public function print($http_status_code = 200, $message = '') {
$arrayBody = ['http_code' => $http_status_code, 'message' => $message];

$this->rawData->withStatus($http_status_code);
$this->rawData->withHeader('Content-Type', $this->contentType);
public function print(ResponseInterface $response, $http_status_code = 200, $message = '') {
$arrayBody = ['response_code' => $http_status_code, 'message' => $message];

$buffer = $response->withStatus($http_status_code)
->withHeader('Content-Type', $this->contentType);
if ($this->cors_status === true) {
$this->rawData->withHeader('Access-Control-Allow-Origin', $this->cors_domain);
$this->rawData->withHeader('Access-Control-Allow-Headers', $this->cors_headers);
$this->rawData->withHeader('Access-Control-Allow-Methods', $this->cors_methods);
$buffer = $buffer->withHeader('Access-Control-Allow-Origin', $this->cors_domain)
->withHeader('Access-Control-Allow-Headers', $this->cors_headers)
->withHeader('Access-Control-Allow-Methods', $this->cors_methods);
}

if ($this->options !== false) {
$this->rawData->write(json_encode($arrayBody, $this->options));
$buffer->write(json_encode($arrayBody, $this->options));
} else {
$this->rawData->write(json_encode($arrayBody));
$buffer->write(json_encode($arrayBody));
}

return $buffer;
}

}
Expand Down

0 comments on commit 682d20b

Please sign in to comment.