Download the PHP package maciej-sz/nbp-php without Composer
On this page you can find all versions of the php package maciej-sz/nbp-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package nbp-php
nbp-php
API for accessing Polish National Bank (NBP - Narodowy Bank Polski) currency and commodities exchange rates.
Usage
Installing via composer
Minimal setup
Examples
All working examples from this README are included in the examples/
directory of the repository.
Services
CurrencyAverageRatesService
This service provides API for accessing average rates published in NBP tables.
fromMonth
method
Returns flat collection of rates from all NBP tables at given month
fromDay
method
Returns a dictionary with NBP tables from given day.
fromDayBefore
method
Returns a dictionary with NBP tables from day before given day. This method can be useful in some bookkeeping applications when there is a legislatory need to calculate transfer prices. The legislation requires for the prices to be calculated using currency rate applied in the business day before the actual transfer date. Which is exactly what this method exposes.
Example:
getMonthTablesA
method
Returns the A
table iterator from a specific month. Rates here are grouped into tables,
which represent the actual data structure provided by NBP. To get the rates there needs
to be second iteration:
Example getting specific rate:
getMonthTablesB
method
Returns the B
table iterator from a specific month.
Warning about missing currencies in table B
In table B
there can be multiple currencies with the same code.
It is also possible, that a specific currency is present in table from one day, but is not present in table from the next day.
In such case you should not use the getRate($rate)
method but rather
iterate over all currencies returned by getRates()
.
Currency trading rates service
This service is used to get buy and sell currency rates from NBP tables.
fromMonth
method
Returns trading rates from entire month.
fromEffectiveDay
method
Return rates from effective date.
fromTradingDay
method
Return rates from trading date.
Gold rates service
This service is used to get gold commodity rates from NBP tables.
fromMonth
method
Gets all rates from specific month.
fromDay
method
Returns a gold rate from specific date.
fromDayBefore
method
Returns a gold rate from before a specific date.
Using cache
Note that a library implementing PSR-6 has to be provided in order to use the caching abilities.
The CachedTransport
class is a proxy for all other transport implementations.
This transport has to be backed by another transport, as it relies on it to
make the actual requests that have not been cached yet.
Example
Using custom transport
The library uses Symfony HTTP Client and Guzzle as default transports.
If those packages are not available then it falls back to the file_get_contents
method. This may not be ideal in some situations. Especially when there is no access
to HTTP client packages as may be the case when using PHP version prior to 8.0.
In such cases it is suggested to use different transport. It can be achieved
by replacing the TransportFactory
of the NbpClient
with your own implementation.
Layers
Service layer
The service consists of facades
for the package. Classes here are named services
instead of facades due to common
understanding of this word as something through which you access the internals of a system.
This layer provides a high level business-oriented methods for interacting
with the NBP API.
It exposes most common use cases of the nbp-php
package and is the
likely starting point for all applications using this package.
One needs to interact with other layers only for more complex tasks.
The service layer is structured in the way that it directly communicates with the repository layer.
Repository layer
Repository layer allows getting data from NBP API by providing methods that closely reflect the NBP Web API. This layer operates on higher level, hydrated domain objects.
The only constraint here is that the repository layer operates on month-based dates. So for example you can get trading rates from entire month, but in order to retrieve a specific date you have to iterate through the retrieved month (you can use service layer for that). This is by design for the purpose of reducing network traffic to the NBP servers. It also allows simplifying caching on the transport layer, because there is no need for any cache-pooling logic.
Client layer
Client layer is a bridge between the repository and the transport layer. It processes request objects on the input, and then it uses the transport layer to fulfill those requests.
The requests thet client layer uses are higher-level requests then those on
the transport layer. They implement NbpClientRequest
interface.
Transport layer
The transport layer is responsible for directly interacting with the NBP API. A few independent transport implementations are provided for serving connections to the NBP API.
It is also equipped with a convenient factories which pick the most appropriate implementation depending on installed libraries and configuration.
All versions of nbp-php with dependencies
ext-json Version *