From 40b7cb833ce53b783a26235ef74d579752131023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Thu, 24 Dec 2015 21:28:26 +0100 Subject: [PATCH 1/2] Add formatter interface --- src/Formatter.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/Formatter.php diff --git a/src/Formatter.php b/src/Formatter.php new file mode 100644 index 0000000..d04d2c3 --- /dev/null +++ b/src/Formatter.php @@ -0,0 +1,32 @@ + + */ +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); +} From ec277bbea7675855fd146d6210a15118d2ae8742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Thu, 24 Dec 2015 21:34:59 +0100 Subject: [PATCH 2/2] Add simple text formatter implementation --- spec/Formatter/SimpleFormatterSpec.php | 40 ++++++++++++++++++++++++ src/Formatter/SimpleFormatter.php | 42 ++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 spec/Formatter/SimpleFormatterSpec.php create mode 100644 src/Formatter/SimpleFormatter.php diff --git a/spec/Formatter/SimpleFormatterSpec.php b/spec/Formatter/SimpleFormatterSpec.php new file mode 100644 index 0000000..ef70eb8 --- /dev/null +++ b/spec/Formatter/SimpleFormatterSpec.php @@ -0,0 +1,40 @@ +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'); + } +} diff --git a/src/Formatter/SimpleFormatter.php b/src/Formatter/SimpleFormatter.php new file mode 100644 index 0000000..b1fcabd --- /dev/null +++ b/src/Formatter/SimpleFormatter.php @@ -0,0 +1,42 @@ + + * @author Márk Sági-Kazár + */ +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() + ); + } +}