diff --git a/src/ResponseArrayTrait.php b/src/ResponseArrayTrait.php index 30eec90..266a9d0 100644 --- a/src/ResponseArrayTrait.php +++ b/src/ResponseArrayTrait.php @@ -271,6 +271,22 @@ public function __invoke($key = null) return $this->key($key); } + /** + * Converts response to string on current key. + * + * @return string + */ + public function __toString() + { + $value = $this->get(); + + if (is_array($value) || is_object($value)) { + return json_encode($value); + } + + return (string) $value; + } + /** * Constructs full key. * diff --git a/tests/BitcoindResponseTest.php b/tests/BitcoindResponseTest.php index 93d4686..783636a 100644 --- a/tests/BitcoindResponseTest.php +++ b/tests/BitcoindResponseTest.php @@ -20,6 +20,22 @@ public function setUp() $this->response = $this->response->withHeader('X-Test', 'test'); } + /** + * Test casting response to string. + * + * @return void + */ + public function testResponseToString() + { + $response = $this->response; + $this->assertSame((string) $response('difficulty'), '1'); + $this->assertSame((string) $response('confirmations'), '449162'); + $this->assertSame( + (string) $response('tx'), + json_encode(self::$getBlockResponse['tx']) + ); + } + /** * Test response with result. *