Skip to content

Formatter #17

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
Dec 25, 2015
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
40 changes: 40 additions & 0 deletions spec/Formatter/SimpleFormatterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace spec\Http\Message\Formatter;

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

class SimpleFormatterSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Http\Message\Formatter\SimpleFormatter');
}

function it_is_a_formatter()
{
$this->shouldImplement('Http\Message\Formatter');
}

function it_formats_the_request(RequestInterface $request, UriInterface $uri)
{
$uri->__toString()->willReturn('http://foo.com/bar');
$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn($uri);
$request->getProtocolVersion()->willReturn('1.1');

$this->formatRequest($request)->shouldReturn('GET http://foo.com/bar 1.1');
}

function it_formats_the_response(ResponseInterface $response)
{
$response->getReasonPhrase()->willReturn('OK');
$response->getProtocolVersion()->willReturn('1.1');
$response->getStatusCode()->willReturn('200');

$this->formatResponse($response)->shouldReturn('200 OK 1.1');
}
}
32 changes: 32 additions & 0 deletions src/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Http\Message;

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

/**
* Formats a request and/or a response as a string.
*
* @author Márk Sági-Kazár <[email protected]>
*/
interface Formatter
{
/**
* Formats a request.
*
* @param RequestInterface $request
*
* @return string
*/
public function formatRequest(RequestInterface $request);

/**
* Formats a response.
*
* @param ResponseInterface $response
*
* @return string
*/
public function formatResponse(ResponseInterface $response);
}
42 changes: 42 additions & 0 deletions src/Formatter/SimpleFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Http\Message\Formatter;

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

/**
* Normalize a request or a response into a string or an array.
*
* @author Joel Wurtz <[email protected]>
* @author Márk Sági-Kazár <[email protected]>
*/
class SimpleFormatter implements Formatter
{
/**
* {@inheritdoc}
*/
public function formatRequest(RequestInterface $request)
{
return sprintf(
'%s %s %s',
$request->getMethod(),
$request->getUri()->__toString(),
$request->getProtocolVersion()
);
}

/**
* {@inheritdoc}
*/
public function formatResponse(ResponseInterface $response)
{
return sprintf(
'%s %s %s',
$response->getStatusCode(),
$response->getReasonPhrase(),
$response->getProtocolVersion()
);
}
}