Skip to content

Commit 3053fde

Browse files
committed
Merge pull request #17 from php-http/formatter
Formatter, fixes #14
2 parents f3a36b2 + ec277bb commit 3053fde

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Formatter;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\UriInterface;
8+
use PhpSpec\ObjectBehavior;
9+
10+
class SimpleFormatterSpec extends ObjectBehavior
11+
{
12+
function it_is_initializable()
13+
{
14+
$this->shouldHaveType('Http\Message\Formatter\SimpleFormatter');
15+
}
16+
17+
function it_is_a_formatter()
18+
{
19+
$this->shouldImplement('Http\Message\Formatter');
20+
}
21+
22+
function it_formats_the_request(RequestInterface $request, UriInterface $uri)
23+
{
24+
$uri->__toString()->willReturn('http://foo.com/bar');
25+
$request->getMethod()->willReturn('GET');
26+
$request->getUri()->willReturn($uri);
27+
$request->getProtocolVersion()->willReturn('1.1');
28+
29+
$this->formatRequest($request)->shouldReturn('GET http://foo.com/bar 1.1');
30+
}
31+
32+
function it_formats_the_response(ResponseInterface $response)
33+
{
34+
$response->getReasonPhrase()->willReturn('OK');
35+
$response->getProtocolVersion()->willReturn('1.1');
36+
$response->getStatusCode()->willReturn('200');
37+
38+
$this->formatResponse($response)->shouldReturn('200 OK 1.1');
39+
}
40+
}

src/Formatter.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Http\Message;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
/**
9+
* Formats a request and/or a response as a string.
10+
*
11+
* @author Márk Sági-Kazár <[email protected]>
12+
*/
13+
interface Formatter
14+
{
15+
/**
16+
* Formats a request.
17+
*
18+
* @param RequestInterface $request
19+
*
20+
* @return string
21+
*/
22+
public function formatRequest(RequestInterface $request);
23+
24+
/**
25+
* Formats a response.
26+
*
27+
* @param ResponseInterface $response
28+
*
29+
* @return string
30+
*/
31+
public function formatResponse(ResponseInterface $response);
32+
}

src/Formatter/SimpleFormatter.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Http\Message\Formatter;
4+
5+
use Http\Message\Formatter;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
/**
10+
* Normalize a request or a response into a string or an array.
11+
*
12+
* @author Joel Wurtz <[email protected]>
13+
* @author Márk Sági-Kazár <[email protected]>
14+
*/
15+
class SimpleFormatter implements Formatter
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function formatRequest(RequestInterface $request)
21+
{
22+
return sprintf(
23+
'%s %s %s',
24+
$request->getMethod(),
25+
$request->getUri()->__toString(),
26+
$request->getProtocolVersion()
27+
);
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function formatResponse(ResponseInterface $response)
34+
{
35+
return sprintf(
36+
'%s %s %s',
37+
$response->getStatusCode(),
38+
$response->getReasonPhrase(),
39+
$response->getProtocolVersion()
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)