You are here

class CurrencyRepository in Price 8

Same name and namespace in other branches
  1. 3.x src/Repository/CurrencyRepository.php \Drupal\price\Repository\CurrencyRepository
  2. 2.0.x src/Repository/CurrencyRepository.php \Drupal\price\Repository\CurrencyRepository
  3. 2.x src/Repository/CurrencyRepository.php \Drupal\price\Repository\CurrencyRepository
  4. 3.0.x src/Repository/CurrencyRepository.php \Drupal\price\Repository\CurrencyRepository

Defines the currency repository.

Provides currencies to the CurrencyFormatter in the expected format, loaded from Drupal's currency storage (price_currency entities).

Note: This repository doesn't support loading currencies in a non-default locale, since it would be imprecise to map $locale to Drupal's languages.

Hierarchy

  • class \Drupal\price\Repository\CurrencyRepository implements \CommerceGuys\Intl\Currency\CurrencyRepositoryInterface

Expanded class hierarchy of CurrencyRepository

1 string reference to 'CurrencyRepository'
price.services.yml in ./price.services.yml
price.services.yml
1 service uses CurrencyRepository
price.currency_repository in ./price.services.yml
Drupal\price\Repository\CurrencyRepository

File

src/Repository/CurrencyRepository.php, line 20

Namespace

Drupal\price\Repository
View source
class CurrencyRepository implements CurrencyRepositoryInterface {

  /**
   * The currency storage.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected $currencyStorage;

  /**
   * Creates an CurrencyRepository instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->currencyStorage = $entity_type_manager
      ->getStorage('price_currency');
  }

  /**
   * {@inheritdoc}
   */
  public function get($currency_code, $locale = NULL) {

    /** @var \Drupal\price\Entity\CurrencyInterface $currency */
    $currency = $this->currencyStorage
      ->load($currency_code);
    if (!$currency) {
      throw new UnknownCurrencyException($currency_code);
    }
    return $this
      ->createValueObjectFromEntity($currency);
  }

  /**
   * {@inheritdoc}
   */
  public function getAll($locale = NULL) {
    $all = [];

    /** @var \Drupal\price\Entity\CurrencyInterface[] $currencies */
    $currencies = $this->currencyStorage
      ->loadMultiple();
    foreach ($currencies as $currency_code => $currency) {
      $all[$currency_code] = $this
        ->createValueObjectFromEntity($currency);
    }
    return $all;
  }

  /**
   * {@inheritdoc}
   */
  public function getList($locale = NULL) {
    $list = [];

    /** @var \Drupal\price\Entity\CurrencyInterface[] $entities */
    $currencies = $this->currencyStorage
      ->loadMultiple();
    foreach ($currencies as $currency_code => $currency) {
      $list[$currency_code] = $currency
        ->getName();
    }
    return $list;
  }

  /**
   * Creates a currency value object from the given entity.
   *
   * @param \Drupal\price\Entity\CurrencyInterface $currency
   *   The currency entity.
   *
   * @return \CommerceGuys\Intl\Currency\Currency
   *   The currency value object.
   */
  protected function createValueObjectFromEntity(CurrencyInterface $currency) {
    return new Currency([
      'currency_code' => $currency
        ->getCurrencyCode(),
      'name' => $currency
        ->getName(),
      'numeric_code' => $currency
        ->getNumericCode(),
      'symbol' => $currency
        ->getSymbol(),
      'fraction_digits' => $currency
        ->getFractionDigits(),
      'locale' => $currency
        ->language()
        ->getId(),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencyRepository::$currencyStorage protected property The currency storage.
CurrencyRepository::createValueObjectFromEntity protected function Creates a currency value object from the given entity.
CurrencyRepository::get public function
CurrencyRepository::getAll public function
CurrencyRepository::getList public function
CurrencyRepository::__construct public function Creates an CurrencyRepository instance.