Skip to content

Commit

Permalink
Merge pull request #4 from djmarland/master
Browse files Browse the repository at this point in the history
Handle asking for the Base currency and fix error in Readme
  • Loading branch information
fadion authored Jul 27, 2016
2 parents a8a47ed + f3dd5a2 commit 790ced4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ print $rates->GBP;
The last option is to return the response as a `Result` class. This allows access to the full set of properties returned from the feed.

```php
$result = (new Exchange())->symbols(Currency::USD, Currency::GBP)->getAsObject();
$result = (new Exchange())->symbols(Currency::USD, Currency::GBP)->getResult();

$date = $result->getDate(); // The date the data is from
$rates = $result->getRates(); // Array of rates as above
Expand Down
8 changes: 8 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public function getRates()
*/
public function getRate($code)
{
// the result won't have the base code in it,
// because that would always be 1. But to make
// dynamic code easier this prevents null if
// the base code is asked for
if ($code == $this->getBase()) {
return 1.0;
}

if (isset($this->rates[$code])) {
return $this->rates[$code];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ExchangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function testResponseAsResult()
{
$response = m::mock('StdClass');
$response->shouldReceive('getBody')->once()->andReturn(json_encode([
'base' => 'USD',
'base' => 'EUR',
'date' => '2016-01-02',
'rates' => [
'GBP' => 1.01,
Expand All @@ -132,7 +132,7 @@ public function testResponseAsResult()
$result = $exchange->getResult();
$this->assertInstanceOf('\Fadion\Fixerio\Result', $result);

$this->assertEquals(1.02, $result->getRate(Currency::USD));
$this->assertEquals(1.01, $result->getRate(Currency::GBP));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function testGetters()

$this->assertEquals(1.23, $result->getRate(Currency::USD));
$this->assertEquals(1.01, $result->getRate(Currency::GBP));

// Check that asking for the Base will return 1
$this->assertEquals(1, $result->getRate(Currency::EUR));

// Null if currency not in result
$this->assertNull($result->getRate(Currency::HKD));
$this->assertNull($result->getRate('invalid'));
}
Expand Down

0 comments on commit 790ced4

Please sign in to comment.