-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a46113c
commit 843a7a6
Showing
4 changed files
with
177 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Cknow\Money\Currencies; | ||
|
||
use ArrayIterator; | ||
use Money\Currencies; | ||
use Money\Currency; | ||
use Money\Exception\UnknownCurrencyException; | ||
use Traversable; | ||
|
||
class ISOCurrencies implements Currencies | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private static $currencies; | ||
|
||
public function contains(Currency $currency): bool | ||
{ | ||
return isset($this->getCurrencies()[$currency->getCode()]); | ||
} | ||
|
||
public function subunitFor(Currency $currency): int | ||
{ | ||
if (! $this->contains($currency)) { | ||
throw new UnknownCurrencyException('Cannot find ISO currency '.$currency->getCode()); | ||
} | ||
|
||
return $this->getCurrencies()[$currency->getCode()]['minorUnit']; | ||
} | ||
|
||
/** | ||
* Returns the numeric code for a currency. | ||
* | ||
* @throws UnknownCurrencyException If currency is not available in the current context. | ||
*/ | ||
public function numericCodeFor(Currency $currency): int | ||
{ | ||
if (! $this->contains($currency)) { | ||
throw new UnknownCurrencyException('Cannot find ISO currency '.$currency->getCode()); | ||
} | ||
|
||
return $this->getCurrencies()[$currency->getCode()]['numericCode']; | ||
} | ||
|
||
#[\ReturnTypeWillChange] | ||
public function getIterator(): Traversable | ||
{ | ||
return new ArrayIterator( | ||
array_map( | ||
static function ($code) { | ||
return new Currency($code); | ||
}, | ||
array_keys($this->getCurrencies()) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Returns a map of known currencies indexed by code. | ||
* | ||
* @return array | ||
*/ | ||
public function getCurrencies() | ||
{ | ||
if (self::$currencies === null) { | ||
self::$currencies = $this->loadCurrencies(); | ||
} | ||
|
||
return self::$currencies; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function loadCurrencies() | ||
{ | ||
$file = config('money.isoCurrenciesPath'); | ||
|
||
if (file_exists($file)) { | ||
return require $file; | ||
} | ||
|
||
throw new \RuntimeException('Failed to load currency ISO codes.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Cknow\Money\Tests\Currencies; | ||
|
||
use Cknow\Money\Currencies\ISOCurrencies; | ||
use Cknow\Money\Tests\TestCase; | ||
|
||
class ISOCurrenciesTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function testContains() | ||
{ | ||
$currencies = new ISOCurrencies(); | ||
static::assertTrue($currencies->contains(new \Money\Currency('EUR'))); | ||
static::assertTrue($currencies->contains(new \Money\Currency('USD'))); | ||
} | ||
|
||
public function testSubunitFor() | ||
{ | ||
$currencies = new ISOCurrencies(); | ||
static::assertEquals(2, $currencies->subunitFor(new \Money\Currency('EUR'))); | ||
static::assertEquals(2, $currencies->subunitFor(new \Money\Currency('USD'))); | ||
} | ||
|
||
public function testSubunitForInvalidCurrency() | ||
{ | ||
$this->expectException(\Money\Exception\UnknownCurrencyException::class); | ||
$this->expectExceptionMessage('Cannot find ISO currency XYZ'); | ||
|
||
$currencies = new ISOCurrencies(); | ||
$currencies->subunitFor(new \Money\Currency('XYZ')); | ||
} | ||
|
||
public function testNumericCodeFor() | ||
{ | ||
$currencies = new ISOCurrencies(); | ||
static::assertEquals(978, $currencies->numericCodeFor(new \Money\Currency('EUR'))); | ||
static::assertEquals(840, $currencies->numericCodeFor(new \Money\Currency('USD'))); | ||
} | ||
|
||
public function testNumericCodeForInvalidCurrency() | ||
{ | ||
$this->expectException(\Money\Exception\UnknownCurrencyException::class); | ||
$this->expectExceptionMessage('Cannot find ISO currency XYZ'); | ||
|
||
$currencies = new ISOCurrencies(); | ||
$currencies->numericCodeFor(new \Money\Currency('XYZ')); | ||
} | ||
|
||
public function testLoadCurrencies() | ||
{ | ||
$currencies = new ISOCurrencies(); | ||
static::assertContainsOnlyInstancesOf(\Money\Currency::class, $currencies->getIterator()); | ||
} | ||
|
||
public function testGetCurrencies() | ||
{ | ||
$currencies = new ISOCurrencies(); | ||
|
||
static::assertIsArray($currencies->getCurrencies()); | ||
static::assertArrayHasKey('EUR', $currencies->getCurrencies()); | ||
static::assertArrayHasKey('USD', $currencies->getCurrencies()); | ||
static::assertArrayNotHasKey('XYZ', $currencies->getCurrencies()); | ||
} | ||
|
||
public function testInvalidConfigCurrenciesPath() | ||
{ | ||
config(['money.isoCurrenciesPath' => null]); | ||
|
||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessage('Failed to load currency ISO codes.'); | ||
|
||
$currencies = new ISOCurrencies(); | ||
|
||
$reflection = new \ReflectionObject($currencies); | ||
$property = $reflection->getProperty('currencies'); | ||
$property->setAccessible(true); | ||
$property->setValue($currencies, null); | ||
|
||
$currencies->getCurrencies(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters