Currency.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Currency
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * include needed classes
  23. */
  24. require_once 'Zend/Locale.php';
  25. require_once 'Zend/Locale/Data.php';
  26. require_once 'Zend/Locale/Format.php';
  27. /**
  28. * Class for handling currency notations
  29. *
  30. * @category Zend
  31. * @package Zend_Currency
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Currency
  36. {
  37. // Constants for defining what currency symbol should be displayed
  38. const NO_SYMBOL = 1;
  39. const USE_SYMBOL = 2;
  40. const USE_SHORTNAME = 3;
  41. const USE_NAME = 4;
  42. // Constants for defining the position of the currencysign
  43. const STANDARD = 8;
  44. const RIGHT = 16;
  45. const LEFT = 32;
  46. /**
  47. * Options array
  48. *
  49. * The following options are available
  50. * 'position' => Position for the currency sign
  51. * 'script' => Script for the output
  52. * 'format' => Locale for numeric output
  53. * 'display' => Currency detail to show
  54. * 'precision' => Precision for the currency
  55. * 'name' => Name for this currency
  56. * 'currency' => 3 lettered international abbreviation
  57. * 'symbol' => Currency symbol
  58. * 'locale' => Locale for this currency
  59. * 'value' => Money value
  60. * 'service' => Exchange service to use
  61. *
  62. * @var array
  63. * @see Zend_Locale
  64. */
  65. protected $_options = array(
  66. 'position' => self::STANDARD,
  67. 'script' => null,
  68. 'format' => null,
  69. 'display' => self::NO_SYMBOL,
  70. 'precision' => 2,
  71. 'name' => null,
  72. 'currency' => null,
  73. 'symbol' => null,
  74. 'locale' => null,
  75. 'value' => 0,
  76. 'service' => null
  77. );
  78. /**
  79. * Creates a currency instance. Every supressed parameter is used from the actual or the given locale.
  80. *
  81. * @param string|array $options OPTIONAL Options array or currency short name
  82. * when string is given
  83. * @param string|Zend_Locale $locale OPTIONAL locale name
  84. * @throws Zend_Currency_Exception When currency is invalid
  85. */
  86. public function __construct($options = null, $locale = null)
  87. {
  88. if (is_array($options)) {
  89. $this->setLocale($locale);
  90. $this->setFormat($options);
  91. } else if (Zend_Locale::isLocale($options, false, false)) {
  92. $this->setLocale($options);
  93. $options = $locale;
  94. } else {
  95. $this->setLocale($locale);
  96. }
  97. // Get currency details
  98. if (!isset($this->_options['currency']) || !is_array($options)) {
  99. $this->_options['currency'] = self::getShortName($options, $this->_options['locale']);
  100. }
  101. if (!isset($this->_options['name']) || !is_array($options)) {
  102. $this->_options['name'] = self::getName($options, $this->_options['locale']);
  103. }
  104. if (!isset($this->_options['symbol']) || !is_array($options)) {
  105. $this->_options['symbol'] = self::getSymbol($options, $this->_options['locale']);
  106. }
  107. if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
  108. require_once 'Zend/Currency/Exception.php';
  109. throw new Zend_Currency_Exception("Currency '$options' not found");
  110. }
  111. // Get the format
  112. if (!empty($this->_options['symbol'])) {
  113. $this->_options['display'] = self::USE_SYMBOL;
  114. } else if (!empty($this->_options['currency'])) {
  115. $this->_options['display'] = self::USE_SHORTNAME;
  116. }
  117. }
  118. /**
  119. * Returns a localized currency string
  120. *
  121. * @param integer|float $value OPTIONAL Currency value
  122. * @param array $options OPTIONAL options to set temporary
  123. * @throws Zend_Currency_Exception When the value is not a number
  124. * @return string
  125. */
  126. public function toCurrency($value = null, array $options = array())
  127. {
  128. if ($value === null) {
  129. if (is_array($value) && isset($options['value'])) {
  130. $value = $options['value'];
  131. } else {
  132. $value = $this->_options['value'];
  133. }
  134. }
  135. if (is_array($value)) {
  136. $options += $value;
  137. if (isset($options['value'])) {
  138. $value = $options['value'];
  139. }
  140. }
  141. // Validate the passed number
  142. if (!(isset($value)) or (is_numeric($value) === false)) {
  143. require_once 'Zend/Currency/Exception.php';
  144. throw new Zend_Currency_Exception("Value '$value' has to be numeric");
  145. }
  146. if (isset($options['currency'])) {
  147. if (!isset($options['locale'])) {
  148. $options['locale'] = $this->_options['locale'];
  149. }
  150. $options['currency'] = self::getShortName($options['currency'], $options['locale']);
  151. $options['name'] = self::getName($options['currency'], $options['locale']);
  152. $options['symbol'] = self::getSymbol($options['currency'], $options['locale']);
  153. }
  154. $options = $this->_checkOptions($options) + $this->_options;
  155. // Format the number
  156. $format = $options['format'];
  157. $locale = $options['locale'];
  158. if (empty($format)) {
  159. $format = Zend_Locale_Data::getContent($locale, 'currencynumber');
  160. } else if (Zend_Locale::isLocale($format, true, false)) {
  161. $locale = $format;
  162. $format = Zend_Locale_Data::getContent($format, 'currencynumber');
  163. }
  164. $original = $value;
  165. $value = Zend_Locale_Format::toNumber($value, array('locale' => $locale,
  166. 'number_format' => $format,
  167. 'precision' => $options['precision']));
  168. if ($options['position'] !== self::STANDARD) {
  169. $value = str_replace('¤', '', $value);
  170. $space = '';
  171. if (iconv_strpos($value, ' ') !== false) {
  172. $value = str_replace(' ', '', $value);
  173. $space = ' ';
  174. }
  175. if ($options['position'] == self::LEFT) {
  176. $value = '¤' . $space . $value;
  177. } else {
  178. $value = $value . $space . '¤';
  179. }
  180. }
  181. // Localize the number digits
  182. if (empty($options['script']) === false) {
  183. $value = Zend_Locale_Format::convertNumerals($value, 'Latn', $options['script']);
  184. }
  185. // Get the sign to be placed next to the number
  186. if (is_numeric($options['display']) === false) {
  187. $sign = $options['display'];
  188. } else {
  189. switch($options['display']) {
  190. case self::USE_SYMBOL:
  191. $sign = $this->_extractPattern($options['symbol'], $original);
  192. break;
  193. case self::USE_SHORTNAME:
  194. $sign = $options['currency'];
  195. break;
  196. case self::USE_NAME:
  197. $sign = $options['name'];
  198. break;
  199. default:
  200. $sign = '';
  201. $value = str_replace(' ', '', $value);
  202. break;
  203. }
  204. }
  205. $value = str_replace('¤', $sign, $value);
  206. return $value;
  207. }
  208. /**
  209. * Internal method to extract the currency pattern
  210. * when a choice is given based on the given value
  211. *
  212. * @param string $pattern
  213. * @param float|integer $value
  214. * @return string
  215. */
  216. private function _extractPattern($pattern, $value)
  217. {
  218. if (strpos($pattern, '|') === false) {
  219. return $pattern;
  220. }
  221. $patterns = explode('|', $pattern);
  222. $token = $pattern;
  223. $value = trim(str_replace('¤', '', $value));
  224. krsort($patterns);
  225. foreach($patterns as $content) {
  226. if (strpos($content, '<') !== false) {
  227. $check = iconv_substr($content, 0, iconv_strpos($content, '<'));
  228. $token = iconv_substr($content, iconv_strpos($content, '<') + 1);
  229. if ($check < $value) {
  230. return $token;
  231. }
  232. } else {
  233. $check = iconv_substr($content, 0, iconv_strpos($content, '≤'));
  234. $token = iconv_substr($content, iconv_strpos($content, '≤') + 1);
  235. if ($check <= $value) {
  236. return $token;
  237. }
  238. }
  239. }
  240. return $token;
  241. }
  242. /**
  243. * Sets the formating options of the localized currency string
  244. * If no parameter is passed, the standard setting of the
  245. * actual set locale will be used
  246. *
  247. * @param array $options (Optional) Options to set
  248. * @return Zend_Currency
  249. */
  250. public function setFormat(array $options = array())
  251. {
  252. $this->_options = $this->_checkOptions($options) + $this->_options;
  253. return $this;
  254. }
  255. /**
  256. * Internal function for checking static given locale parameter
  257. *
  258. * @param string $currency (Optional) Currency name
  259. * @param string|Zend_Locale $locale (Optional) Locale to display informations
  260. * @throws Zend_Currency_Exception When locale contains no region
  261. * @return string The extracted locale representation as string
  262. */
  263. private function _checkParams($currency = null, $locale = null)
  264. {
  265. // Manage the params
  266. if ((empty($locale)) and (!empty($currency)) and
  267. (Zend_Locale::isLocale($currency, true, false))) {
  268. $locale = $currency;
  269. $currency = null;
  270. }
  271. // Validate the locale and get the country short name
  272. $country = null;
  273. if ((Zend_Locale::isLocale($locale, true, false)) and (strlen($locale) > 4)) {
  274. $country = substr($locale, (strpos($locale, '_') + 1));
  275. } else {
  276. require_once 'Zend/Currency/Exception.php';
  277. throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
  278. }
  279. // Get the available currencies for this country
  280. $data = Zend_Locale_Data::getContent($locale, 'currencytoregion', $country);
  281. if ((empty($currency) === false) and (empty($data) === false)) {
  282. $abbreviation = $currency;
  283. } else {
  284. $abbreviation = $data;
  285. }
  286. return array('locale' => $locale, 'currency' => $currency, 'name' => $abbreviation, 'country' => $country);
  287. }
  288. /**
  289. * Returns the actual or details of other currency symbols,
  290. * when no symbol is available it returns the currency shortname (f.e. FIM for Finnian Mark)
  291. *
  292. * @param string $currency (Optional) Currency name
  293. * @param string|Zend_Locale $locale (Optional) Locale to display informations
  294. * @return string
  295. */
  296. public function getSymbol($currency = null, $locale = null)
  297. {
  298. if (($currency === null) and ($locale === null)) {
  299. return $this->_options['symbol'];
  300. }
  301. $params = self::_checkParams($currency, $locale);
  302. // Get the symbol
  303. $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['currency']);
  304. if (empty($symbol) === true) {
  305. $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['name']);
  306. }
  307. if (empty($symbol) === true) {
  308. return null;
  309. }
  310. return $symbol;
  311. }
  312. /**
  313. * Returns the actual or details of other currency shortnames
  314. *
  315. * @param string $currency OPTIONAL Currency's name
  316. * @param string|Zend_Locale $locale OPTIONAL The locale
  317. * @return string
  318. */
  319. public function getShortName($currency = null, $locale = null)
  320. {
  321. if (($currency === null) and ($locale === null)) {
  322. return $this->_options['currency'];
  323. }
  324. $params = self::_checkParams($currency, $locale);
  325. // Get the shortname
  326. if (empty($params['currency']) === true) {
  327. return $params['name'];
  328. }
  329. $list = Zend_Locale_Data::getContent($params['locale'], 'currencytoname', $params['currency']);
  330. if (empty($list) === true) {
  331. $list = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
  332. if (empty($list) === false) {
  333. $list = $params['currency'];
  334. }
  335. }
  336. if (empty($list) === true) {
  337. return null;
  338. }
  339. return $list;
  340. }
  341. /**
  342. * Returns the actual or details of other currency names
  343. *
  344. * @param string $currency (Optional) Currency's short name
  345. * @param string|Zend_Locale $locale (Optional) The locale
  346. * @return string
  347. */
  348. public function getName($currency = null, $locale = null)
  349. {
  350. if (($currency === null) and ($locale === null)) {
  351. return $this->_options['name'];
  352. }
  353. $params = self::_checkParams($currency, $locale);
  354. // Get the name
  355. $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
  356. if (empty($name) === true) {
  357. $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['name']);
  358. }
  359. if (empty($name) === true) {
  360. return null;
  361. }
  362. return $name;
  363. }
  364. /**
  365. * Returns a list of regions where this currency is or was known
  366. *
  367. * @param string $currency OPTIONAL Currency's short name
  368. * @throws Zend_Currency_Exception When no currency was defined
  369. * @return array List of regions
  370. */
  371. public function getRegionList($currency = null)
  372. {
  373. if ($currency === null) {
  374. $currency = $this->_options['currency'];
  375. }
  376. if (empty($currency) === true) {
  377. require_once 'Zend/Currency/Exception.php';
  378. throw new Zend_Currency_Exception('No currency defined');
  379. }
  380. $data = Zend_Locale_Data::getContent('', 'regiontocurrency', $currency);
  381. $result = explode(' ', $data);
  382. return $result;
  383. }
  384. /**
  385. * Returns a list of currencies which are used in this region
  386. * a region name should be 2 charachters only (f.e. EG, DE, US)
  387. * If no region is given, the actual region is used
  388. *
  389. * @param string $region OPTIONAL Region to return the currencies for
  390. * @return array List of currencies
  391. */
  392. public function getCurrencyList($region = null)
  393. {
  394. if (empty($region) === true) {
  395. if (strlen($this->_options['locale']) > 4) {
  396. $region = substr($this->_options['locale'], (strpos($this->_options['locale'], '_') + 1));
  397. }
  398. }
  399. return Zend_Locale_Data::getList('', 'regiontocurrency', $region);
  400. }
  401. /**
  402. * Returns the actual currency name
  403. *
  404. * @return string
  405. */
  406. public function toString()
  407. {
  408. return $this->toCurrency();
  409. }
  410. /**
  411. * Returns the currency name
  412. *
  413. * @return string
  414. */
  415. public function __toString()
  416. {
  417. return $this->toString();
  418. }
  419. /**
  420. * Returns the set cache
  421. *
  422. * @return Zend_Cache_Core The set cache
  423. */
  424. public static function getCache()
  425. {
  426. $cache = Zend_Locale_Data::getCache();
  427. return $cache;
  428. }
  429. /**
  430. * Sets a cache for Zend_Currency
  431. *
  432. * @param Zend_Cache_Core $cache Cache to set
  433. * @return void
  434. */
  435. public static function setCache(Zend_Cache_Core $cache)
  436. {
  437. Zend_Locale_Data::setCache($cache);
  438. }
  439. /**
  440. * Returns true when a cache is set
  441. *
  442. * @return boolean
  443. */
  444. public static function hasCache()
  445. {
  446. return Zend_Locale_Data::hasCache();
  447. }
  448. /**
  449. * Removes any set cache
  450. *
  451. * @return void
  452. */
  453. public static function removeCache()
  454. {
  455. Zend_Locale_Data::removeCache();
  456. }
  457. /**
  458. * Clears all set cache data
  459. *
  460. * @return void
  461. */
  462. public static function clearCache()
  463. {
  464. Zend_Locale_Data::clearCache();
  465. }
  466. /**
  467. * Sets a new locale for data retreivement
  468. * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
  469. * 'xx_YY' will be set to 'root' because 'xx' does not exist
  470. *
  471. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  472. * @throws Zend_Currency_Exception When the given locale does not exist
  473. * @return Zend_Currency Provides fluent interface
  474. */
  475. public function setLocale($locale = null)
  476. {
  477. require_once 'Zend/Locale.php';
  478. try {
  479. $locale = Zend_Locale::findLocale($locale);
  480. if (strlen($locale) > 4) {
  481. $this->_options['locale'] = $locale;
  482. } else {
  483. require_once 'Zend/Currency/Exception.php';
  484. throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
  485. }
  486. } catch (Zend_Locale_Exception $e) {
  487. require_once 'Zend/Currency/Exception.php';
  488. throw new Zend_Currency_Exception($e->getMessage());
  489. }
  490. // Get currency details
  491. $this->_options['currency'] = $this->getShortName(null, $this->_options['locale']);
  492. $this->_options['name'] = $this->getName(null, $this->_options['locale']);
  493. $this->_options['symbol'] = $this->getSymbol(null, $this->_options['locale']);
  494. return $this;
  495. }
  496. /**
  497. * Returns the actual set locale
  498. *
  499. * @return string
  500. */
  501. public function getLocale()
  502. {
  503. return $this->_options['locale'];
  504. }
  505. /**
  506. * Returns the value
  507. *
  508. * @return float
  509. */
  510. public function getValue()
  511. {
  512. return $this->_options['value'];
  513. }
  514. /**
  515. * Adds a currency
  516. *
  517. * @param float|integer|Zend_Currency $value Add this value to currency
  518. * @param string|Zend_Currency $currency The currency to add
  519. * @return Zend_Currency
  520. */
  521. public function setValue($value, $currency = null)
  522. {
  523. $this->_options['value'] = $this->_exchangeCurrency($value, $currency);
  524. return $this;
  525. }
  526. /**
  527. * Adds a currency
  528. *
  529. * @param float|integer|Zend_Currency $value Add this value to currency
  530. * @param string|Zend_Currency $currency The currency to add
  531. * @return Zend_Currency
  532. */
  533. public function add($value, $currency = null)
  534. {
  535. $value = $this->_exchangeCurrency($value, $currency);
  536. $this->_options['value'] += (float) $value;
  537. return $this;
  538. }
  539. /**
  540. * Substracts a currency
  541. *
  542. * @param float|integer|Zend_Currency $value Substracts this value from currency
  543. * @param string|Zend_Currency $currency The currency to substract
  544. * @return Zend_Currency
  545. */
  546. public function sub($value, $currency = null)
  547. {
  548. $value = $this->_exchangeCurrency($value, $currency);
  549. $this->_options['value'] -= (float) $value;
  550. return $this;
  551. }
  552. /**
  553. * Divides a currency
  554. *
  555. * @param float|integer|Zend_Currency $value Divides this value from currency
  556. * @param string|Zend_Currency $currency The currency to divide
  557. * @return Zend_Currency
  558. */
  559. public function div($value, $currency = null)
  560. {
  561. $value = $this->_exchangeCurrency($value, $currency);
  562. $this->_options['value'] /= (float) $value;
  563. return $this;
  564. }
  565. /**
  566. * Multiplies a currency
  567. *
  568. * @param float|integer|Zend_Currency $value Multiplies this value from currency
  569. * @param string|Zend_Currency $currency The currency to multiply
  570. * @return Zend_Currency
  571. */
  572. public function mul($value, $currency = null)
  573. {
  574. $value = $this->_exchangeCurrency($value, $currency);
  575. $this->_options['value'] *= (float) $value;
  576. return $this;
  577. }
  578. /**
  579. * Calculates the modulo from a currency
  580. *
  581. * @param float|integer|Zend_Currency $value Calculate modulo from this value
  582. * @param string|Zend_Currency $currency The currency to calculate the modulo
  583. * @return Zend_Currency
  584. */
  585. public function mod($value, $currency = null)
  586. {
  587. $value = $this->_exchangeCurrency($value, $currency);
  588. $this->_options['value'] %= (float) $value;
  589. return $this;
  590. }
  591. /**
  592. * Compares two currencies
  593. *
  594. * @param float|integer|Zend_Currency $value Compares the currency with this value
  595. * @param string|Zend_Currency $currency The currency to compare this value from
  596. * @return Zend_Currency
  597. */
  598. public function compare($value, $currency = null)
  599. {
  600. $value = $this->_exchangeCurrency($value, $currency);
  601. $value = $this->_options['value'] - $value;
  602. if ($value < 0) {
  603. return -1;
  604. } else if ($value > 0) {
  605. return 1;
  606. }
  607. return 0;
  608. }
  609. /**
  610. * Returns true when the two currencies are equal
  611. *
  612. * @param float|integer|Zend_Currency $value Compares the currency with this value
  613. * @param string|Zend_Currency $currency The currency to compare this value from
  614. * @return boolean
  615. */
  616. public function equals($value, $currency = null)
  617. {
  618. $value = $this->_exchangeCurrency($value, $currency);
  619. if ($this->_options['value'] == $value) {
  620. return true;
  621. }
  622. return false;
  623. }
  624. /**
  625. * Returns true when the currency is more than the given value
  626. *
  627. * @param float|integer|Zend_Currency $value Compares the currency with this value
  628. * @param string|Zend_Currency $currency The currency to compare this value from
  629. * @return boolean
  630. */
  631. public function isMore($value, $currency = null)
  632. {
  633. $value = $this->_exchangeCurrency($value, $currency);
  634. if ($this->_options['value'] > $value) {
  635. return true;
  636. }
  637. return false;
  638. }
  639. /**
  640. * Returns true when the currency is less than the given value
  641. *
  642. * @param float|integer|Zend_Currency $value Compares the currency with this value
  643. * @param string|Zend_Currency $currency The currency to compare this value from
  644. * @return boolean
  645. */
  646. public function isLess($value, $currency = null)
  647. {
  648. $value = $this->_exchangeCurrency($value, $currency);
  649. if ($this->_options['value'] < $value) {
  650. return true;
  651. }
  652. return false;
  653. }
  654. /**
  655. * Internal method which calculates the exchanges currency
  656. *
  657. * @param float|integer|Zend_Currency $value Compares the currency with this value
  658. * @param string|Zend_Currency $currency The currency to compare this value from
  659. * @return unknown
  660. */
  661. protected function _exchangeCurrency($value, $currency)
  662. {
  663. if ($value instanceof Zend_Currency) {
  664. $currency = $value->getShortName();
  665. $value = $value->getValue();
  666. } else {
  667. $currency = $this->getShortName($currency, $this->getLocale());
  668. }
  669. $rate = 1;
  670. if ($currency !== $this->getShortName()) {
  671. $service = $this->getService();
  672. if (!($service instanceof Zend_Currency_CurrencyInterface)) {
  673. require_once 'Zend/Currency/Exception.php';
  674. throw new Zend_Currency_Exception('No exchange service applied');
  675. }
  676. $rate = $service->getRate($this->getShortName(), $currency);
  677. }
  678. $value *= $rate;
  679. return $value;
  680. }
  681. /**
  682. * Returns the set service class
  683. *
  684. * @return Zend_Service
  685. */
  686. public function getService()
  687. {
  688. return $this->_options['service'];
  689. }
  690. /**
  691. * Sets a new exchange service
  692. *
  693. * @param string|Zend_Currency_CurrencyInterface $service Service class
  694. * @return Zend_Currency
  695. */
  696. public function setService($service)
  697. {
  698. if (is_string($service)) {
  699. require_once 'Zend/Loader.php';
  700. if (!class_exists($service)) {
  701. $file = str_replace('_', DIRECTORY_SEPARATOR, $service) . '.php';
  702. if (Zend_Loader::isReadable($file)) {
  703. Zend_Loader::loadClass($class);
  704. }
  705. }
  706. $service = new $service;
  707. }
  708. if (!($service instanceof Zend_Currency_CurrencyInterface)) {
  709. require_once 'Zend/Currency/Exception.php';
  710. throw new Zend_Currency_Exception('A currency service must implement Zend_Currency_CurrencyInterface');
  711. }
  712. $this->_options['service'] = $service;
  713. return $this;
  714. }
  715. /**
  716. * Internal method for checking the options array
  717. *
  718. * @param array $options Options to check
  719. * @throws Zend_Currency_Exception On unknown position
  720. * @throws Zend_Currency_Exception On unknown locale
  721. * @throws Zend_Currency_Exception On unknown display
  722. * @throws Zend_Currency_Exception On precision not between -1 and 30
  723. * @throws Zend_Currency_Exception On problem with script conversion
  724. * @throws Zend_Currency_Exception On unknown options
  725. * @return array
  726. */
  727. protected function _checkOptions(array $options = array())
  728. {
  729. if (count($options) === 0) {
  730. return $this->_options;
  731. }
  732. foreach ($options as $name => $value) {
  733. $name = strtolower($name);
  734. if ($name !== 'format') {
  735. if (gettype($value) === 'string') {
  736. $value = strtolower($value);
  737. }
  738. }
  739. switch($name) {
  740. case 'position':
  741. if (($value !== self::STANDARD) and ($value !== self::RIGHT) and ($value !== self::LEFT)) {
  742. require_once 'Zend/Currency/Exception.php';
  743. throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
  744. }
  745. break;
  746. case 'format':
  747. if ((empty($value) === false) and (Zend_Locale::isLocale($value, null, false) === false)) {
  748. if (!is_string($value) || (strpos($value, '0') === false)) {
  749. require_once 'Zend/Currency/Exception.php';
  750. throw new Zend_Currency_Exception("'" .
  751. ((gettype($value) === 'object') ? get_class($value) : $value)
  752. . "' is no format token");
  753. }
  754. }
  755. break;
  756. case 'display':
  757. if (is_numeric($value) and ($value !== self::NO_SYMBOL) and ($value !== self::USE_SYMBOL) and
  758. ($value !== self::USE_SHORTNAME) and ($value !== self::USE_NAME)) {
  759. require_once 'Zend/Currency/Exception.php';
  760. throw new Zend_Currency_Exception("Unknown display '$value'");
  761. }
  762. break;
  763. case 'precision':
  764. if ($value === null) {
  765. $value = -1;
  766. }
  767. if (($value < -1) or ($value > 30)) {
  768. require_once 'Zend/Currency/Exception.php';
  769. throw new Zend_Currency_Exception("'$value' precision has to be between -1 and 30.");
  770. }
  771. break;
  772. case 'script':
  773. try {
  774. Zend_Locale_Format::convertNumerals(0, $options['script']);
  775. } catch (Zend_Locale_Exception $e) {
  776. require_once 'Zend/Currency/Exception.php';
  777. throw new Zend_Currency_Exception($e->getMessage());
  778. }
  779. break;
  780. default:
  781. break;
  782. }
  783. }
  784. return $options;
  785. }
  786. }