-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
129 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters