\Hazaar
Money
Money class

This class is used to extend a normal integer value by adding currency related features such as the currency type (AUD, USD, JPY, etc) and realtime currency conversion using Yahoo Quotes.

Example

$aud = new Money(500, ‘AUD’); $usd = new Money(200, ‘USD’); $total = $aud->add($usd);

The default money format is ‘%.2n’ which will format the value to whole dollar with 2 decimal places. ie: $123.45. You can specify the format when retrieving the amount (see Money::format()) or you can set the default format at any time.

You can also set the default currency code to use when none is specified.

It is recommended that these be set in your bootstrap file so that they are consistent across the whole application.

Example bootstrap.php

Hazaar\Money::$money_format = ‘%.0n’; Hazaar\Money::$default_currency = ‘AUD’;

Summary
Methods Properties Constants
add
No constants
Properties
$cache
$cache
Static
$currency_info
$currency_info

Tags

Private
Static
$default_currency
$default_currency
Default currency code
Static
$exchange_rates
$exchange_rates
$local_currency
$local_currency
Static
$money_format
$money_format
Default money format
$value
$value
Methods
__construct()
__construct($value, $currency = null)

The money class constructors takes two parameters. The value of the currency and the type of currency the value is representative of.

Currency info is loaded from a built-in support file named countryInfo.txt. This file contains country information including country codes, currency names, etc. The first time a currency is used this information is loaded into memory only once and is shared between all currency objects.

A cache object is also set up for use by the exchange conversion methods. It will attempt to use the APC cache backend but if that is not available it will fall back to the file backend.

Parameters

$value\Hazaar\float

The currency value amount

$currencystring

The name of the currency or country of origin. Ie: ‘USD’ and ‘US’ will both resolve to US dollars.

__tostring()
__tostring() : string

This magic function is called when PHP tries to automatically convert the currency to a string. Simply calls the Money::toString() method.

Returns

string

eg: $100USD

add()
add() : Money

Add one or more amounts or Money objects to the current currency. Parameters here can be either a numeric value or another Money object. If the parameter is a Money object then the value will be automatically converted using the current exchange rate before it is added.

Parameters

Any number of numeric or Money objects parameters.

Returns

\Hazaar\Money

The sum of all values as a new Money object.

convertTo()
convertTo($foreign_currency) : Money

Convert the currency object to another currency and return a new Money object.

Parameters

$foreign_currencystring

The currency to convert to. Can be country or currency code.

Returns

\Hazaar\Money

A new currency object with the value of the foreign currency amount.

format()
format($format = null) : string

The format method will format the currency value amount to an international standard format of {symbol}{amount}{code}. For example, US dollars will be expressed as $100USD. Australian dollar as $105AUD and so on.

Parameters

$formatstring

An optional format passed to the money_format function. If not specified the global default format will be used.

Returns

string

The currency value as a formatted string

getCode()
getCode($country = null) : string

Get the currency code for the current currency object or look up the currency code for a country. This value is normalised during object instantiation. This means that if you specify a country upon instantiation, this will still return the correct currency code.

Optionally, if a country parameter is specified, this method can be used to look up the currency code for that country.

Example

echo $currency->getCode(‘au’); //This will echo the string ‘AUD’.

Parameters

$countrystring

Optional country code to look up a currency code for.

Returns

string

The currency code requested.

getCurrencyCode()
getCurrencyCode($code = null) : string

Get either the default currency code, or get a currency code for a country. Use this instead of accessing Money::default_currency directly because if Money::$default_currency is not set, this will try and determine the default currency and set it automatically which will occur when a new Money object is created.

Tags

Since

1.2

Parameters

$code

No description

Returns

string

A valid currency code such as AUD, USD, JPY, etc.

getCurrencySymbol()
getCurrencySymbol() : string

Get the symbol for the current currency. The currency symbol is usually prefixed to the currency amount. This method doesn’t actually return the currency symbol as such, but will return the HTML entity name of the currency symbol, for example ‘dollar’, ‘pound’, ‘yen’, etc.

Returns

string

The HTML entity name of the currency symbol.

getExchangeRate()
getExchangeRate($foreign_currency) : float

Get the current exchange rate for the currency value against a foreign currency. This method uses the Yahoo Quotes service to get the current exchange rate. For this method to work your host needs to have web access (ie: port 80). This should ‘just work’ for all but a small number of cases.

Because this method contacts another web service the response can be a little slow. Because of this results are cached so that subsequent requests for the same conversion will be faster.

Parameters

$foreign_currencystring

The foreign currency to get an exchange rate for.

Returns

\Hazaar\float

The current currency exchange rate.

loadCurrencyInfo()
loadCurrencyInfo()
subtract()
subtract() : Money

Subtract one or more values or Money objects from the current object. Parameters can be either a numeric value or another Money object. If the parameter is a Money object then the value will be automatically converted using the current exchange rate before it is subtracted.

Returns

\Hazaar\Money

The result of subtraction as a new Money object.

toCents()
toCents() : int

Get the currency value represented as an integer in cents. 1 dollar = 100 cents.

Returns

int

The currency value in whole cents.

toFloat()
toFloat($precision = 2) : float

Get the currency as a float value formatted using the money_format PHP function with optional precision to specify the number of decimal places.

Parameters

$precisionint

The number of decimal places to round to. Defaults to 2.

Returns

\Hazaar\float

The currency value as a float

toString()
toString() : string

Convert currency to a string. Outputs the same as the Money::format() method using the default format.

Returns

string

eg: $100USD