Skip to content

Commit

Permalink
add currency class
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Feb 3, 2024
1 parent 68710a0 commit b63e785
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Fields/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Cone\Bazar\Fields;

use Cone\Bazar\Bazar;
use Cone\Bazar\Support\Currency;
use Cone\Root\Fields\Meta;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Number;

class Price extends Meta
{
Expand All @@ -29,7 +29,7 @@ public function __construct(string $label, ?string $currency = null)
$this->field->min(0);

$this->field->format(function (Request $request, Model $model, mixed $value): ?string {
return is_null($value) ? null : Number::currency($value, $this->currency);
return is_null($value) ? null : (new Currency($value, $this->currency))->format();
});
}

Expand Down
7 changes: 4 additions & 3 deletions src/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cone\Bazar\Database\Factories\ItemFactory;
use Cone\Bazar\Interfaces\Buyable;
use Cone\Bazar\Interfaces\Models\Item as Contract;
use Cone\Bazar\Support\Currency;
use Cone\Bazar\Traits\InteractsWithTaxes;
use Cone\Root\Traits\InteractsWithProxy;
use Illuminate\Database\Eloquent\Casts\Attribute;
Expand Down Expand Up @@ -208,7 +209,7 @@ public function getPrice(): float
*/
public function getFormattedPrice(): string
{
return Number::currency($this->getPrice(), $this->itemable->getCurrency());
return (new Currency($this->getPrice(), $this->itemable->getCurrency()))->format();
}

/**
Expand All @@ -224,7 +225,7 @@ public function getTotal(): float
*/
public function getFormattedTotal(): string
{
return Number::currency($this->getTotal(), $this->itemable->getCurrency());
return (new Currency($this->getTotal(), $this->itemable->getCurrency()))->format();
}

/**
Expand All @@ -240,7 +241,7 @@ public function getSubtotal(): float
*/
public function getFormattedSubtotal(): string
{
return Number::currency($this->getSubtotal(), $this->itemable->getCurrency());
return (new Currency($this->getSubtotal(), $this->itemable->getCurrency()))->format();
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Models/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cone\Bazar\Database\Factories\ShippingFactory;
use Cone\Bazar\Interfaces\Models\Shipping as Contract;
use Cone\Bazar\Support\Currency;
use Cone\Bazar\Support\Facades\Shipping as Manager;
use Cone\Bazar\Traits\Addressable;
use Cone\Bazar\Traits\InteractsWithTaxes;
Expand Down Expand Up @@ -214,7 +215,7 @@ public function getPrice(): float
*/
public function getFormattedPrice(): string
{
return Number::currency($this->getPrice(), $this->shippable->getCurrency());
return (new Currency($this->getPrice(), $this->shippable->getCurrency()))->format();
}

/**
Expand All @@ -230,7 +231,7 @@ public function getTotal(): float
*/
public function getFormattedTotal(): string
{
return Number::currency($this->getTotal(), $this->shippable->getCurrency());
return (new Currency($this->getTotal(), $this->shippable->getCurrency()))->format();
}

/**
Expand All @@ -246,7 +247,7 @@ public function getSubtotal(): float
*/
public function getFormattedSubtotal(): string
{
return Number::currency($this->getSubtotal(), $this->shippable->getCurrency());
return (new Currency($this->getSubtotal(), $this->shippable->getCurrency()))->format();
}

/**
Expand Down
94 changes: 94 additions & 0 deletions src/Support/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Cone\Bazar\Support;

use Closure;
use Cone\Bazar\Bazar;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Traits\Macroable;
use JsonSerializable;
use NumberFormatter;
use Stringable;

class Currency implements JsonSerializable, Stringable
{
use Macroable;

/**
* The currency value.
*/
protected int|float $value;

/**
* The currency.
*/
protected string $currency;

/**
* The currency locale.
*/
protected string $locale = 'en';

/**
* The currency precision.
*/
protected ?int $precision = null;

/**
* The currency formatter.
*/
protected static ?Closure $formatter = null;

/**
* Create a new currency instance.
*/
public function __construct(int|float $value, ?string $currency = null, ?int $precision = null, ?string $locale = null)
{
$this->value = $value;
$this->currency = $currency ?: Bazar::getCurrency();
$this->precision = $precision;
$this->locale = $locale ?: App::getLocale();
}

/**
* Set the formatter callback.
*/
public static function formatUsing(Closure $callback): void
{
static::$formatter = $callback;
}

/**
* Format the currency.
*/
public function format(): string
{
$formatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);

if (! is_null($this->precision)) {
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $this->precision);
}

