PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Currency.php

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