/** * Hydrate an array to a money object * * This hydrator breaks the hydration pattern as it returns a new instance * As money objects are immutable, the object given in $object cannot be changed * * @param array $data * @param mixed $object * @return Money */ public function hydrate(array $data, $object) { $currency = new Currency($data['currency']); $amount = (int) ($data['amount'] * $currency->getSubUnit()); $object = new Money($amount, $currency); return $object; }
/** * Constructor populates allowed codes with defaults and optionally filters those passed as an argument * @param array|NULL $allow An array of currency codes to allow * @param array|NULL $deny An array of currency codes to remove from the allowed list, or remove from the defaults if no allow is set * @return void */ public function __construct($allow = NULL, $deny = NULL) { $this->defaults = Currency::getAvailableCurrencies(); if (NULL !== $allow) { $this->setAllow($allow); } if (NULL !== $deny) { $this->remove($deny); } }
/** * @return array */ public function getValueOptions() { if (!count($this->valueOptions)) { $names = Currency::getAvailableCurrencyNames(); $codes = $this->getCurrencyList()->getAllow(); $options = parent::getValueOptions(); foreach ($codes as $code) { $name = $this->getDisplayNames() ? $names[$code] : $code; $options[$code] = $name; } $this->setValueOptions($options); } return parent::getValueOptions(); }
/** * Returns the currency code of the monetary value represented by this * object. * * @return string */ public function getCurrencyCode() { return $this->currency->getCurrencyCode(); }
/** * covers \NetglueMoney\Money\Currency::getAvailableCurrencyNames */ public function testGetAvailableCurencyNames() { $array = Currency::getAvailableCurrencyNames(); $this->assertInternalType('array', $array); $this->assertContainsOnly('string', $array); foreach ($array as $code => $name) { $this->assertRegExp('/^[A-Z]{3}$/', $code); } }