Skip to content

Commit

Permalink
Added ability to cast response to str.
Browse files Browse the repository at this point in the history
  • Loading branch information
denpamusic committed Sep 18, 2018
1 parent c10408b commit aa14d94
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/ResponseArrayTrait.php
Original file line number Diff line number Diff line change
@@ -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.
*
16 changes: 16 additions & 0 deletions tests/BitcoindResponseTest.php
Original file line number Diff line number Diff line change
@@ -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.
*

0 comments on commit aa14d94

Please sign in to comment.