From 2f56d07b8f5a830c0328abb0213890e7cca5a81b Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 20 Nov 2018 17:09:16 +0530 Subject: [PATCH] Remove magic getters for protected properties of Client/Response. Add Response::getJson() and Response::getXml(). --- src/Http/Client/Response.php | 63 ++++++++++++++++----- tests/TestCase/Http/Client/ResponseTest.php | 30 +++++----- 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/src/Http/Client/Response.php b/src/Http/Client/Response.php index b0d8a3cbd5e..35bfad6ead2 100644 --- a/src/Http/Client/Response.php +++ b/src/Http/Client/Response.php @@ -44,14 +44,6 @@ * $response->getHeaders(); * ``` * - * You can also get at the headers using object access. When getting - * headers with object access, you have to use case-sensitive header - * names: - * - * ``` - * $val = $response->headers['Content-Type']; - * ``` - * * ### Get the response body * * You can access the response body stream using: @@ -60,11 +52,10 @@ * $content = $response->getBody(); * ``` * - * You can also use object access to get the string version - * of the response body: + * You can get the body string using: * * ``` - * $content = $response->body; + * $content = $response->getBody()->getContents(); * ``` * * If your response body is in XML or JSON you can use @@ -74,9 +65,9 @@ * * ``` * // Get as xml - * $content = $response->xml + * $content = $response->getXml() * // Get as json - * $content = $response->json + * $content = $response->getJson() * ``` * * If the response cannot be decoded, null will be returned. @@ -142,6 +133,20 @@ class Response extends Message implements ResponseInterface 'headers' => '_getHeaders', ]; + /** + * Map of deprecated magic properties. + * + * @var array + * @internal + */ + protected $_deprecatedMagicProperties = [ + 'cookies' => 'getCookies()', + 'body' => 'getBody()->getContents()', + 'json' => 'getJson()', + 'xml' => 'getXml()', + 'headers' => 'getHeaders()', + ]; + /** * Constructor * @@ -563,6 +568,16 @@ public function body($parser = null) return $stream->getContents(); } + /** + * Get the response body as JSON decoded data. + * + * @return array|null + */ + public function getJson() + { + return $this->_getJson(); + } + /** * Get the response body as JSON decoded data. * @@ -577,6 +592,16 @@ protected function _getJson() return $this->_json = json_decode($this->_getBody(), true); } + /** + * Get the response body as XML decoded data. + * + * @return null|\SimpleXMLElement + */ + public function getXml() + { + return $this->_getXml(); + } + /** * Get the response body as XML decoded data. * @@ -638,6 +663,12 @@ public function __get($name) } $key = $this->_exposedProperties[$name]; if (substr($key, 0, 4) === '_get') { + deprecationWarning(sprintf( + 'Response::%s is deprecated. Use Response::%s instead.', + $name, + $this->_deprecatedMagicProperties[$name] + )); + return $this->{$key}(); } @@ -664,6 +695,12 @@ public function __isset($name) } $key = $this->_exposedProperties[$name]; if (substr($key, 0, 4) === '_get') { + deprecationWarning(sprintf( + 'Response::%s is deprecated. Use Response::%s instead.', + $name, + $this->_deprecatedMagicProperties[$name] + )); + $val = $this->{$key}(); return $val !== null; diff --git a/tests/TestCase/Http/Client/ResponseTest.php b/tests/TestCase/Http/Client/ResponseTest.php index 5ff5afbd139..91f8248014c 100644 --- a/tests/TestCase/Http/Client/ResponseTest.php +++ b/tests/TestCase/Http/Client/ResponseTest.php @@ -77,9 +77,8 @@ public function testHeaderParsing() $this->assertEquals( 'text/html;charset="UTF-8"', - $response->headers['Content-Type'] + $response->getHeaderLine('Content-Type') ); - $this->assertTrue(isset($response->headers)); $headers = [ 'HTTP/1.0 200', @@ -109,8 +108,9 @@ public function testBody() $result = $response->body('json_decode'); $this->assertEquals($data['property'], $result->property); - $this->assertEquals($encoded, $response->body); - $this->assertTrue(isset($response->body)); + $stream = $response->getBody(); + $stream->rewind(); + $this->assertEquals($encoded, $stream->getContents()); } /** @@ -125,28 +125,27 @@ public function testBodyJson() ]; $encoded = json_encode($data); $response = new Response([], $encoded); - $this->assertTrue(isset($response->json)); - $this->assertEquals($data['property'], $response->json['property']); + $this->assertEquals($data['property'], $response->getJson()['property']); $data = ''; $response = new Response([], $data); - $this->assertNull($response->json); + $this->assertNull($response->getJson()); $data = json_encode([]); $response = new Response([], $data); - $this->assertInternalType('array', $response->json); + $this->assertInternalType('array', $response->getJson()); $data = json_encode(null); $response = new Response([], $data); - $this->assertNull($response->json); + $this->assertNull($response->getJson()); $data = json_encode(false); $response = new Response([], $data); - $this->assertFalse($response->json); + $this->assertFalse($response->getJson()); $data = json_encode(''); $response = new Response([], $data); - $this->assertSame('', $response->json); + $this->assertSame('', $response->getJson()); } /** @@ -162,7 +161,7 @@ public function testBodyJsonPsr7() $encoded = json_encode($data); $response = new Response([], ''); $response->getBody()->write($encoded); - $this->assertEquals($data, $response->json); + $this->assertEquals($data, $response->getJson()); } /** @@ -179,12 +178,11 @@ public function testBodyXml() XML; $response = new Response([], $data); - $this->assertTrue(isset($response->xml)); - $this->assertEquals('Test', (string)$response->xml->test); + $this->assertEquals('Test', (string)$response->getXml()->test); $data = ''; $response = new Response([], $data); - $this->assertFalse(isset($response->xml)); + $this->assertNull($response->getXml()); } /** @@ -501,6 +499,6 @@ public function testAutoDecodeGzipBody() ]; $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA='); $response = new Response($headers, $body); - $this->assertEquals('Hello world!', $response->body); + $this->assertEquals('Hello world!', $response->getBody()->getContents()); } }