Download the PHP package stichoza/nbg-currency without Composer
On this page you can find all versions of the php package stichoza/nbg-currency. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package nbg-currency
NBG Currency
National Bank of Georgia (NBG) currency service API wrapper in PHP
Installation
Install this package via Composer.
Note PHP 8.1 or later is required. Use following versions of this package for older PHP versions:
Package version | PHP Version | Documentation |
---|---|---|
v3.0 |
PHP >= 8.1 | v3 Docs |
v2.0 |
PHP >= 7.1.8 | v2 Docs |
v1.2 |
PHP >= 5.5.9 |
Quick Examples
Continue reading for more details about additional functionality.
Basic Usage
The class is namespaced as Stichoza\NbgCurrency\NbgCurrency
:
This package has three main static methods from which you can access currency rates.
Get Currency Rate
The NbgCurrency::rate()
method returns a currency rate in float
.
Parameter | Default | Description |
---|---|---|
$code |
Currency code, not case-sensitive | |
$date |
null |
Date of currency rate: Carbon, DateTime, string or null. |
Important The rate is always for a single unit. The original NBG JSON API returns rate for different amounts per currency. For example Japanese Yen (JPY) rate will be 1.9865 and quantity will be set to 100 (100 JPY is 1.9865 GEL). It is quite confusing during calculations so this package always returns price per single unit. So in this case JPY will be 0.019865 (1 JPY is 0.019865 GEL).
Examples:
When passing dates as Carbon
or DateTime
objects, it's recommended to have its timezone set to Asia/Tbilisi
to avoid unexpected behavior. For convenience, timezone string is available as NbgCurrency::TIMEZONE
class constant.
Get Currency Object
The NbgCurrency::get()
method returns a Currency
object containing data of a currency for specified date.
This method accepts same parameters as ::rate()
method and one additional parameter for language (Used for currency name).
Parameter | Default | Description |
---|---|---|
$code |
Currency code, not case-sensitive | |
$date |
null |
Date of currency rate |
$language |
ka |
Language for currency name (Currently only en or ka ) |
Examples:
Note All properties of
Currency
class are declared asreadonly
. Updating them will result in Fatal Error.
Advanced Usage
Get All Currencies
The NbgCurrency::date()
method will return a Currencies
object. This is a collection-like object that contains a list of all Currency
objects available for specified date.
Parameter | Default | Description |
---|---|---|
$date |
null |
Date of currency rates |
$language |
ka |
Language for names of currencies (Currently only en or ka ) |
Examples:
Currencies
class has date attribute and several methods that you can use.
Note that ->get()
method of Currencies
object has only one parameter string $code
, while the static method with the same name (NbgCurrency::get()
) has two additional parameters described in basic usage examples above.
The Currencies
object also implements Countable
and IteratorAggregate
interfaces, so you can use the object in foreach
loops and count()
function.
Examples:
Error Handling
There are 5 exceptions in Stichoza\NbgCurrency\Exceptions
namespace that could be thrown from methods:
CurrencyNotFoundException
- If currency is not available.DateNotFoundException
- If specified date is not available.LanguageNotFoundException
- If specified language is not available.InvalidDateException
- If the specified date is in invalid format or cannot be parsed.RequestFailedException
- If there was an error during API request.
All exceptions above extend Exception
class, so you can handle all exceptions by catching Exception
or Throwable
.
Additional Info
Number of HTTP Requests
When you access any currency, all currencies for that day in selected language will be fetched (API returns all currencies in a single request) and stored in NbgCurrency class attribute. When you request any other currency, no additional HTTP requests will be made for same date and language.
Memory Usage & Caching
By default, all retrieved currencies are stored in a static property of NbgCurrency
class. If you're planning to get currencies for many different dates, it might use excessive memory. In this case it's recommended to turn off the caching feature.
On the other hand, disabling caching may increase number of HTTP requests being sent. Example:
The above code would make a single HTTP request to the API when caching is enabled. But if you disable caching, it will send 5 separate HTTP requests. To load multiple currencies of same date using a single HTTP request with caching disabled, you can use ::date()
method to get Currencies
object and then access all contained objects after.
In this case there will be a single HTTP request made even with caching disabled.
Keywords
ლარის კურსი, ეროვნული ბანკის გაცვლითი კურსი, ვალუტა, ლარის ვალუტის კურსი, laris kursi, laris valuta, lari currency, national bank of georgia, nbg