Skip to content

Commit 2f56d07

Browse files
committed
Remove magic getters for protected properties of Client/Response.
Add Response::getJson() and Response::getXml().
1 parent 79cc421 commit 2f56d07

File tree

2 files changed

+64
-29
lines changed

2 files changed

+64
-29
lines changed

src/Http/Client/Response.php

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@
4444
* $response->getHeaders();
4545
* ```
4646
*
47-
* You can also get at the headers using object access. When getting
48-
* headers with object access, you have to use case-sensitive header
49-
* names:
50-
*
51-
* ```
52-
* $val = $response->headers['Content-Type'];
53-
* ```
54-
*
5547
* ### Get the response body
5648
*
5749
* You can access the response body stream using:
@@ -60,11 +52,10 @@
6052
* $content = $response->getBody();
6153
* ```
6254
*
63-
* You can also use object access to get the string version
64-
* of the response body:
55+
* You can get the body string using:
6556
*
6657
* ```
67-
* $content = $response->body;
58+
* $content = $response->getBody()->getContents();
6859
* ```
6960
*
7061
* If your response body is in XML or JSON you can use
@@ -74,9 +65,9 @@
7465
*
7566
* ```
7667
* // Get as xml
77-
* $content = $response->xml
68+
* $content = $response->getXml()
7869
* // Get as json
79-
* $content = $response->json
70+
* $content = $response->getJson()
8071
* ```
8172
*
8273
* If the response cannot be decoded, null will be returned.
@@ -142,6 +133,20 @@ class Response extends Message implements ResponseInterface
142133
'headers' => '_getHeaders',
143134
];
144135

136+
/**
137+
* Map of deprecated magic properties.
138+
*
139+
* @var array
140+
* @internal
141+
*/
142+
protected $_deprecatedMagicProperties = [
143+
'cookies' => 'getCookies()',
144+
'body' => 'getBody()->getContents()',
145+
'json' => 'getJson()',
146+
'xml' => 'getXml()',
147+
'headers' => 'getHeaders()',
148+
];
149+
145150
/**
146151
* Constructor
147152
*
@@ -563,6 +568,16 @@ public function body($parser = null)
563568
return $stream->getContents();
564569
}
565570

571+
/**
572+
* Get the response body as JSON decoded data.
573+
*
574+
* @return array|null
575+
*/
576+
public function getJson()
577+
{
578+
return $this->_getJson();
579+
}
580+
566581
/**
567582
* Get the response body as JSON decoded data.
568583
*
@@ -577,6 +592,16 @@ protected function _getJson()
577592
return $this->_json = json_decode($this->_getBody(), true);
578593
}
579594

595+
/**
596+
* Get the response body as XML decoded data.
597+
*
598+
* @return null|\SimpleXMLElement
599+
*/
600+
public function getXml()
601+
{
602+
return $this->_getXml();
603+
}
604+
580605
/**
581606
* Get the response body as XML decoded data.
582607
*
@@ -638,6 +663,12 @@ public function __get($name)
638663
}
639664
$key = $this->_exposedProperties[$name];
640665
if (substr($key, 0, 4) === '_get') {
666+
deprecationWarning(sprintf(
667+
'Response::%s is deprecated. Use Response::%s instead.',
668+
$name,
669+
$this->_deprecatedMagicProperties[$name]
670+
));
671+
641672
return $this->{$key}();
642673
}
643674

@@ -664,6 +695,12 @@ public function __isset($name)
664695
}
665696
$key = $this->_exposedProperties[$name];
666697
if (substr($key, 0, 4) === '_get') {
698+
deprecationWarning(sprintf(
699+
'Response::%s is deprecated. Use Response::%s instead.',
700+
$name,
701+
$this->_deprecatedMagicProperties[$name]
702+
));
703+
667704
$val = $this->{$key}();
668705

669706
return $val !== null;

tests/TestCase/Http/Client/ResponseTest.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ public function testHeaderParsing()
7777

7878
$this->assertEquals(
7979
'text/html;charset="UTF-8"',
80-
$response->headers['Content-Type']
80+
$response->getHeaderLine('Content-Type')
8181
);
82-
$this->assertTrue(isset($response->headers));
8382

8483
$headers = [
8584
'HTTP/1.0 200',
@@ -109,8 +108,9 @@ public function testBody()
109108

110109
$result = $response->body('json_decode');
111110
$this->assertEquals($data['property'], $result->property);
112-
$this->assertEquals($encoded, $response->body);
113-
$this->assertTrue(isset($response->body));
111+
$stream = $response->getBody();
112+
$stream->rewind();
113+
$this->assertEquals($encoded, $stream->getContents());
114114
}
115115

116116
/**
@@ -125,28 +125,27 @@ public function testBodyJson()
125125
];
126126
$encoded = json_encode($data);
127127
$response = new Response([], $encoded);
128-
$this->assertTrue(isset($response->json));
129-
$this->assertEquals($data['property'], $response->json['property']);
128+
$this->assertEquals($data['property'], $response->getJson()['property']);
130129

131130
$data = '';
132131
$response = new Response([], $data);
133-
$this->assertNull($response->json);
132+
$this->assertNull($response->getJson());
134133

135134
$data = json_encode([]);
136135
$response = new Response([], $data);
137-
$this->assertInternalType('array', $response->json);
136+
$this->assertInternalType('array', $response->getJson());
138137

139138
$data = json_encode(null);
140139
$response = new Response([], $data);
141-
$this->assertNull($response->json);
140+
$this->assertNull($response->getJson());
142141

143142
$data = json_encode(false);
144143
$response = new Response([], $data);
145-
$this->assertFalse($response->json);
144+
$this->assertFalse($response->getJson());
146145

147146
$data = json_encode('');
148147
$response = new Response([], $data);
149-
$this->assertSame('', $response->json);
148+
$this->assertSame('', $response->getJson());
150149
}
151150

152151
/**
@@ -162,7 +161,7 @@ public function testBodyJsonPsr7()
162161
$encoded = json_encode($data);
163162
$response = new Response([], '');
164163
$response->getBody()->write($encoded);
165-
$this->assertEquals($data, $response->json);
164+
$this->assertEquals($data, $response->getJson());
166165
}
167166

168167
/**
@@ -179,12 +178,11 @@ public function testBodyXml()
179178
</root>
180179
XML;
181180
$response = new Response([], $data);
182-
$this->assertTrue(isset($response->xml));
183-
$this->assertEquals('Test', (string)$response->xml->test);
181+
$this->assertEquals('Test', (string)$response->getXml()->test);
184182

185183
$data = '';
186184
$response = new Response([], $data);
187-
$this->assertFalse(isset($response->xml));
185+
$this->assertNull($response->getXml());
188186
}
189187

190188
/**
@@ -501,6 +499,6 @@ public function testAutoDecodeGzipBody()
501499
];
502500
$body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
503501
$response = new Response($headers, $body);
504-
$this->assertEquals('Hello world!', $response->body);
502+
$this->assertEquals('Hello world!', $response->getBody()->getContents());
505503
}
506504
}

0 commit comments

Comments
 (0)