PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Currency.php

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