Skip to content

Commit

Permalink
currencies iterator should iterate over currency object rather than c…
Browse files Browse the repository at this point in the history
…urrency string (#262)
  • Loading branch information
frederikbosch committed Aug 3, 2016
1 parent ea6ed11 commit c3020b0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
12 changes: 6 additions & 6 deletions doc/Currencies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ As it's name says, the ISO currencies implementation provides all available ISO4
use Money\Currency;
$currencies = new ISOCurrencies();
foreach ($currencies as $currencyCode) {
echo $currencyCode; // prints an available currency code within the repository
foreach ($currencies as $currency) {
echo $currency->getCode(); // prints an available currency code within the repository
}
$currencies->contains(new Currency('USD')); // returns boolean whether USD is available in this repository
Expand All @@ -41,8 +41,8 @@ The Bitcoin currencies provides a single currency: the Bitcoin. It uses XBT as i
use Money\Currency;
$currencies = new BitcoinCurrencies();
foreach ($currencies as $currencyCode) {
echo $currencyCode; // prints XBT
foreach ($currencies as $currency) {
echo $currency->getCode(); // prints XBT
}
$currencies->contains(new Currency('XBT')); // returns boolean whether XBT is available in this repository (true)
Expand All @@ -67,8 +67,8 @@ This formatter collects multiple currencies.
new ISOCurrencies()
]);
foreach ($currencies as $currencyCode) {
echo $currencyCode; // prints XBT or any ISO currency code
foreach ($currencies as $currency) {
echo $currency->getCode(); // prints XBT or any ISO currency code
}
$currencies->contains(new Currency('XBT')); // returns boolean whether XBT is available in this repository (true)
Expand Down
4 changes: 2 additions & 2 deletions spec/Currencies/AggregateCurrenciesSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ function it_contains_currencies(Currencies $isoCurrencies, Currencies $otherCurr

function it_can_be_iterated(Currencies $isoCurrencies, Currencies $otherCurrencies)
{
$isoCurrencies->getIterator()->willReturn(new \ArrayIterator(['EUR']));
$otherCurrencies->getIterator()->willReturn(new \ArrayIterator(['USD']));
$isoCurrencies->getIterator()->willReturn(new \ArrayIterator([new Currency('EUR')]));
$otherCurrencies->getIterator()->willReturn(new \ArrayIterator([new Currency('USD')]));

$this->getIterator()->shouldReturnAnInstanceOf(\Traversable::class);
$this->getIterator()->shouldHaveCurrency('EUR');
Expand Down
2 changes: 1 addition & 1 deletion spec/Currencies/CachedCurrenciesSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function it_can_be_iterated(
$pool->save($item)->shouldBeCalled();

$pool->getItem('currency|availability|EUR')->willReturn($item);
$currencies->getIterator()->willReturn(new \ArrayIterator(['EUR']));
$currencies->getIterator()->willReturn(new \ArrayIterator([new Currency('EUR')]));

$this->getIterator()->shouldReturnAnInstanceOf(\Traversable::class);
$this->getIterator()->shouldHaveCurrency('EUR');
Expand Down
7 changes: 5 additions & 2 deletions spec/Currencies/HaveCurrencyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace spec\Money\Currencies;

use Money\Currency;

trait HaveCurrencyTrait
{
/**
Expand All @@ -11,8 +13,9 @@ public function getMatchers()
{
return [
'haveCurrency' => function ($subject, $value) {
foreach ($subject as $code) {
if ($code === $value) {
/** @var Currency $currency */
foreach ($subject as $currency) {
if ($currency->getCode() === $value) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Currencies/BitcoinCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function subunitFor(Currency $currency)
*/
public function getIterator()
{
return new \ArrayIterator([self::CODE]);
return new \ArrayIterator([new Currency(self::CODE)]);
}
}
4 changes: 2 additions & 2 deletions src/Currencies/CachedCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public function getIterator()
{
return new \CallbackFilterIterator(
$this->currencies->getIterator(),
function ($currencyCode) {
$item = $this->pool->getItem('currency|availability|'.$currencyCode);
function (Currency $currency) {
$item = $this->pool->getItem('currency|availability|'.$currency->getCode());
$item->set(true);

if ($item instanceof TaggableItemInterface) {
Expand Down
9 changes: 8 additions & 1 deletion src/Currencies/ISOCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ public function getIterator()
self::$currencies = $this->loadCurrencies();
}

return new \ArrayIterator(array_keys(self::$currencies));
return new \ArrayIterator(
array_map(
function ($code) {
return new Currency($code);
},
array_keys(self::$currencies)
)
);
}

/**
Expand Down

0 comments on commit c3020b0

Please sign in to comment.