Skip to content

Commit

Permalink
Cast as string the output if content type is text. Also add a helper …
Browse files Browse the repository at this point in the history
…to echo output.
  • Loading branch information
daniel-aranda committed Apr 5, 2015
1 parent d02d502 commit dc4cd38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/RESTful/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,23 @@ public function getResponse(){
return $this->response;
}

public function echoResponse($value)
{
$this->setResponse($value);
$this->render();
}

public function render(){
$this->validateHeaders();

if( $this->isJSON() ){
$this->response = json_encode($this->response);
}

if( $this->isText() && !is_string($this->response) ){
$this->response = var_export($this->response, true);
}

$this->trigger(self::OUTPUT_EVENT, [$this]);

}
Expand Down
15 changes: 15 additions & 0 deletions tests/RESTful/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,19 @@ public function testSetInvalidResponseType() {

}

public function testEchoResponse()
{

$output = null;
$this->response->addEventHandler(Response::OUTPUT_EVENT, function(Response $response) use (&$output){
$output = $response->getResponse();
});

$this->response->setResponseType(Response::TEXT);
$this->response->echoResponse('Hello World');

$this->assertSame('Hello World', $output);

}

}

0 comments on commit dc4cd38

Please sign in to comment.