PHPackages                             ob-ivan/sd-currency - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ob-ivan/sd-currency

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ob-ivan/sd-currency
===================

v4.1(8y ago)01.0kPHP

Since Jul 31Pushed 8y ago1 watchersCompare

[ Source](https://github.com/ob-ivan/sd-currency)[ Packagist](https://packagist.org/packages/ob-ivan/sd-currency)[ RSS](/packages/ob-ivan-sd-currency/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (3)Versions (19)Used By (0)

A collection of utility functions to enumerate available currencies and update their actual exchange rates.

Installation
============

[](#installation)

```
composer require ob-ivan/sd-currency
```

Usage
=====

[](#usage)

Registry
--------

[](#registry)

A currency instance associates currency code with its representations in Unicode and as an HTML entity.

These currencies are supported:

- RUB
- EUR
- USD

You can enumerate available currencies using a registry instance:

```
use SD\Currency\Model\Registry;

$registry = new Registry();
foreach ($registry->getAll() as $currency) {
    print "The Unicode symbol for {$currency->getCode()} is {$currency->getUnicode()}\n";
}
```

Or if you use dependency injection (see below):

```
foreach ($this->getCurrency()->getRegistry()->getAll() as $currency) {
    ...
}
```

Formatter
---------

[](#formatter)

Use formatter service to format price according to given currency. Formatter instance uses a number of configuration parameters.

```
use SD\Currency\Model\Money;
use SD\Currency\Model\Registry;
use SD\Currency\Service\Formatter;

$registry = new Registry();
$formatter = new Formatter($registry, ['thousandSeparator' => '\'']);
// or using dependency injection:
$formatter = $this->getCurrency()->getFormatter($formatName);

echo $formatter->formatMoney(new Money(10000, $registry->getByCode('USD'))); // $ 10'000
echo $formatter->formatMoney(new Money(590000, $registry->getByCode('RUB'))); // 590'000 ₽
```

Repository &amp; Store
----------------------

[](#repository--store)

All functions are available within repository instance which acts as a library facade:

```
use SD\Currency\Repository;

$repository = new Repository($config);
```

Some of its methods require a store class to be configured:

```
use SD\Currency\Repository;
use SD\Currency\Store\FileStore;

$repository = new Repository([
    'store' => FileStore::class,
    'args' => [
        'dir' => __DIR__ . '/currency_cache',
    ],
]);
$options = $repository->getOptions();
```

The store is used to retrieve currency rates and to persist them when updated (see Updater secion).

Two store implementations are provided by this library:

- `FileStore` uses a json file on hard drive.
- `ArrayStore` only keeps data in memory and is well suited for unit testing.

You can also implement `SD\Currency\Store\StoreInterface` to provide your own kind of store, e.g. in database or memcache. Then pass its class name to `Repository` constructor as shown above.

Updater
-------

[](#updater)

An updater service runs against the provided store and sets up exchange rates from the fixed source. The official API of Central Bank of Russia is used by default. You can configure the updater with your own XML source and XPath, as well as update interval (defaults to '1 day') if you don't want to make too much requests to your source.

```
use SD\Currency\Model\Registry;
use SD\Currency\Service\Updater;
use SD\Currency\Store\FileStore;

$registry = new Registry();
$store = new FileStore(__DIR__);
$updaterConfig = [
    'update_interval' => '30 minutes',
];
$updater = new Updater($registry, $store, $updaterConfig);
$updater->updateRates();
```

Or in case you use dependency injection:

```
$this->getCurrency()->getUpdater()->updateRates();
```

Dependency Injection
--------------------

[](#dependency-injection)

If you use `SD\DependencyInjection\Container` in your application, consumers may import dependency injection trait to take advantage of the autodeclare feature:

```
use SD\Currency\DependencyInjection\CurrencyAwareTrait;
use SD\DependencyInjection\AutoDeclarerInterface;
use SD\DependencyInjection\AutoDeclarerTrait;

class ExampleController implements AutoDeclarerInterface
{
    use AutoDeclarerTrait;
    use CurrencyAwareTrait;

    public function exampleAction()
    {
        return $this->render('example.twig', [
            'currencyOptions' => $this->getCurrency()->getOptions(),
        ]);
    }
}
```

Configuration
-------------

[](#configuration)

When using dependency injection you may provide a configuration file to set up your services:

```
# config/currency.yaml
currency:
    store:
        class: App\Currency\Store
    formatter:
        class: App\Currency\Formatter
        myAwesomeFormat:
            thousandSeparator: ','
            symbolType: fontAwesome
            roundDirection: ceil
            roundDigits: 2
        myOtherFormat:
            ...
    updater:
        class: App\Currency\Updater
        config:
            url: https://money.example.com/
            xpath: //currency[code = "$code"]/rate
            updateInterval: 3 hours
```

Use `ConfigLoader` to populate a container with config values:

```
use SD\Config\ConfigLoader;
use SD\Currency\DependencyInjection\CurrencyProvider;
use SD\DependencyInjection\Container;

$loader = new ConfigLoader('/path/to/config/dir');
$config = $loader->load();
$container = new Container(['config' => $config]);
$container->connect(new CurrencyProvider());
```

This will inject config values into corresponding services:

```
$container->inject(function ($currency) {
    $store = $currency->getStore(); // instance of App\Currency\Store
    $formatter = $currency->getFormatter('myAwesomeFormat'); // uses config values
    $updater = $currency->getUpdater(); // uses config values
});
```

Development
===========

[](#development)

To run tests:

```
composer install
vendor/bin/phpunit
```

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Recently: every ~27 days

Total

18

Last Release

3017d ago

Major Versions

v1.0.1 → v2.02017-08-02

v2.6 → v3.02017-11-03

v3.4.1 → v4.02018-02-14

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1832804?v=4)[ob-ivan](/maintainers/ob-ivan)[@ob-ivan](https://github.com/ob-ivan)

---

Top Contributors

[![ob-ivan](https://avatars.githubusercontent.com/u/1832804?v=4)](https://github.com/ob-ivan "ob-ivan (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ob-ivan-sd-currency/health.svg)

```
[![Health](https://phpackages.com/badges/ob-ivan-sd-currency/health.svg)](https://phpackages.com/packages/ob-ivan-sd-currency)
```

###  Alternatives

[ckeditor/ckeditor

JavaScript WYSIWYG web text editor.

5244.3M79](/packages/ckeditor-ckeditor)[permafrost-dev/phpcsfixer-preset

shared php-cs-fixer rules &amp; finders preset

226.6k1](/packages/permafrost-dev-phpcsfixer-preset)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
