You are here

class Currency in Currency 7.2

Same name in this branch
  1. 7.2 currency/includes/Currency.inc \Currency
  2. 7.2 currency/vendor/bartfeenstra/currency/src/BartFeenstra/Currency/Currency.php \BartFeenstra\Currency\Currency

Describes a currency.

Hierarchy

Expanded class hierarchy of Currency

2 files declare their use of Currency
CurrencyTest.php in currency/vendor/bartfeenstra/currency/src/BartFeenstra/Tests/Currency/CurrencyTest.php
Contains class \BartFeenstra\Tests\Currency\CurrencyTest.
ResourcesTest.php in currency/vendor/bartfeenstra/currency/src/BartFeenstra/Tests/Currency/ResourcesTest.php
Contains class \BartFeenstra\Tests\Currency\Resources.
26 string references to 'Currency'
currency.inc in currency/ctools/plugins/export_ui/currency.inc
CurrencyAmountFormElementWebTestCase::getInfo in currency/tests/CurrencyAmountFormElementWebTestCase.test
Implements DrupalTestCase::getInfo().
CurrencyAmountViewsHandlerFieldWebTestCase::getInfo in currency/tests/CurrencyAmountViewsHandlerFieldWebTestCase.test
Implements DrupalTestCase::getInfo().
CurrencyCRUDWebTestCase::getInfo in currency/tests/CurrencyCRUDWebTestCase.test
Implements DrupalTestCase::getInfo().
CurrencyExchangerBartFeenstraCurrencyWebTestCase::getInfo in currency/tests/CurrencyConverterBartFeenstraCurrencyWebTestCase.test
Implements DrupalTestCase::getInfo().

... See full list

File

currency/vendor/bartfeenstra/currency/src/BartFeenstra/Currency/Currency.php, line 15
Contains class \BartFeenstra\Currency\Currency.

Namespace

BartFeenstra\Currency
View source
class Currency {

  /**
   * Alternative (non-official) currency signs.
   *
   * @var array
   *   An array of strings that are similar to Currency::sign.
   */
  public $alternativeSigns = array();

  /**
   * Exchange rates to other currencies.
   *
   * @var array
   *   Keys are ISO 4217 codes, values are numeric strings.
   */
  public $exchangeRates = array();

  /**
   * ISO 4217 currency code.
   *
   * @var string
   */
  public $ISO4217Code = NULL;

  /**
   * ISO 4217 currency number.
   *
   * @var string
   */
  public $ISO4217Number = NULL;

  /**
   * The currency's official sign, such as '€' or '$'.
   *
   * @var string
   */
  public $sign = '¤';

  /**
   * The number of subunits this currency has.
   *
   * @var integer|null
   */
  public $subunits = NULL;

  /**
   * Human-readable title in US English.
   *
   * @var string
   */
  public $title = NULL;

  /**
   * This currency's usage.
   *
   * @var array
   *   An array of \BartFeenstra\Currency\Usage objects.
   */
  public $usage = array();

  /**
   * The path to the resources directory.
   */
  public static $resourcePath = '/../../../resources/';

  /**
   * A list of the ISO 4217 codes of all known currency resources.
   */
  public static $resourceISO4217Codes = array();

  /**
   * Returns the directory that contains the currency resources.
   *
   * @return string
   */
  public static function resourceDir() {
    return __DIR__ . self::$resourcePath;
  }

  /**
   * Lists all currency resources in the library.
   *
   * @return array
   *   An array with ISO 4217 currency codes.
   */
  public static function resourceListAll() {
    if (!self::$resourceISO4217Codes) {
      $directory = new \RecursiveDirectoryIterator(self::resourceDir());
      foreach ($directory as $item) {
        if (preg_match('#^...\\.yml$#', $item
          ->getFilename())) {
          self::$resourceISO4217Codes[] = substr($item
            ->getFilename(), 0, 3);
        }
      }
    }
    return self::$resourceISO4217Codes;
  }

  /**
   * Loads a currency resource into this object.
   *
   * @param string $iso_4217_code
   */
  public function resourceLoad($iso_4217_code) {
    $filepath = self::resourceDir() . "{$iso_4217_code}.yml";
    if (is_readable($filepath)) {
      $this
        ->resourceParse(file_get_contents($filepath));
    }
    else {
      throw new \RuntimeException(sprintf('The currency resource file %s does not exist or is not readable.', $filepath));
    }
  }

  /**
   * Parses a YAML file into this object.
   *
   * @param string $yaml
   */
  public function resourceParse($yaml) {
    $currency_data = Yaml::parse($yaml);
    $usages_data = $currency_data['usage'];
    $currency_data['usage'] = array();
    foreach ($currency_data as $property => $value) {
      $this->{$property} = $value;
    }
    foreach ($usages_data as $usage_data) {
      $usage = new Usage();
      foreach ($usage_data as $property => $value) {
        $usage->{$property} = $value;
      }
      $this->usage[] = $usage;
    }
    unset($currency_data);
  }

  /**
   * Dumps this object to YAML code.
   *
   * @return string
   */
  public function resourceDump() {
    $currency_data = get_object_vars($this);
    $currency_data['usage'] = array();
    foreach ($this->usage as $usage) {
      $currency_data['usage'][] = get_object_vars($usage);
    }
    return Yaml::dump($currency_data);
  }

  /**
   * Returns the number of decimals.
   *
   * @return int
   */
  function getDecimals() {
    $decimals = 0;
    if ($this->subunits > 0) {
      $decimals = 1;
      while (pow(10, $decimals) < $this->subunits) {
        $decimals++;
      }
    }
    return $decimals;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Currency::$alternativeSigns public property Alternative (non-official) currency signs.
Currency::$exchangeRates public property Exchange rates to other currencies.
Currency::$ISO4217Code public property ISO 4217 currency code.
Currency::$ISO4217Number public property ISO 4217 currency number.
Currency::$resourceISO4217Codes public static property A list of the ISO 4217 codes of all known currency resources.
Currency::$resourcePath public static property The path to the resources directory.
Currency::$sign public property The currency's official sign, such as '€' or '$'.
Currency::$subunits public property The number of subunits this currency has.
Currency::$title public property Human-readable title in US English.
Currency::$usage public property This currency's usage.
Currency::getDecimals function Returns the number of decimals.
Currency::resourceDir public static function Returns the directory that contains the currency resources.
Currency::resourceDump public function Dumps this object to YAML code.
Currency::resourceListAll public static function Lists all currency resources in the library.
Currency::resourceLoad public function Loads a currency resource into this object.
Currency::resourceParse public function Parses a YAML file into this object.