Sons of PHP
Source Code
  • 🏠Home
  • Contributor Covenant Code of Conduct
  • Getting Help
  • Reporting Issues
  • Security Policy
  • 🪈Bard
    • Overview
    • Commands
  • Symfony Bundles
    • Feature Toggle
  • Contracts
    • Contracts Overview
    • Common
    • Cookie
    • CQRS
    • Filesystem
    • Mailer
    • Pager
    • Registry
    • State Machine
  • 📦Components
    • Assert
    • Cache
      • Adapters
      • Marshallers
    • Clock
    • Container
    • Cookie
    • CQRS
    • Event Dispatcher
    • Event Sourcing
      • Aggregates
        • Aggregate Repository
      • Event Messages
        • Using the Serializable Event Message
        • Message Enrichers
        • Message Serializers
        • Message Repository
        • Message Upcasters
    • Feature Toggle
    • Filesystem
      • Adapters
    • Http Factory
    • Http Handler
    • Http Message
    • JSON
    • Link
    • Logger
      • Handlers
      • Enrichers
      • Filters
      • Formatters
    • Mailer
      • Transports
    • Money
      • Currency Providers
      • Operators
      • Queries
    • Pager
      • Adapters
    • Registry
    • State Machine
    • Version
  • 💁Contributing
    • Contributing Overview
    • Contributing Code
    • Discussions
    • Documentation
Powered by GitBook
On this page
  • Basic Usage
  • Loop over Currencies
  • Check if Provider has a Currency
  • Getting Currency from the Provider
  • Currency Providers
  • CurrencyProvider
  • XCurrencyProvider
  • ChainCurrencyProvider

Was this helpful?

Edit on GitHub
  1. Components
  2. Money

Currency Providers

The Currency Provider allows you to grab all the known Currencies. It also has a query method to pass in CurrencyProviderQuerys to return various results.

Basic Usage

Loop over Currencies

<?php
// @var CurrencyInterface $currency
foreach ($provider->getCurrencies() as $currency) {
    // Do something with each currency
}

Check if Provider has a Currency

// Check to see if the provider has the currency you are looking for
// @var bool $hasCurrency
$hasCurrency = $provider->hasCurrency('USD');
$hasCurrency = $provider->hasCurrency(Currency::USD());

Getting Currency from the Provider

// You can get a Currency like this. If the Currency does not exist
// it will throw an exception.
// @var CurrencyInterface $currency
$currency = $provider->getCurrency('USD');

$code      = $currency->getCurrencyCode(); // "USD"
$numCode   = $currency->getNumericCode(); // 840
$minorUnit = $currency->getMinorUnit(); // 2

Currency Providers

CurrencyProvider

<?php
use SonsOfPHP\Component\Money\CurrencyProvider\CurrencyProvider;

$provider = new CurrencyProvider();

XCurrencyProvider

The XCurrencyProvider gives you access to all of the "X" Currencies. For example, this provider is where you will find the Currency "XTS" which is reserved for use in testing.

<?php
use SonsOfPHP\Component\Money\CurrencyProvider\XCurrencyProvider;

$provider = new XCurrencyProvider();

ChainCurrencyProvider

The ChainCurrencyProvider lets you use multiple Currency Providers together at once. It does not provide any currencies itself.

<?php
use SonsOfPHP\Component\Money\CurrencyProvider\ChainCurrencyProvider;
use SonsOfPHP\Component\Money\CurrencyProvider\CurrencyProvider;
use SonsOfPHP\Component\Money\CurrencyProvider\XCurrencyProvider;

$provider = new ChainCurrencyProvider([
    new CurrencyProvider(),
    new XCurrencyProvider(),
]);

// You can add additional providers as well
$provider->addProvier(new MyCustomCurrencyProvider());
PreviousMoneyNextOperators

Last updated 8 months ago

Was this helpful?

The CurrencyProvider is usually the one that you will be using most of the time. It provides most of the currencies.

📦
ISO 4217