CurrencyRepository.php in Price 3.0.x
File
src/Repository/CurrencyRepository.php
View source
<?php
namespace Drupal\price\Repository;
use CommerceGuys\Intl\Currency\Currency;
use CommerceGuys\Intl\Currency\CurrencyRepositoryInterface;
use CommerceGuys\Intl\Exception\UnknownCurrencyException;
use Drupal\price\Entity\CurrencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class CurrencyRepository implements CurrencyRepositoryInterface {
protected $currencyStorage;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->currencyStorage = $entity_type_manager
->getStorage('price_currency');
}
public function get($currency_code, $locale = NULL) {
$currency = $this->currencyStorage
->load($currency_code);
if (!$currency) {
throw new UnknownCurrencyException($currency_code);
}
return $this
->createValueObjectFromEntity($currency);
}
public function getAll($locale = NULL) {
$all = [];
$currencies = $this->currencyStorage
->loadMultiple();
foreach ($currencies as $currency_code => $currency) {
$all[$currency_code] = $this
->createValueObjectFromEntity($currency);
}
return $all;
}
public function getList($locale = NULL) {
$list = [];
$currencies = $this->currencyStorage
->loadMultiple();
foreach ($currencies as $currency_code => $currency) {
$list[$currency_code] = $currency
->getName();
}
return $list;
}
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(),
]);
}
}