Skip to content
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

## Unreleased

### Added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep alphabetic order in the changelog.


- The FullHttpMessageFormatter was added

### Fixed

- #41: Response builder broke header value


## 1.2.0 - 2016-03-29

### Added
Expand Down
72 changes: 72 additions & 0 deletions src/Formatter/FullHttpMessageFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Http\Message\Formatter;

use Http\Message\Formatter;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* A formatter that prints the complete HTTP message.
*
* @author Tobias Nyholm <[email protected]>
*/
class FullHttpMessageFormatter implements Formatter
{
/**
* The maximum length of the body.
*
* @var int
*/
private $maxBodyLength;

/**
* @param int $maxBodyLength
*/
public function __construct($maxBodyLength = 1000)
{
$this->maxBodyLength = $maxBodyLength;
}

/**
* {@inheritdoc}
*/
public function formatRequest(RequestInterface $request)
{
$message = sprintf(
"%s %s HTTP/%s\n",
$request->getMethod(),
$request->getRequestTarget(),
$request->getProtocolVersion()
);

foreach ($request->getHeaders() as $name => $values) {
$message .= $name.': '.implode(', ', $values)."\n";
}

$message .= "\n".mb_substr($request->getBody()->__toString(), 0, $this->maxBodyLength);

return $message;
}

/**
* {@inheritdoc}
*/
public function formatResponse(ResponseInterface $response)
{
$message = sprintf(
"HTTP/%s %s %s\n",
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->getReasonPhrase()
);

foreach ($response->getHeaders() as $name => $values) {
$message .= $name.': '.implode(', ', $values)."\n";
}

$message .= "\n".mb_substr($response->getBody()->__toString(), 0, $this->maxBodyLength);

return $message;
}
}