2017 © Pedro Peláez
 

library sd-currency

image

ob-ivan/sd-currency

  • Tuesday, March 20, 2018
  • by ob-ivan
  • Repository
  • 1 Watchers
  • 0 Stars
  • 493 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 19 Versions
  • 55 % Grown

The README.md

A collection of utility functions to enumerate available currencies and update their actual exchange rates., (*1)

Installation

composer require ob-ivan/sd-currency

Usage

Registry

A currency instance associates currency code with its representations in Unicode and as an HTML entity., (*2)

These currencies are supported: - RUB - EUR - USD, (*3)

You can enumerate available currencies using a registry instance:, (*4)

use SD\Currency\Model\Registry;

$registry = new Registry();
foreach ($registry->getAll() as $currency) {
    print "

The Unicode symbol for {$currency->getCode()} is {$currency->getUnicode()}, (*5)

\n"; }

Or if you use dependency injection (see below):, (*6)

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

Formatter

Use formatter service to format price according to given currency. Formatter instance uses a number of configuration parameters., (*7)

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 & Store

All functions are available within repository instance which acts as a library facade:, (*8)

use SD\Currency\Repository;

$repository = new Repository($config);

Some of its methods require a store class to be configured:, (*9)

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)., (*10)

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., (*11)

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., (*12)

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., (*13)

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:, (*14)

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

Dependency Injection

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

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

When using dependency injection you may provide a configuration file to set up your services:, (*16)

# 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:, (*17)

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:, (*18)

$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

To run tests:, (*19)

composer install
vendor/bin/phpunit

The Versions

20/03 2018

dev-master

9999999-dev

  Sources   Download

The Requires

 

The Development Requires

20/03 2018
14/02 2018
25/01 2018

v3.4.1

3.4.1.0

  Sources   Download

The Requires

 

The Development Requires

01/12 2017

v3.4

3.4.0.0

  Sources   Download

The Requires

 

The Development Requires

30/11 2017

v3.3

3.3.0.0

  Sources   Download

The Requires

 

The Development Requires

29/11 2017

v3.2.1

3.2.1.0

  Sources   Download

The Requires

 

The Development Requires

29/11 2017

v3.2

3.2.0.0

  Sources   Download

The Requires

 

The Development Requires

03/11 2017

v3.1

3.1.0.0

  Sources   Download

The Requires

 

The Development Requires

03/11 2017

v3.0

3.0.0.0

  Sources   Download

The Requires

 

The Development Requires

31/10 2017

v2.6

2.6.0.0

  Sources   Download

The Requires

 

The Development Requires

27/09 2017

v2.5

2.5.0.0

  Sources   Download

The Requires

 

The Development Requires

15/08 2017

v2.4

2.4.0.0

  Sources   Download

The Requires

 

The Development Requires

14/08 2017

v2.3

2.3.0.0

  Sources   Download

The Requires

 

The Development Requires

02/08 2017

v2.2

2.2.0.0

  Sources   Download

The Requires

 

The Development Requires

02/08 2017

v2.1

2.1.0.0

  Sources   Download

The Requires

 

The Development Requires

02/08 2017

v2.0

2.0.0.0

  Sources   Download

The Requires

 

The Development Requires

31/07 2017

v1.0.1

1.0.1.0

  Sources   Download

31/07 2017

v1.0

1.0.0.0

  Sources   Download