if (! is_null(static::$formatter)) {
call_user_func_array(static::$formatter, [$formatter, $this->value, $this->currency]);
}

return $formatter->formatCurrency($this->value, $this->currency);
}

/**
* Get the JSON serializable format of the currency.
*/
public function jsonSerialize(): mixed
{
return $this->format();
}

/**
* Convert the currency to string.
*/
public function __toString(): string
{
return $this->format();
}
}
4 changes: 2 additions & 2 deletions src/Traits/HasPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Cone\Bazar\Bazar;
use Cone\Bazar\Models\Price;
use Cone\Bazar\Relations\Prices;
use Cone\Bazar\Support\Currency;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Number;

trait HasPrices
{
Expand Down Expand Up @@ -69,7 +69,7 @@ public function getFormattedPrice(?string $currency = null): ?string

$price = $this->getPrice($currency);

return is_null($price) ? null : Number::currency($price, $currency);
return is_null($price) ? null : (new Currency($price, $currency))->format();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/InteractsWithDiscounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Cone\Bazar\Traits;

use Cone\Bazar\Support\Currency;
use Cone\Bazar\Support\Facades\Discount;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Number;

trait InteractsWithDiscounts
{
Expand Down Expand Up @@ -35,7 +35,7 @@ public function getDiscount(): float
*/
public function getFormattedDiscount(): string
{
return Number::currency($this->getDiscount(), $this->currency);
return (new Currency($this->getDiscount(), $this->currency))->format();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Traits/InteractsWithItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cone\Bazar\Interfaces\LineItem;
use Cone\Bazar\Models\Item;
use Cone\Bazar\Models\Shipping;
use Cone\Bazar\Support\Currency;
use Cone\Bazar\Support\Facades\Shipping as ShippingManager;
use Cone\Root\Interfaces\Models\User;
use Illuminate\Database\Eloquent\Casts\Attribute;
Expand All @@ -17,7 +18,6 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Number;

trait InteractsWithItems
{
Expand Down Expand Up @@ -241,7 +241,7 @@ public function getTotal(): float
*/
public function getFormattedTotal(): string
{
return Number::currency($this->getTotal(), $this->getCurrency());
return (new Currency($this->getTotal(), $this->getCurrency()))->format();
}

/**
Expand All @@ -261,7 +261,7 @@ public function getSubtotal(): float
*/
public function getFormattedSubtotal(): string
{
return Number::currency($this->getSubtotal(), $this->getCurrency());
return (new Currency($this->getSubtotal(), $this->getCurrency()))->format();
}

/**
Expand All @@ -281,7 +281,7 @@ public function getFeeTotal(): float
*/
public function getFormattedFeeTotal(): string
{
return Number::currency($this->getFeeTotal(), $this->getCurrency());
return (new Currency($this->getFeeTotal(), $this->getCurrency()))->format();
}

/**
Expand All @@ -301,7 +301,7 @@ public function getTax(): float
*/
public function getFormattedTax(): string
{
return Number::currency($this->getTax(), $this->getCurrency());
return (new Currency($this->getTax(), $this->getCurrency()))->format();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/InteractsWithTaxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Cone\Bazar\Bazar;
use Cone\Bazar\Models\Item;
use Cone\Bazar\Models\Shipping;
use Cone\Bazar\Support\Currency;
use Cone\Bazar\Support\Facades\Tax;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Number;

trait InteractsWithTaxes
{
Expand Down Expand Up @@ -46,7 +46,7 @@ public function getFormattedTax(): string
$currency = $this->shippable->currency;
}

return Number::currency($this->getTax(), $currency);
return (new Currency($this->getTax(), $currency))->format();
}

/**
Expand Down
14 changes: 9 additions & 5 deletions tests/Models/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Cone\Bazar\Models\Cart;
use Cone\Bazar\Models\Item;
use Cone\Bazar\Models\Product;
use Cone\Bazar\Support\Currency;
use Cone\Bazar\Support\Facades\Tax;
use Cone\Bazar\Tests\TestCase;
use Illuminate\Support\Number;

class ItemTest extends TestCase
{
Expand All @@ -35,15 +35,19 @@ public function setUp(): void
public function test_item_is_taxable(): void
{
$this->assertInstanceOf(Taxable::class, $this->item);
$this->assertSame(Number::currency($this->item->tax, $this->item->itemable->currency), $this->item->getFormattedTax());
$this->assertSame(
(new Currency($this->item->tax, $this->item->itemable->currency))->format(),
$this->item->getFormattedTax()
);
$this->assertSame($this->item->getFormattedTax(), $this->item->formattedTax);
}

public function test_item_has_price_attribute(): void
{
$this->assertSame($this->item->price, $this->item->getPrice());
$this->assertSame(
Number::currency($this->item->price, $this->item->itemable->currency), $this->item->getFormattedPrice()
(new Currency($this->item->price, $this->item->itemable->currency))->format(),
$this->item->getFormattedPrice()
);
$this->assertSame($this->item->getFormattedPrice(), $this->item->formattedPrice);
}
Expand All @@ -56,14 +60,14 @@ public function test_item_has_total_attribute(): void
);
$this->assertSame($this->item->getTotal(), $this->item->total);
$this->assertSame(
Number::currency($this->item->total, $this->item->itemable->currency),
(new Currency($this->item->total, $this->item->itemable->currency))->format(),
$this->item->getFormattedTotal()
);
$this->assertSame($this->item->getFormattedTotal(), $this->item->formattedTotal);
$this->assertSame($this->item->price * $this->item->quantity, $this->item->getSubtotal());
$this->assertSame($this->item->getSubtotal(), $this->item->subtotal);
$this->assertSame(
Number::currency($this->item->subtotal, $this->item->itemable->currency),
(new Currency($this->item->subtotal, $this->item->itemable->currency))->format(),
$this->item->getFormattedSubtotal()
);
$this->assertSame($this->item->getFormattedSubtotal(), $this->item->formattedSubtotal);
Expand Down
9 changes: 5 additions & 4 deletions tests/Models/ShippingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Cone\Bazar\Models\Cart;
use Cone\Bazar\Models\Order;
use Cone\Bazar\Models\Shipping;
use Cone\Bazar\Support\Currency;
use Cone\Bazar\Support\Facades\Shipping as ShippingManager;
use Cone\Bazar\Support\Facades\Tax;
use Cone\Bazar\Tests\TestCase;
use Cone\Bazar\Tests\User;
use Illuminate\Support\Number;

class ShippingTest extends TestCase
{
Expand Down Expand Up @@ -88,7 +88,8 @@ public function testt_is_taxable(): void
$this->assertInstanceOf(Taxable::class, $this->shipping);
$this->assertSame($this->shipping->price * 0.1, $this->shipping->tax);
$this->assertSame(
Number::currency($this->shipping->tax, $this->shipping->shippable->currency), $this->shipping->getFormattedTax()
(new Currency($this->shipping->tax, $this->shipping->shippable->currency))->format(),
$this->shipping->getFormattedTax()
);
$this->assertSame($this->shipping->getFormattedTax(), $this->shipping->formattedTax);
}
Expand All @@ -101,14 +102,14 @@ public function testt_has_total_attribute(): void
);
$this->assertSame($this->shipping->getTotal(), $this->shipping->total);
$this->assertSame(
Number::currency($this->shipping->total, $this->shipping->shippable->currency),
(new Currency($this->shipping->total, $this->shipping->shippable->currency))->format(),
$this->shipping->getFormattedTotal()
);
$this->assertSame($this->shipping->getFormattedTotal(), $this->shipping->formattedTotal);
$this->assertSame($this->shipping->cost, $this->shipping->getSubtotal());
$this->assertSame($this->shipping->getSubtotal(), $this->shipping->subtotal);
$this->assertSame(
Number::currency($this->shipping->subtotal, $this->shipping->shippable->currency),
(new Currency($this->shipping->subtotal, $this->shipping->shippable->currency))->format(),
$this->shipping->getFormattedSubtotal()
);
$this->assertSame($this->shipping->getFormattedSubtotal(), $this->shipping->formattedSubtotal);
Expand Down

0 comments on commit b63e785

Please sign in to comment.