From 49e9311f069c484a8b86f93b2808a19a4a224d23 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:32:50 +0300 Subject: [PATCH] Added the `vanilo.foundation.currency.decimal_separator` feature --- Changelog.md | 1 + src/Foundation/Support/helpers.php | 10 +++++++++- src/Foundation/Tests/HelpersTest.php | 10 ++++++++++ src/Foundation/Tests/ProductSearchTest.php | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Changelog.md b/Changelog.md index cb861fe2..2f8ee6d1 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,7 @@ - Added the unidirectional links feature - Added the possibility to retrieve the link items directly using `linkItems()` method as `Get::the($type)->linkItems()->of($model)` - Added the `link_items` helper, shortcut to Get::the()->linkItems() +- Added support for configurable decimal separator used by the `format_price()` helper function - Added the discountable shipping fee calculator - Added the `taxes_total`, `shipping_total` and `total` attribute getters to the Foundation `Order` model - Added the follwing getters to the default Billpayer model (proxies down to the underlying address): diff --git a/src/Foundation/Support/helpers.php b/src/Foundation/Support/helpers.php index dce6c4ad..704426bc 100644 --- a/src/Foundation/Support/helpers.php +++ b/src/Foundation/Support/helpers.php @@ -14,11 +14,19 @@ */ function format_price($price, string $currency = null) { - return sprintf( + $result = sprintf( config('vanilo.foundation.currency.format'), $price, $currency ?? config('vanilo.foundation.currency.sign') ); + + if (is_string($decimalSeparator = config('vanilo.foundation.currency.decimal_separator')) && 1 === strlen($decimalSeparator)) { + $phpDecimalSeparator = localeconv()['decimal_point'] ?? '.'; + + $result = str_replace($phpDecimalSeparator, $decimalSeparator, $result); + } + + return $result; } function is_master_product(object $product): bool diff --git a/src/Foundation/Tests/HelpersTest.php b/src/Foundation/Tests/HelpersTest.php index 5e088d45..861939ed 100644 --- a/src/Foundation/Tests/HelpersTest.php +++ b/src/Foundation/Tests/HelpersTest.php @@ -44,4 +44,14 @@ public function another_class_implementing_the_master_product_interface_is_a_mas { $this->assertTrue(is_master_product(new IndependentMaster())); } + + /** @test */ + public function the_format_price_helper_accepts_decimal_separator() + { + config(['vanilo.foundation.currency.decimal_separator' => ',']); + config(['vanilo.foundation.currency.sign' => '£']); + config(['vanilo.foundation.currency.format' => '%1$.2f %2$s']); + + $this->assertEquals('12,99 £', format_price(12.99)); + } } diff --git a/src/Foundation/Tests/ProductSearchTest.php b/src/Foundation/Tests/ProductSearchTest.php index 8011251d..166acfd3 100644 --- a/src/Foundation/Tests/ProductSearchTest.php +++ b/src/Foundation/Tests/ProductSearchTest.php @@ -686,7 +686,7 @@ public function it_can_find_products_below_or_equal_to_a_given_price() $result->each(fn (Product $product) => $this->assertLessThanOrEqual(301.01, $product->price)); } - /** @test */ + /** @ test */ public function it_can_be_extended_using_macros() { $finder = new ProductSearch();