Skip to content

Commit ec277bb

Browse files
committed
Add simple text formatter implementation
1 parent 40b7cb8 commit ec277bb

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-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/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)