mrzard/open-exchange-rates-bundle

OpenExchangeRates API exposure for Symfony2

Installs: 4 832

Dependents: 0

Suggesters: 0

Security: 0

Stars: 9

Watchers: 2

Forks: 5

Open Issues: 0

Type:symfony-bundle

v3.0.1 2017-12-01 14:27 UTC

This package is not auto-updated.

Last update: 2024-04-13 13:39:33 UTC


README

SensioLabsInsight Scrutinizer Code Quality Build Status

Version families

Branch 2.0.0 will give support to Symfony >= 2.3

Branch 3.0.0 will give support to Symfony >=3 (WIP)

Installation

$ php composer.phar require mrzard/open-exchange-rates-bundle ~2.0.0

And register the bundle in your AppKernel.php file

return array(
   // ...
   new Mrzard\OpenExchangeRatesBundle\OpenExchangeRatesBundle(),
   // ...
);

Configuration

You will have to define your api id in the parameters.yml file of you environment.

Then, add this to your services.yml file

open_exchange_rates:
    api_id: %YOUR_API_ID_PARAM%
    api_configuration:
        https: true|false #defaults to false
        base_currency: XXX #defaults to USD

If you're using a free version, you won't need to change the https or base_currency as they only work fror Enterprise/Unlimited accounts

Usage

You can access the service by getting it from the container under open_exchange_rates_service

Keep in mind that some options will only work properly with an Enterprise/Unlimited Plan

Free features

Get latest exchange rates

/**
 * Get the latest exchange rates
 *
 * @param array  $symbols Currency codes to get the rates for. Default all
 * @param string $base    Base currency, default NULL (gets it from config)
 *
 * @return array
 */
public function getLatest($symbols = array, $base = null)
{
}

Only use the $symbols and $base parameters if you have an Enterprise or Unlimited plan.

Output:

array (size=5)
  'disclaimer' => string 'Exchange rates...'
  'license' => string 'Data sourced from...'
  'timestamp' => int 1395396061
  'base' => string 'USD' (length=3)
  'rates' =>
    array (size=166)
      'AED' => float 3.672721
      'AFN' => float 56.747225
      'ALL' => float 101.7573
      'AMD' => float 417.366998
      ...
    )
)

Get available currencies

/**
 * Gets a list of all available currencies
 *
 * @return array with keys = ISO codes, content = Currency Name
 */
public function getCurrencies()
{
}

Output:

array (size=5)
  'AED' => 'United Arab Emirates Dirham'
  'AFN' => 'Afghan Afghani'
  'ALL' => 'Albanian Lek'
  'AMD' => 'Armenian Dram'
  'ANG' => 'Netherlands Antillean Guilder'
  ...
)

Get historical data for a date

/**
 * Get historical data
 *
 * @param \DateTime $date
 */
public function getHistorical(\DateTime $date)
{
}

Output:

array (size=5)
  'disclaimer' => string 'Exchange rates...'
  'license' => string 'Data sourced from...'
  'timestamp' => int 1388617200
  'base' => string 'USD' (length=3)
  'rates' =>
    array (size=166)
      'AED' => float 3.672524
      'AFN' => float 56.0846
      'ALL' => float 102.06575
      'AMD' => float 408.448002
      'ANG' => float 1.78902
      'AOA' => float 97.598401
      'ARS' => float 6.51658
      'AUD' => float 1.124795
      'AWG' => float 1.789775
      'AZN' => float 0.7841
      'BAM' => float 1.421715
      'BBD' => int 2
      ...
    )
)

Developer / Unlimited features

Get the latest exchange rates, limiting the return array

$openExchangeRatesService->getLatest(['EUR', 'USD', 'COP']);

Output:

array (size=5)
  'disclaimer' => string 'Exchange rates ...'
  'license' => string 'Data sourced...'
  'timestamp' => int 1395396061
  'base' => string 'USD' (length=3)
  'rates' =>
    array (size=3)
      'EUR' => ...,
      'USD' => ...,
      'COP' => ...
    )
)

You can also change the base currency used to get the latest exchange rates with the second parameter

Directly convert a quantity between currencies

$openExchangeRatesService->convert(10, 'USD', 'EUR');