Skip to content

Commit

Permalink
Remove magic getters for protected properties of Client/Response.
Browse files Browse the repository at this point in the history
Add Response::getJson() and Response::getXml().
  • Loading branch information
ADmad committed Nov 20, 2018
1 parent 79cc421 commit 2f56d07
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 29 deletions.
63 changes: 50 additions & 13 deletions src/Http/Client/Response.php
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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}();
}

Expand All @@ -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;
Expand Down
30 changes: 14 additions & 16 deletions tests/TestCase/Http/Client/ResponseTest.php
Expand Up @@ -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',
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -179,12 +178,11 @@ public function testBodyXml()
</root>
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());
}

/**
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 2f56d07

Please sign in to comment.