final class money implements JsonSerializable, Stringable

internal  
 

Value Object: Money.

Stores currency amounts as integers (minor units) to avoid floating point errors. Example: R$ 10,00 is stored as 1000.

Capabilities:

  • Immutable arithmetic (add, subtract, multiply).
  • Automatic formatting based on Locale.
  • Automatic currency resolution based on Locale.
  • Dynamic decimal precision handling (JPY vs BRL vs KWD).

Constants

DEFAULT_CURRENCY

Properties

string $currency

Methods

__construct(int $amount, string $currency = '')

No description

string
__toString()

String representation using formatted output.

static money
from_current_locale(float $amount_float)

Factory: Create from current user's Moodle Locale.

static money
from_float(float $amount, string $currency)

Create instance from a float value (Major Units).

float
to_float()

Returns the amount as a float (Major Units).

add(money $other)

Add another money object.

subtract(money $other)

Subtract another money object.

multiply(float $multiplier)

Multiply by a factor (e.g. tax calculation).

bool
is_zero()

Check if money represents zero.

bool
is_positive()

Check if money is positive.

bool
is_negative()

Check if money is negative.

string
format(string|null $locale = null)

Format the money for display using PHP's Intl NumberFormatter.

array
jsonSerialize()

JSON Serialization.

Details

at line 51
__construct(int $amount, string $currency = '')

No description

Parameters

int $amount

Amount in minor units (cents, centavos, yen, fils)

string $currency

ISO 4217 Currency Code (e.g. BRL, USD).

at line 72
string __toString()

String representation using formatted output.

Return Value

string

at line 87
static money from_current_locale(float $amount_float)

Factory: Create from current user's Moodle Locale.

If the user is viewing the site in "Português - Brasil" (pt_br), this creates a BRL money object.

Parameters

float $amount_float

Amount in major units (e.g. 10.50)

Return Value

money

at line 110
static money from_float(float $amount, string $currency)

Create instance from a float value (Major Units).

Automatically calculates the minor units based on the currency precision.

Example: from_float(10.50, 'BRL') -> amount: 1050 Example: from_float(100, 'JPY') -> amount: 100

Parameters

float $amount
string $currency

Return Value

money

at line 127
float to_float()

Returns the amount as a float (Major Units).

Useful for API responses or generic math.

Return Value

float

at line 148
money add(money $other)

Add another money object.

Parameters

money $other

Return Value

money

Exceptions

InvalidArgumentException

at line 164
money subtract(money $other)

Subtract another money object.

Parameters

money $other

Return Value

money

Exceptions

InvalidArgumentException

at line 179
money multiply(float $multiplier)

Multiply by a factor (e.g. tax calculation).

Rounds to the nearest integer unit.

Parameters

float $multiplier

Return Value

money

at line 187
bool is_zero()

Check if money represents zero.

Return Value

bool

at line 195
bool is_positive()

Check if money is positive.

Return Value

bool

at line 203
bool is_negative()

Check if money is negative.

Return Value

bool

at line 219
string format(string|null $locale = null)

Format the money for display using PHP's Intl NumberFormatter.

Parameters

string|null $locale

Example: 'pt_BR', 'en_US'. Defaults to Moodle current lang.

Return Value

string

e.g., "R$ 1.250,00" or "US$ 1,250.00"

at line 247
array jsonSerialize()

JSON Serialization.

Return Value

array