rustem-kaimolla/kz-currency-rates

PHP-библиотека для получения курсов валют с НБ РК

v1.0.0 2025-05-12 18:41 UTC

This package is auto-updated.

Last update: 2025-05-22 05:17:27 UTC


README

PHP License Packagist Downloads

Lightweight PHP library for getting exchange rates from the official API of the National Bank of Kazakhstan.

📡 Source: https://nationalbank.kz/rss/get_rates.cfm?fdate=dd.mm.YYYY

🚀 Installation

composer require rusdev/kz-currency-rates

🧱 Stack

  • PHP 8.1+
  • Guzzle HTTP client
  • PSR-4 autoload

📦 Quick start

use Currency\Requests\RateRequestBuilder;
use Currency\Parsers\XmlRateParser;
use Currency\Services\RateFetcher;
use GuzzleHttp\Client;

$builder = (new RateRequestBuilder())->forDate('12.05.2025');
$parser = new XmlRateParser();

$fetcher = new RateFetcher(
    $builder->build(),
    $parser
);

$rates = $fetcher->getAllRates();

foreach ($rates as $rate) {
    echo $rate->code . ': ' . $rate->rate . PHP_EOL;
}

📌 Receiving a specific currency

$usd = $fetcher->getRate('USD');

echo "USD/KZT: {$usd->rate}";

🧠 Filtering major currencies (Specification)

use Currency\Filters\MajorCurrencySpecification;

$spec = new MajorCurrencySpecification();

$majorRates = array_filter($rates, fn($rate) => $spec->isSatisfiedBy($rate));

foreach ($majorRates as $rate) {
    echo $rate->currency() . ': ' . $rate->value() . PHP_EOL;
}

🧪 Tests

composer test

Test Coverage:

  • XmlRateParser
  • RateRequestBuilder
  • CurrencyRateDTO
  • MajorCurrencySpecification
  • RateFetcher (с Guzzle mock)

📄 License

MIT.