Skip to content

Commit aa14d94

Browse files
committed
Added ability to cast response to str.
1 parent c10408b commit aa14d94

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: src/ResponseArrayTrait.php

+16
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,22 @@ public function __invoke($key = null)
271271
return $this->key($key);
272272
}
273273

274+
/**
275+
* Converts response to string on current key.
276+
*
277+
* @return string
278+
*/
279+
public function __toString()
280+
{
281+
$value = $this->get();
282+
283+
if (is_array($value) || is_object($value)) {
284+
return json_encode($value);
285+
}
286+
287+
return (string) $value;
288+
}
289+
274290
/**
275291
* Constructs full key.
276292
*

Diff for: tests/BitcoindResponseTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ public function setUp()
2020
$this->response = $this->response->withHeader('X-Test', 'test');
2121
}
2222

23+
/**
24+
* Test casting response to string.
25+
*
26+
* @return void
27+
*/
28+
public function testResponseToString()
29+
{
30+
$response = $this->response;
31+
$this->assertSame((string) $response('difficulty'), '1');
32+
$this->assertSame((string) $response('confirmations'), '449162');
33+
$this->assertSame(
34+
(string) $response('tx'),
35+
json_encode(self::$getBlockResponse['tx'])
36+
);
37+
}
38+
2339
/**
2440
* Test response with result.
2541
*

0 commit comments

Comments
 (0)