From e66193f59259912053641544badf42d956ca1f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Wed, 3 Jan 2024 14:23:56 +0100 Subject: [PATCH] rework currency --- config/bazar.php | 6 +++--- database/factories/ProductFactory.php | 2 +- database/factories/VariantFactory.php | 2 +- src/Bazar.php | 9 +++------ src/Traits/HasPrices.php | 2 +- src/Traits/InteractsWithItems.php | 4 ++-- tests/BazarTest.php | 8 ++++---- 7 files changed, 15 insertions(+), 18 deletions(-) diff --git a/config/bazar.php b/config/bazar.php index aab255d2..c14599c3 100644 --- a/config/bazar.php +++ b/config/bazar.php @@ -14,10 +14,10 @@ */ 'currencies' => [ - 'default' => strtolower(env('BAZAR_CURRENCY', 'usd')), + 'default' => strtolower(env('BAZAR_CURRENCY', 'USD')), 'available' => [ - 'usd' => 'USD', - 'eur' => 'EUR', + 'USD', + 'EUR', ], ], diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php index 900bf905..111f69ea 100644 --- a/database/factories/ProductFactory.php +++ b/database/factories/ProductFactory.php @@ -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) { diff --git a/database/factories/VariantFactory.php b/database/factories/VariantFactory.php index 9d3a9742..da461109 100644 --- a/database/factories/VariantFactory.php +++ b/database/factories/VariantFactory.php @@ -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) { diff --git a/src/Bazar.php b/src/Bazar.php index 7b68cb02..bf070aad 100644 --- a/src/Bazar.php +++ b/src/Bazar.php @@ -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."); } diff --git a/src/Traits/HasPrices.php b/src/Traits/HasPrices.php index 4a5e08e4..adc5c700 100644 --- a/src/Traits/HasPrices.php +++ b/src/Traits/HasPrices.php @@ -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); diff --git a/src/Traits/InteractsWithItems.php b/src/Traits/InteractsWithItems.php index 93808d22..75e0a818 100644 --- a/src/Traits/InteractsWithItems.php +++ b/src/Traits/InteractsWithItems.php @@ -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()); } ); } @@ -199,7 +199,7 @@ public function needsShipping(): bool */ public function getCurrency(): string { - return $this->currency; + return strtoupper($this->currency); } /** diff --git a/tests/BazarTest.php b/tests/BazarTest.php index 9e9c0580..911fd49e 100644 --- a/tests/BazarTest.php +++ b/tests/BazarTest.php @@ -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');