Skip to content

ResponseBuilder class. #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions spec/Builder/ResponseBuilderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace spec\Http\Message\Builder;

use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;

class ResponseBuilderSpec extends ObjectBehavior
{
function it_is_initializable(ResponseInterface $response)
{
$this->beConstructedWith($response);
$this->shouldHaveType('Http\Message\Builder\ResponseBuilder');
}

function it_reads_headers_from_array(ResponseInterface $response)
{
$response->withStatus(200, 'OK')->willReturn($response);
$response->withProtocolVersion('1.1')->willReturn($response);
$response->hasHeader('Content-type')->willReturn(false);
$response->withHeader('Content-type', 'text/html')->willReturn($response);
$this->beConstructedWith($response);
$this->setHeadersFromArray(['HTTP/1.1 200 OK', 'Content-type: text/html']);
}

function it_reads_headers_from_string(ResponseInterface $response)
{
$response->withStatus(200, 'OK')->willReturn($response);
$response->withProtocolVersion('1.1')->willReturn($response);
$response->hasHeader('Content-type')->willReturn(false);
$response->withHeader('Content-type', 'text/html')->willReturn($response);
$this->beConstructedWith($response);
$this->setHeadersFromString("HTTP/1.1 200 OK\r\nContent-type: text/html\r\n");
}
}
116 changes: 116 additions & 0 deletions src/Builder/ResponseBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Http\Message\Builder;

use Psr\Http\Message\ResponseInterface;

/**
* Fills response object with values.
*/
class ResponseBuilder
{
/**
* The response to be built.
*
* @var ResponseInterface
*/
protected $response;

/**
* Create builder for the given response.
*
* @param ResponseInterface $response
*/
public function __construct(ResponseInterface $response)
{
$this->response = $response;
}

/**
* Return response.
*
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}

/**
* Add headers represented by an array of header lines.
*
* @param string[] $headers Response headers as array of header lines.
*
* @return $this
*
* @throws \UnexpectedValueException For invalid header values.
* @throws \InvalidArgumentException For invalid status code arguments.
*/
public function setHeadersFromArray(array $headers)
{
$statusLine = trim(array_shift($headers));
$parts = explode(' ', $statusLine, 3);
if (count($parts) < 2 || substr(strtolower($parts[0]), 0, 5) !== 'http/') {
throw new \UnexpectedValueException(
sprintf('"%s" is not a valid HTTP status line', $statusLine)
);
}

$reasonPhrase = count($parts) > 2 ? $parts[2] : '';
$this->response = $this->response
->withStatus((int) $parts[1], $reasonPhrase)
->withProtocolVersion(substr($parts[0], 5));

foreach ($headers as $headerLine) {
$headerLine = trim($headerLine);
if ('' === $headerLine) {
continue;
}

$parts = explode(':', $headerLine, 2);
if (count($parts) !== 2) {
throw new \UnexpectedValueException(
sprintf('"%s" is not a valid HTTP header line', $headerLine)
);
}
$name = trim(urldecode($parts[0]));
$value = trim(urldecode($parts[1]));
if ($this->response->hasHeader($name)) {
$this->response = $this->response->withAddedHeader($name, $value);
} else {
$this->response = $this->response->withHeader($name, $value);
}
}

return $this;
}

/**
* Add headers represented by a single string.
*
* @param string $headers Response headers as single string.
*
* @return $this
*
* @throws \InvalidArgumentException if $headers is not a string on object with __toString()
* @throws \UnexpectedValueException For invalid header values.
*/
public function setHeadersFromString($headers)
{
if (!(is_string($headers)
|| (is_object($headers) && method_exists($headers, '__toString')))
) {
throw new \InvalidArgumentException(
sprintf(
'%s expects parameter 1 to be a string, %s given',
__METHOD__,
is_object($headers) ? get_class($headers) : gettype($headers)
)
);
}

$this->setHeadersFromArray(explode("\r\n", $headers));

return $this;
}
}