Skip to content

Commit

Permalink
rework currency
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 3, 2024
1 parent 0fb19a0 commit e66193f
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 18 deletions.
6 changes: 3 additions & 3 deletions config/bazar.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
*/

'currencies' => [
'default' => strtolower(env('BAZAR_CURRENCY', 'usd')),
'default' => strtolower(env('BAZAR_CURRENCY', 'USD')),
'available' => [
'usd' => 'USD',
'eur' => 'EUR',
'USD',
'EUR',
],
],

Expand Down
2 changes: 1 addition & 1 deletion database/factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function configure(): static
{
return $this->afterMaking(static function (Product $product): void {
$product->setRelation('metaData', $product->metaData()->makeMany([
['key' => 'price_'.Bazar::getCurrency(), 'value' => mt_rand(10, 100)],
['key' => 'price_'.strtolower(Bazar::getCurrency()), 'value' => mt_rand(10, 100)],
]));
})->afterCreating(static function (Product $product): void {
$product->metaData->each(static function (Meta $meta) use ($product) {
Expand Down
2 changes: 1 addition & 1 deletion database/factories/VariantFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function configure(): static
{
return $this->afterMaking(static function (Variant $variant): void {
$variant->setRelation('metaData', $variant->metaData()->makeMany([
['key' => 'price_'.Bazar::getCurrency(), 'value' => mt_rand(10, 100)],
['key' => 'price_'.strtolower(Bazar::getCurrency()), 'value' => mt_rand(10, 100)],
]));
})->afterCreating(static function (Variant $variant): void {
$variant->metaData->each(static function (Meta $meta) use ($variant) {
Expand Down
9 changes: 3 additions & 6 deletions src/Bazar.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ public static function getCurrencies(): array
*/
public static function getCurrency(): string
{
return static::$currency ?: Config::get('bazar.currencies.default', 'usd');
return strtoupper(static::$currency ?: Config::get('bazar.currencies.default', 'USD'));
}

/**
* Set the currency in use.
*
*
* @throws \Cone\Bazar\Exceptions\InvalidCurrencyException
*/
public static function setCurrency(string $currency): void
{
$currency = strtolower($currency);
$currency = strtoupper($currency);

if (! array_key_exists($currency, static::getCurrencies())) {
if (! in_array($currency, static::getCurrencies())) {
throw new InvalidCurrencyException("The [{$currency}] currency is not registered.");
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getPrice(?string $currency = null): ?float
{
$currency ??= Bazar::getCurrency();

$key = sprintf('price_%s', $currency);
$key = sprintf('price_%s', strtolower($currency));

$meta = $this->prices->firstWhere('key', $key);

Expand Down
4 changes: 2 additions & 2 deletions src/Traits/InteractsWithItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function currency(): Attribute
{
return new Attribute(
get: static function (?string $value = null): string {
return $value ?: Bazar::getCurrency();
return strtoupper($value ?: Bazar::getCurrency());
}
);
}
Expand Down Expand Up @@ -199,7 +199,7 @@ public function needsShipping(): bool
*/
public function getCurrency(): string
{
return $this->currency;
return strtoupper($this->currency);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/BazarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ class BazarTest extends TestCase
public function test_bazar_has_currencies(): void
{
$this->assertSame(
$this->app['config']->get('bazar.currencies.available'),
['USD', 'EUR'],
Bazar::getCurrencies()
);
}

public function test_bazar_can_get_currency(): void
{
$this->assertSame($this->app['config']->get('bazar.currencies.default'), Bazar::getCurrency());
$this->assertSame('USD', Bazar::getCurrency());
}

public function test_bazar_can_set_currency(): void
{
Bazar::setCurrency('eur');
$this->assertSame('eur', Bazar::getCurrency());
Bazar::setCurrency('EUR');
$this->assertSame('EUR', Bazar::getCurrency());

$this->expectException(InvalidCurrencyException::class);
Bazar::setCurrency('fake');
Expand Down

0 comments on commit e66193f

Please sign in to comment.