Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
30 / 30 |
Responses | |
100.00% |
1 / 1 |
|
100.00% |
11 / 11 |
11 | |
100.00% |
30 / 30 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
string | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
redirect | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
json | |
100.00% |
1 / 1 |
1 | |
100.00% |
7 / 7 |
|||
streamFile | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
download | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
downloadFile | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
response | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
stringResponse | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
downloadResponse | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
buildResponse | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
<?php | |
namespace PHPixie\HTTP; | |
class Responses | |
{ | |
protected $builder; | |
public function __construct($builder) | |
{ | |
$this->builder = $builder; | |
} | |
public function string($string) | |
{ | |
return $this->stringResponse($string); | |
} | |
public function redirect($url, $statusCode = 302) | |
{ | |
return $this->stringResponse( | |
'', | |
array( | |
'Location' => $url | |
), | |
$statusCode | |
); | |
} | |
public function json($data) | |
{ | |
$string = json_encode($data); | |
return $this->stringResponse( | |
$string, | |
array( | |
'Cache-Control' => 'no-cache, must-revalidate', | |
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', | |
'Content-type' => 'application/json' | |
) | |
); | |
} | |
public function streamFile($file) | |
{ | |
$body = $this->builder->messages()->stream($file); | |
return $this->response($body); | |
} | |
public function download($fileName, $contentType, $contents) | |
{ | |
$body = $this->builder->messages()->stringStream($contents); | |
return $this->downloadResponse($fileName, $contentType, $body); | |
} | |
public function downloadFile($fileName, $contentType, $file) | |
{ | |
$body = $this->builder->messages()->stream($file); | |
return $this->downloadResponse($fileName, $contentType, $body); | |
} | |
public function response($body, $headerArray = array(), $statusCode = 200, $reasonPhrase = null) | |
{ | |
$headers = $this->builder->editableHeaders($headerArray); | |
return $this->buildResponse($headers, $body, $statusCode, $reasonPhrase); | |
} | |
protected function stringResponse($string, $headers = array(), $statusCode = 200) | |
{ | |
$body = $this->builder->messages()->stringStream($string); | |
return $this->response($body, $headers, $statusCode); | |
} | |
protected function downloadResponse($fileName, $contentType, $body) | |
{ | |
$headers = array( | |
'Content-Type' => $contentType, | |
'Content-Disposition' => 'attachment; filename="'.$fileName.'"' | |
); | |
return $this->response($body, $headers); | |
} | |
protected function buildResponse($headers, $body, $statusCode = 200, $reasonPhrase = null) | |
{ | |
$messages = $this->builder->messages(); | |
return new Responses\Response($messages, $headers, $body, $statusCode, $reasonPhrase); | |
} | |
} |