Skip to main content
The Currency class provides a wrapper around Brick\Money\Currency with additional formatting and configuration options specific to Laravel Brick Money.

Class Definition

namespace Devhammed\LaravelBrickMoney;

class Currency implements Arrayable, Jsonable, JsonSerializable, Stringable
The Currency class uses Laravel’s Macroable trait, allowing you to extend it with custom methods.

Creating Currency Instances

of()

Create an instance of Currency from Brick\Money\Currency or string.
public static function of(BrickCurrency|string $currency): static
currency
Brick\Money\Currency|string
required
A Brick Currency instance or currency code (e.g., ‘USD’, ‘EUR’)
return
Currency
A new Currency instance
Example:
use Devhammed\LaravelBrickMoney\Currency;

$usd = Currency::of('USD');
$eur = Currency::of('EUR');

Configuration Methods

currencies()

Get or set the available currencies.
public static function currencies(?array $currencies = null): array
currencies
array|null
default:"null"
Array of currency configurations. If null, returns the current currencies.
return
array
The available currencies array
Example:
// Get all available currencies
$currencies = Currency::currencies();

// Set custom currencies
Currency::currencies([
    'USD' => [
        'name' => 'US Dollar',
        'numeric_code' => 840,
        'symbol' => '$',
        'symbol_first' => true,
        'symbol_spaced' => false,
        'decimal_places' => 2,
        'decimal_separator' => '.',
        'thousand_places' => 3,
        'thousand_separator' => ',',
    ],
]);

jsonSerializer()

Get or set the callback to use for JSON serialization.
public static function jsonSerializer(?Closure $callback = null): Closure
callback
Closure|null
default:"null"
A callback that receives a Currency instance and returns an array. If null, returns the current serializer.
return
Closure
The current JSON serializer callback
Example:
Currency::jsonSerializer(fn(Currency $currency) => [
    'name' => $currency->getName(),
    'code' => $currency->getCode(),
]);

Basic Information Methods

getCode()

Get the currency code.
public function getCode(): string
return
string
The currency code (e.g., ‘USD’, ‘EUR’)
Example:
$currency = Currency::of('USD');
echo $currency->getCode(); // "USD"

getName()

Get the currency name.
public function getName(): string
return
string
The full currency name (e.g., ‘US Dollar’)
Example:
$currency = Currency::of('USD');
echo $currency->getName(); // "US Dollar"

getNumericCode()

Get the currency numeric code (ISO).
public function getNumericCode(): int
return
int
The ISO 4217 numeric code
Example:
$currency = Currency::of('USD');
echo $currency->getNumericCode(); // 840

getSymbol()

Get the currency symbol.
public function getSymbol(): string
return
string
The currency symbol (e.g., ’$’, ’€’)
Example:
$usd = Currency::of('USD');
echo $usd->getSymbol(); // "$"

$eur = Currency::of('EUR');
echo $eur->getSymbol(); // "€"

Symbol Position Methods

isSymbolFirst()

Determine if the currency symbol comes first during formatting.
public function isSymbolFirst(): bool
return
bool
True if the symbol appears before the amount
Example:
$usd = Currency::of('USD');
$usd->isSymbolFirst(); // true (formatted as $100.00)

$eur = Currency::of('EUR');
$eur->isSymbolFirst(); // false (formatted as 100,00€)

isSymbolSpaced()

Determine if the currency symbol should have space before/after the amount.
public function isSymbolSpaced(): bool
return
bool
True if there should be a space between symbol and amount
Example:
$usd = Currency::of('USD');
$usd->isSymbolSpaced(); // false (formatted as $100.00)

// Some currencies use spacing
$chf = Currency::of('CHF');
$chf->isSymbolSpaced(); // true (formatted as CHF 100.00)

Decimal Format Methods

getDecimalPlaces()

Get the currency decimal places.
public function getDecimalPlaces(): int
return
int
The number of decimal places
Example:
$usd = Currency::of('USD');
echo $usd->getDecimalPlaces(); // 2

$jpy = Currency::of('JPY');
echo $jpy->getDecimalPlaces(); // 0

getDecimalSeparator()

Get the currency decimal separator.
public function getDecimalSeparator(): string
return
string
The decimal separator character
Example:
$usd = Currency::of('USD');
echo $usd->getDecimalSeparator(); // "."

$eur = Currency::of('EUR');
echo $eur->getDecimalSeparator(); // ","

Thousand Format Methods

getThousandPlaces()

Get the currency thousand places.
public function getThousandPlaces(): int
return
int
The grouping size for thousands
Example:
$usd = Currency::of('USD');
echo $usd->getThousandPlaces(); // 3 (groups of 3 digits)

