Skip to content

Commit

Permalink
Added the vanilo.foundation.currency.decimal_separator feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jun 3, 2024
1 parent 7b1e45d commit 49e9311
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 9 additions & 1 deletion src/Foundation/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/Foundation/Tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion src/Foundation/Tests/ProductSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 49e9311

Please sign in to comment.