getThousandSeparator()

Get the currency thousand separator.
public function getThousandSeparator(): string
return
string
The thousands separator character
Example:
$usd = Currency::of('USD');
echo $usd->getThousandSeparator(); // "," (formatted as 1,000.00)

$eur = Currency::of('EUR');
echo $eur->getThousandSeparator(); // "." (formatted as 1.000,00)

Underlying Instance Methods

getCurrency()

Get the underlying Brick\Money\Currency instance.
public function getCurrency(): BrickCurrency
return
Brick\Money\Currency
The underlying Brick Currency instance
Example:
$currency = Currency::of('USD');
$brickCurrency = $currency->getCurrency();

Comparison Methods

is()

Returns whether this currency is equal to the given currency.The currencies are considered equal if their currency codes are equal.
public function is(Currency $currency): bool
currency
Currency
required
The currency to compare with
return
bool
True if currencies are equal
Example:
$usd1 = Currency::of('USD');
$usd2 = Currency::of('USD');
$eur = Currency::of('EUR');

$usd1->is($usd2); // true
$usd1->is($eur);  // false

Serialization Methods

toArray()

Get the instance as an array.
public function toArray(): array
return
array
Array representation of the Currency
Example:
$currency = Currency::of('USD');
$array = $currency->toArray();
// [
//   'name' => 'US Dollar',
//   'code' => 'USD',
//   'numeric_code' => 840,
//   'symbol' => '$',
//   'symbol_first' => true,
//   'symbol_spaced' => false,
//   'decimal_places' => 2,
//   'decimal_separator' => '.',
//   'thousand_places' => 3,
//   'thousand_separator' => ','
// ]

toJson()

Convert the object to its JSON representation.
public function toJson($options = 0): string
options
int
default:"0"
JSON encoding options
return
string
JSON string representation
Example:
$currency = Currency::of('USD');
echo $currency->toJson();
// {"name":"US Dollar","code":"USD","numeric_code":840,...}

jsonSerialize()

Specify data which should be serialized to JSON.
public function jsonSerialize(): array
return
array
Array to be JSON serialized

__toString()

Convert the object to its string representation.
public function __toString(): string
return
string
String representation (returns the currency code)
Example:
$currency = Currency::of('USD');
echo $currency; // "USD"

Complete Usage Example

Here’s a comprehensive example showing various Currency methods:
use Devhammed\LaravelBrickMoney\Currency;
use Devhammed\LaravelBrickMoney\Money;

// Create currencies
$usd = Currency::of('USD');
$eur = Currency::of('EUR');

// Get basic information
echo $usd->getCode();         // "USD"
echo $usd->getName();         // "US Dollar"
echo $usd->getNumericCode();  // 840
echo $usd->getSymbol();       // "$"

// Check formatting rules
$usd->isSymbolFirst();        // true
$usd->isSymbolSpaced();       // false
$usd->getDecimalPlaces();     // 2
$usd->getDecimalSeparator();  // "."
$usd->getThousandPlaces();    // 3
$usd->getThousandSeparator(); // ","

// Compare currencies
$usd->is($eur);               // false

// Use with Money
$money = Money::of(1000, $usd);
echo $money->format();        // "$1,000.00"

// Serialize to JSON
echo $usd->toJson();
// {"name":"US Dollar","code":"USD","numeric_code":840,...}

// String representation
echo $usd;                    // "USD"

Macroable

The Currency class uses Laravel’s Macroable trait, allowing you to add custom methods:
use Devhammed\LaravelBrickMoney\Currency;

// Add a macro to check if currency is cryptocurrency
Currency::macro('isCrypto', function(): bool {
    return in_array($this->getCode(), ['BTC', 'ETH', 'USDT']);
});

$btc = Currency::of('BTC');
$btc->isCrypto(); // true

$usd = Currency::of('USD');
$usd->isCrypto(); // false

Common Currency Examples

Here are formatting examples for common currencies:
// USD - Symbol first, no space, comma thousands, period decimal
$usd = Currency::of('USD');
// Format: $1,000.00

// EUR - Symbol last, no space, period thousands, comma decimal
$eur = Currency::of('EUR');
// Format: 1.000,00€

// GBP - Symbol first, no space, comma thousands, period decimal
$gbp = Currency::of('GBP');
// Format: £1,000.00

// JPY - Symbol first, no decimals
$jpy = Currency::of('JPY');
// Format: ¥1,000

// CHF - Symbol first with space
$chf = Currency::of('CHF');
// Format: CHF 1,000.00

Build docs developers (and LLMs) love